2004-03-24 06:53:31 -05:00
|
|
|
# = uri/common.rb
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
2004-03-24 06:53:31 -05:00
|
|
|
# Author:: Akira Yamada <akira@ruby-lang.org>
|
|
|
|
# Revision:: $Id$
|
|
|
|
# License::
|
|
|
|
# You can redistribute it and/or modify it under the same term as Ruby.
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
module URI
|
|
|
|
module REGEXP
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# Patterns used to parse URI's
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
module PATTERN
|
2004-03-24 06:53:31 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
2002-01-10 03:00:51 -05:00
|
|
|
# RFC 2396 (URI Generic Syntax)
|
|
|
|
# RFC 2732 (IPv6 Literal Addresses in URL's)
|
|
|
|
# RFC 2373 (IPv6 Addressing Architecture)
|
|
|
|
|
|
|
|
# alpha = lowalpha | upalpha
|
|
|
|
ALPHA = "a-zA-Z"
|
|
|
|
# alphanum = alpha | digit
|
|
|
|
ALNUM = "#{ALPHA}\\d"
|
|
|
|
|
|
|
|
# hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
|
|
|
|
# "a" | "b" | "c" | "d" | "e" | "f"
|
|
|
|
HEX = "a-fA-F\\d"
|
|
|
|
# escaped = "%" hex hex
|
|
|
|
ESCAPED = "%[#{HEX}]{2}"
|
|
|
|
# mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
|
|
|
|
# "(" | ")"
|
|
|
|
# unreserved = alphanum | mark
|
2003-03-29 01:39:50 -05:00
|
|
|
UNRESERVED = "-_.!~*'()#{ALNUM}"
|
2002-01-10 03:00:51 -05:00
|
|
|
# reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
|
|
|
|
# "$" | ","
|
|
|
|
# reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
|
|
|
|
# "$" | "," | "[" | "]" (RFC 2732)
|
|
|
|
RESERVED = ";/?:@&=+$,\\[\\]"
|
|
|
|
|
|
|
|
# uric = reserved | unreserved | escaped
|
|
|
|
URIC = "(?:[#{UNRESERVED}#{RESERVED}]|#{ESCAPED})"
|
|
|
|
# uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
|
|
|
|
# "&" | "=" | "+" | "$" | ","
|
|
|
|
URIC_NO_SLASH = "(?:[#{UNRESERVED};?:@&=+$,]|#{ESCAPED})"
|
|
|
|
# query = *uric
|
|
|
|
QUERY = "#{URIC}*"
|
|
|
|
# fragment = *uric
|
|
|
|
FRAGMENT = "#{URIC}*"
|
|
|
|
|
|
|
|
# domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
|
2003-03-29 01:39:50 -05:00
|
|
|
DOMLABEL = "(?:[#{ALNUM}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
|
2002-01-10 03:00:51 -05:00
|
|
|
# toplabel = alpha | alpha *( alphanum | "-" ) alphanum
|
2003-03-29 01:39:50 -05:00
|
|
|
TOPLABEL = "(?:[#{ALPHA}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
|
2002-01-10 03:00:51 -05:00
|
|
|
# hostname = *( domainlabel "." ) toplabel [ "." ]
|
|
|
|
HOSTNAME = "(?:#{DOMLABEL}\\.)*#{TOPLABEL}\\.?"
|
|
|
|
|
|
|
|
# RFC 2373, APPENDIX B:
|
|
|
|
# IPv6address = hexpart [ ":" IPv4address ]
|
|
|
|
# IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
|
|
|
|
# hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
|
|
|
|
# hexseq = hex4 *( ":" hex4)
|
|
|
|
# hex4 = 1*4HEXDIG
|
|
|
|
#
|
|
|
|
# XXX: This definition has a flaw. "::" + IPv4address must be
|
|
|
|
# allowed too. Here is a replacement.
|
|
|
|
#
|
|
|
|
# IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
|
|
|
|
IPV4ADDR = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
|
|
|
|
# hex4 = 1*4HEXDIG
|
|
|
|
HEX4 = "[#{HEX}]{1,4}"
|
|
|
|
# lastpart = hex4 | IPv4address
|
|
|
|
LASTPART = "(?:#{HEX4}|#{IPV4ADDR})"
|
|
|
|
# hexseq1 = *( hex4 ":" ) hex4
|
|
|
|
HEXSEQ1 = "(?:#{HEX4}:)*#{HEX4}"
|
|
|
|
# hexseq2 = *( hex4 ":" ) lastpart
|
|
|
|
HEXSEQ2 = "(?:#{HEX4}:)*#{LASTPART}"
|
|
|
|
# IPv6address = hexseq2 | [ hexseq1 ] "::" [ hexseq2 ]
|
|
|
|
IPV6ADDR = "(?:#{HEXSEQ2}|(?:#{HEXSEQ1})?::(?:#{HEXSEQ2})?)"
|
|
|
|
|
|
|
|
# IPv6prefix = ( hexseq1 | [ hexseq1 ] "::" [ hexseq1 ] ) "/" 1*2DIGIT
|
|
|
|
# unused
|
|
|
|
|
|
|
|
# ipv6reference = "[" IPv6address "]" (RFC 2732)
|
|
|
|
IPV6REF = "\\[#{IPV6ADDR}\\]"
|
|
|
|
|
|
|
|
# host = hostname | IPv4address
|
|
|
|
# host = hostname | IPv4address | IPv6reference (RFC 2732)
|
|
|
|
HOST = "(?:#{HOSTNAME}|#{IPV4ADDR}|#{IPV6REF})"
|
|
|
|
# port = *digit
|
2005-01-16 10:12:02 -05:00
|
|
|
PORT = '\d*'
|
2002-01-10 03:00:51 -05:00
|
|
|
# hostport = host [ ":" port ]
|
2004-03-08 05:36:50 -05:00
|
|
|
HOSTPORT = "#{HOST}(?::#{PORT})?"
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# userinfo = *( unreserved | escaped |
|
|
|
|
# ";" | ":" | "&" | "=" | "+" | "$" | "," )
|
|
|
|
USERINFO = "(?:[#{UNRESERVED};:&=+$,]|#{ESCAPED})*"
|
|
|
|
|
|
|
|
# pchar = unreserved | escaped |
|
|
|
|
# ":" | "@" | "&" | "=" | "+" | "$" | ","
|
|
|
|
PCHAR = "(?:[#{UNRESERVED}:@&=+$,]|#{ESCAPED})"
|
|
|
|
# param = *pchar
|
|
|
|
PARAM = "#{PCHAR}*"
|
|
|
|
# segment = *pchar *( ";" param )
|
|
|
|
SEGMENT = "#{PCHAR}*(?:;#{PARAM})*"
|
|
|
|
# path_segments = segment *( "/" segment )
|
|
|
|
PATH_SEGMENTS = "#{SEGMENT}(?:/#{SEGMENT})*"
|
|
|
|
|
|
|
|
# server = [ [ userinfo "@" ] hostport ]
|
|
|
|
SERVER = "(?:#{USERINFO}@)?#{HOSTPORT}"
|
|
|
|
# reg_name = 1*( unreserved | escaped | "$" | "," |
|
|
|
|
# ";" | ":" | "@" | "&" | "=" | "+" )
|
2007-12-10 04:31:28 -05:00
|
|
|
REG_NAME = "(?:[#{UNRESERVED}$,;:@&=+]|#{ESCAPED})+"
|
2002-01-10 03:00:51 -05:00
|
|
|
# authority = server | reg_name
|
|
|
|
AUTHORITY = "(?:#{SERVER}|#{REG_NAME})"
|
|
|
|
|
|
|
|
# rel_segment = 1*( unreserved | escaped |
|
|
|
|
# ";" | "@" | "&" | "=" | "+" | "$" | "," )
|
|
|
|
REL_SEGMENT = "(?:[#{UNRESERVED};@&=+$,]|#{ESCAPED})+"
|
|
|
|
|
|
|
|
# scheme = alpha *( alpha | digit | "+" | "-" | "." )
|
2003-03-29 01:39:50 -05:00
|
|
|
SCHEME = "[#{ALPHA}][-+.#{ALPHA}\\d]*"
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# abs_path = "/" path_segments
|
|
|
|
ABS_PATH = "/#{PATH_SEGMENTS}"
|
|
|
|
# rel_path = rel_segment [ abs_path ]
|
|
|
|
REL_PATH = "#{REL_SEGMENT}(?:#{ABS_PATH})?"
|
|
|
|
# net_path = "//" authority [ abs_path ]
|
|
|
|
NET_PATH = "//#{AUTHORITY}(?:#{ABS_PATH})?"
|
|
|
|
|
|
|
|
# hier_part = ( net_path | abs_path ) [ "?" query ]
|
|
|
|
HIER_PART = "(?:#{NET_PATH}|#{ABS_PATH})(?:\\?(?:#{QUERY}))?"
|
|
|
|
# opaque_part = uric_no_slash *uric
|
|
|
|
OPAQUE_PART = "#{URIC_NO_SLASH}#{URIC}*"
|
|
|
|
|
|
|
|
# absoluteURI = scheme ":" ( hier_part | opaque_part )
|
|
|
|
ABS_URI = "#{SCHEME}:(?:#{HIER_PART}|#{OPAQUE_PART})"
|
|
|
|
# relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
|
|
|
|
REL_URI = "(?:#{NET_PATH}|#{ABS_PATH}|#{REL_PATH})(?:\\?#{QUERY})?"
|
|
|
|
|
|
|
|
# URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
|
|
|
|
URI_REF = "(?:#{ABS_URI}|#{REL_URI})?(?:##{FRAGMENT})?"
|
|
|
|
|
|
|
|
# XXX:
|
|
|
|
X_ABS_URI = "
|
2004-03-24 06:53:31 -05:00
|
|
|
(#{PATTERN::SCHEME}): (?# 1: scheme)
|
|
|
|
(?:
|
|
|
|
(#{PATTERN::OPAQUE_PART}) (?# 2: opaque)
|
|
|
|
|
|
|
|
|
(?:(?:
|
|
|
|
//(?:
|
|
|
|
(?:(?:(#{PATTERN::USERINFO})@)? (?# 3: userinfo)
|
|
|
|
(?:(#{PATTERN::HOST})(?::(\\d*))?))?(?# 4: host, 5: port)
|
|
|
|
|
|
|
|
|
(#{PATTERN::REG_NAME}) (?# 6: registry)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?!//)) (?# XXX: '//' is the mark for hostport)
|
|
|
|
(#{PATTERN::ABS_PATH})? (?# 7: path)
|
|
|
|
)(?:\\?(#{PATTERN::QUERY}))? (?# 8: query)
|
|
|
|
)
|
|
|
|
(?:\\#(#{PATTERN::FRAGMENT}))? (?# 9: fragment)
|
2002-01-10 03:00:51 -05:00
|
|
|
"
|
|
|
|
X_REL_URI = "
|
|
|
|
(?:
|
|
|
|
(?:
|
|
|
|
//
|
|
|
|
(?:
|
|
|
|
(?:(#{PATTERN::USERINFO})@)? (?# 1: userinfo)
|
|
|
|
(#{PATTERN::HOST})?(?::(\\d*))? (?# 2: host, 3: port)
|
|
|
|
|
|
|
|
|
(#{PATTERN::REG_NAME}) (?# 4: registry)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(#{PATTERN::REL_SEGMENT}) (?# 5: rel_segment)
|
|
|
|
)?
|
|
|
|
(#{PATTERN::ABS_PATH})? (?# 6: abs_path)
|
|
|
|
(?:\\?(#{PATTERN::QUERY}))? (?# 7: query)
|
|
|
|
(?:\\#(#{PATTERN::FRAGMENT}))? (?# 8: fragment)
|
|
|
|
"
|
2004-03-24 06:53:31 -05:00
|
|
|
# :startdoc:
|
2002-01-10 03:00:51 -05:00
|
|
|
end # PATTERN
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
2002-01-10 03:00:51 -05:00
|
|
|
# for URI::split
|
|
|
|
ABS_URI = Regexp.new('^' + PATTERN::X_ABS_URI + '$', #'
|
2007-12-23 20:51:49 -05:00
|
|
|
Regexp::EXTENDED).freeze
|
2002-01-10 03:00:51 -05:00
|
|
|
REL_URI = Regexp.new('^' + PATTERN::X_REL_URI + '$', #'
|
2007-12-23 20:51:49 -05:00
|
|
|
Regexp::EXTENDED).freeze
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# for URI::extract
|
2007-12-23 20:51:49 -05:00
|
|
|
URI_REF = Regexp.new(PATTERN::URI_REF).freeze
|
|
|
|
ABS_URI_REF = Regexp.new(PATTERN::X_ABS_URI, Regexp::EXTENDED).freeze
|
|
|
|
REL_URI_REF = Regexp.new(PATTERN::X_REL_URI, Regexp::EXTENDED).freeze
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# for URI::escape/unescape
|
2007-12-23 20:51:49 -05:00
|
|
|
ESCAPED = Regexp.new(PATTERN::ESCAPED).freeze
|
|
|
|
UNSAFE = Regexp.new("[^#{PATTERN::UNRESERVED}#{PATTERN::RESERVED}]").freeze
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# for Generic#initialize
|
2007-12-23 20:51:49 -05:00
|
|
|
SCHEME = Regexp.new("^#{PATTERN::SCHEME}$").freeze #"
|
|
|
|
USERINFO = Regexp.new("^#{PATTERN::USERINFO}$").freeze #"
|
|
|
|
HOST = Regexp.new("^#{PATTERN::HOST}$").freeze #"
|
|
|
|
PORT = Regexp.new("^#{PATTERN::PORT}$").freeze #"
|
|
|
|
OPAQUE = Regexp.new("^#{PATTERN::OPAQUE_PART}$").freeze #"
|
|
|
|
REGISTRY = Regexp.new("^#{PATTERN::REG_NAME}$").freeze #"
|
|
|
|
ABS_PATH = Regexp.new("^#{PATTERN::ABS_PATH}$").freeze #"
|
|
|
|
REL_PATH = Regexp.new("^#{PATTERN::REL_PATH}$").freeze #"
|
|
|
|
QUERY = Regexp.new("^#{PATTERN::QUERY}$").freeze #"
|
|
|
|
FRAGMENT = Regexp.new("^#{PATTERN::FRAGMENT}$").freeze #"
|
2004-03-24 06:53:31 -05:00
|
|
|
# :startdoc:
|
2002-01-10 03:00:51 -05:00
|
|
|
end # REGEXP
|
|
|
|
|
2004-07-17 09:14:35 -04:00
|
|
|
module Util # :nodoc:
|
2002-01-10 03:00:51 -05:00
|
|
|
def make_components_hash(klass, array_hash)
|
|
|
|
tmp = {}
|
|
|
|
if array_hash.kind_of?(Array) &&
|
2004-03-24 06:53:31 -05:00
|
|
|
array_hash.size == klass.component.size - 1
|
|
|
|
klass.component[1..-1].each_index do |i|
|
|
|
|
begin
|
|
|
|
tmp[klass.component[i + 1]] = array_hash[i].clone
|
|
|
|
rescue TypeError
|
|
|
|
tmp[klass.component[i + 1]] = array_hash[i]
|
|
|
|
end
|
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
elsif array_hash.kind_of?(Hash)
|
2004-03-24 06:53:31 -05:00
|
|
|
array_hash.each do |key, value|
|
|
|
|
begin
|
|
|
|
tmp[key] = value.clone
|
|
|
|
rescue TypeError
|
|
|
|
tmp[key] = value
|
|
|
|
end
|
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
else
|
2004-03-24 06:53:31 -05:00
|
|
|
raise ArgumentError,
|
|
|
|
"expected Array of or Hash of components of #{klass.to_s} (#{klass.component[1..-1].join(', ')})"
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
tmp[:scheme] = klass.to_s.sub(/\A.*::/, '').downcase
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
end
|
|
|
|
module_function :make_components_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
module Escape
|
|
|
|
include REGEXP
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI.escape(str [, unsafe])
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +str+::
|
|
|
|
# String to replaces in.
|
|
|
|
# +unsafe+::
|
|
|
|
# Regexp that matches all symbols that must be replaced with codes.
|
2004-07-17 09:14:35 -04:00
|
|
|
# By default uses <tt>REGEXP::UNSAFE</tt>.
|
2005-11-12 21:02:24 -05:00
|
|
|
# When this argument is a String, it represents a character set.
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Escapes the string, replacing all unsafe characters with codes.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
2005-06-24 00:18:16 -04:00
|
|
|
# enc_uri = URI.escape("http://example.com/?a=\11\15")
|
2004-03-24 06:53:31 -05:00
|
|
|
# p enc_uri
|
2005-06-24 00:18:16 -04:00
|
|
|
# # => "http://example.com/?a=%09%0D"
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# p URI.unescape(enc_uri)
|
2005-06-24 00:18:16 -04:00
|
|
|
# # => "http://example.com/?a=\t\r"
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2005-11-12 21:02:24 -05:00
|
|
|
# p URI.escape("@?@!", "!?")
|
|
|
|
# # => "@%3F@%21"
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def escape(str, unsafe = UNSAFE)
|
|
|
|
unless unsafe.kind_of?(Regexp)
|
2004-03-24 06:53:31 -05:00
|
|
|
# perhaps unsafe is String object
|
2005-11-12 21:02:24 -05:00
|
|
|
unsafe = Regexp.new("[#{Regexp.quote(unsafe)}]", false, 'N')
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
* ext/json/lib/json/pure/generator.rb,
ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb,
ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb,
lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb,
lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb,
lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb,
lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb,
lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb,
lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb,
lib/yaml/encoding.rb: performance tuning arround String#gsub.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-12 01:18:06 -05:00
|
|
|
str.gsub(unsafe) do
|
|
|
|
us = $&
|
2004-03-24 06:53:31 -05:00
|
|
|
tmp = ''
|
|
|
|
us.each_byte do |uc|
|
|
|
|
tmp << sprintf('%%%02X', uc)
|
|
|
|
end
|
|
|
|
tmp
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias encode escape
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI.unescape(str)
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +str+::
|
|
|
|
# Unescapes the string.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
2005-06-24 00:18:16 -04:00
|
|
|
# enc_uri = URI.escape("http://example.com/?a=\11\15")
|
2004-03-24 06:53:31 -05:00
|
|
|
# p enc_uri
|
2005-06-24 00:18:16 -04:00
|
|
|
# # => "http://example.com/?a=%09%0D"
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# p URI.unescape(enc_uri)
|
2005-06-24 00:18:16 -04:00
|
|
|
# # => "http://example.com/?a=\t\r"
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def unescape(str)
|
|
|
|
str.gsub(ESCAPED) do
|
2004-03-24 06:53:31 -05:00
|
|
|
$&[1,2].hex.chr
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias decode unescape
|
|
|
|
end
|
|
|
|
|
|
|
|
include REGEXP
|
|
|
|
extend Escape
|
|
|
|
|
|
|
|
@@schemes = {}
|
2004-03-24 06:53:31 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Base class for all URI exceptions.
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
class Error < StandardError; end
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# Not a URI.
|
|
|
|
#
|
|
|
|
class InvalidURIError < Error; end
|
|
|
|
#
|
|
|
|
# Not a URI component.
|
|
|
|
#
|
|
|
|
class InvalidComponentError < Error; end
|
|
|
|
#
|
|
|
|
# URI is valid, bad usage is not.
|
|
|
|
#
|
|
|
|
class BadURIError < Error; end
|
|
|
|
|
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI::split(uri)
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +uri+::
|
|
|
|
# String with URI.
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Splits the string on following parts and returns array with result:
|
|
|
|
#
|
|
|
|
# * Scheme
|
|
|
|
# * Userinfo
|
|
|
|
# * Host
|
|
|
|
# * Port
|
|
|
|
# * Registry
|
|
|
|
# * Path
|
|
|
|
# * Opaque
|
|
|
|
# * Query
|
|
|
|
# * Fragment
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# p URI.split("http://www.ruby-lang.org/")
|
|
|
|
# # => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def self.split(uri)
|
|
|
|
case uri
|
|
|
|
when ''
|
|
|
|
# null uri
|
|
|
|
|
|
|
|
when ABS_URI
|
2002-12-12 01:42:42 -05:00
|
|
|
scheme, opaque, userinfo, host, port,
|
2004-03-24 06:53:31 -05:00
|
|
|
registry, path, query, fragment = $~[1..-1]
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
# URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
|
|
|
|
|
|
|
|
# absoluteURI = scheme ":" ( hier_part | opaque_part )
|
|
|
|
# hier_part = ( net_path | abs_path ) [ "?" query ]
|
|
|
|
# opaque_part = uric_no_slash *uric
|
|
|
|
|
|
|
|
# abs_path = "/" path_segments
|
|
|
|
# net_path = "//" authority [ abs_path ]
|
|
|
|
|
|
|
|
# authority = server | reg_name
|
|
|
|
# server = [ [ userinfo "@" ] hostport ]
|
|
|
|
|
|
|
|
if !scheme
|
2004-03-24 06:53:31 -05:00
|
|
|
raise InvalidURIError,
|
|
|
|
"bad URI(absolute but no scheme): #{uri}"
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
if !opaque && (!path && (!host && !registry))
|
2004-03-24 06:53:31 -05:00
|
|
|
raise InvalidURIError,
|
|
|
|
"bad URI(absolute but no path): #{uri}"
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
when REL_URI
|
|
|
|
scheme = nil
|
|
|
|
opaque = nil
|
|
|
|
|
|
|
|
userinfo, host, port, registry,
|
2004-03-24 06:53:31 -05:00
|
|
|
rel_segment, abs_path, query, fragment = $~[1..-1]
|
2002-01-10 03:00:51 -05:00
|
|
|
if rel_segment && abs_path
|
2004-03-24 06:53:31 -05:00
|
|
|
path = rel_segment + abs_path
|
2002-01-10 03:00:51 -05:00
|
|
|
elsif rel_segment
|
2004-03-24 06:53:31 -05:00
|
|
|
path = rel_segment
|
2002-01-10 03:00:51 -05:00
|
|
|
elsif abs_path
|
2004-03-24 06:53:31 -05:00
|
|
|
path = abs_path
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
|
|
|
|
|
|
|
|
# relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
|
|
|
|
|
|
|
|
# net_path = "//" authority [ abs_path ]
|
|
|
|
# abs_path = "/" path_segments
|
|
|
|
# rel_path = rel_segment [ abs_path ]
|
|
|
|
|
|
|
|
# authority = server | reg_name
|
|
|
|
# server = [ [ userinfo "@" ] hostport ]
|
|
|
|
|
|
|
|
else
|
|
|
|
raise InvalidURIError, "bad URI(is not URI?): #{uri}"
|
|
|
|
end
|
|
|
|
|
|
|
|
path = '' if !path && !opaque # (see RFC2396 Section 5.2)
|
|
|
|
ret = [
|
|
|
|
scheme,
|
2004-03-24 06:53:31 -05:00
|
|
|
userinfo, host, port, # X
|
|
|
|
registry, # X
|
|
|
|
path, # Y
|
|
|
|
opaque, # Y
|
2002-01-10 03:00:51 -05:00
|
|
|
query,
|
|
|
|
fragment
|
|
|
|
]
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI::parse(uri_str)
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +uri_str+::
|
|
|
|
# String with URI.
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Creates one of the URI's subclasses instance from the string.
|
|
|
|
#
|
|
|
|
# == Raises
|
|
|
|
#
|
|
|
|
# URI::InvalidURIError
|
|
|
|
# Raised if URI given is not a correct one.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# uri = URI.parse("http://www.ruby-lang.org/")
|
|
|
|
# p uri
|
|
|
|
# # => #<URI::HTTP:0x202281be URL:http://www.ruby-lang.org/>
|
|
|
|
# p uri.scheme
|
|
|
|
# # => "http"
|
|
|
|
# p uri.host
|
|
|
|
# # => "www.ruby-lang.org"
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def self.parse(uri)
|
|
|
|
scheme, userinfo, host, port,
|
|
|
|
registry, path, opaque, query, fragment = self.split(uri)
|
|
|
|
|
|
|
|
if scheme && @@schemes.include?(scheme.upcase)
|
|
|
|
@@schemes[scheme.upcase].new(scheme, userinfo, host, port,
|
2004-03-24 06:53:31 -05:00
|
|
|
registry, path, opaque, query,
|
|
|
|
fragment)
|
2002-01-10 03:00:51 -05:00
|
|
|
else
|
|
|
|
Generic.new(scheme, userinfo, host, port,
|
2004-03-24 06:53:31 -05:00
|
|
|
registry, path, opaque, query,
|
|
|
|
fragment)
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI::join(str[, str, ...])
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +str+::
|
|
|
|
# String(s) to work with
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Joins URIs.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
2005-06-24 00:18:16 -04:00
|
|
|
# p URI.join("http://localhost/","main.rbx")
|
|
|
|
# # => #<URI::HTTP:0x2022ac02 URL:http://localhost/main.rbx>
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-02-06 23:51:11 -05:00
|
|
|
def self.join(*str)
|
|
|
|
u = self.parse(str[0])
|
|
|
|
str[1 .. -1].each do |x|
|
|
|
|
u = u.merge(x)
|
|
|
|
end
|
|
|
|
u
|
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI::extract(str[, schemes][,&blk])
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +str+::
|
|
|
|
# String to extract URIs from.
|
|
|
|
# +schemes+::
|
|
|
|
# Limit URI matching to a specific schemes.
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Extracts URIs from a string. If block given, iterates through all matched URIs.
|
|
|
|
# Returns nil if block given or array with matches.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require "uri"
|
|
|
|
#
|
2005-06-23 04:22:44 -04:00
|
|
|
# URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
|
|
|
|
# # => ["http://foo.example.com/bla", "mailto:test@example.com"]
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2003-12-07 23:03:11 -05:00
|
|
|
def self.extract(str, schemes = nil, &block)
|
|
|
|
if block_given?
|
|
|
|
str.scan(regexp(schemes)) { yield $& }
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
result = []
|
|
|
|
str.scan(regexp(schemes)) { result.push $& }
|
|
|
|
result
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
2003-12-07 23:03:11 -05:00
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
|
|
|
# == Synopsis
|
|
|
|
#
|
|
|
|
# URI::regexp([match_schemes])
|
|
|
|
#
|
|
|
|
# == Args
|
|
|
|
#
|
|
|
|
# +match_schemes+::
|
|
|
|
# Array of schemes. If given, resulting regexp matches to URIs
|
|
|
|
# whose scheme is one of the match_schemes.
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
# Returns a Regexp object which matches to URI-like strings.
|
|
|
|
# The Regexp object returned by this method includes arbitrary
|
|
|
|
# number of capture group (parentheses). Never rely on it's number.
|
|
|
|
#
|
|
|
|
# == Usage
|
|
|
|
#
|
|
|
|
# require 'uri'
|
|
|
|
#
|
|
|
|
# # extract first URI from html_string
|
|
|
|
# html_string.slice(URI.regexp)
|
|
|
|
#
|
|
|
|
# # remove ftp URIs
|
|
|
|
# html_string.sub(URI.regexp(['ftp'])
|
|
|
|
#
|
|
|
|
# # You should not rely on the number of parentheses
|
|
|
|
# html_string.scan(URI.regexp) do |*matches|
|
|
|
|
# p $&
|
|
|
|
# end
|
|
|
|
#
|
2003-12-07 23:03:11 -05:00
|
|
|
def self.regexp(schemes = nil)
|
|
|
|
unless schemes
|
|
|
|
ABS_URI_REF
|
2002-01-10 03:00:51 -05:00
|
|
|
else
|
2003-12-07 23:03:11 -05:00
|
|
|
/(?=#{Regexp.union(*schemes)}:)#{PATTERN::X_ABS_URI}/xn
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
end
|
2004-07-05 10:06:10 -04:00
|
|
|
|
|
|
|
module Kernel
|
|
|
|
# alias for URI.parse.
|
|
|
|
#
|
|
|
|
# This method is introduced at 1.8.2.
|
|
|
|
def URI(uri_str) # :doc:
|
|
|
|
URI.parse(uri_str)
|
|
|
|
end
|
|
|
|
module_function :URI
|
|
|
|
end
|