module URI
@@schemes = {}
def self.register_scheme(scheme, klass)
@@schemes[scheme.to_s.upcase] = klass
end
def self.scheme_list
@@schemes
end
class Error < StandardError; end
class InvalidURIError < Error; end
class InvalidComponentError < Error; end
UNRESERVED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
class Generic
attr_accessor :scheme, :userinfo, :host, :port, :path, :query, :fragment
def initialize(scheme, userinfo, host, port, path, query, fragment)
@scheme = scheme
@userinfo = userinfo
@host = host
@port = port || (host ? default_port : nil)
@path = path
@query = query
@fragment = fragment
end
def default_port
nil
end
def hostname
@host
end
def absolute?
!@scheme.nil?
end
def relative?
@scheme.nil?
end
def to_s
s = +""
s << "#{@scheme}:" if @scheme
if @host || @userinfo
s << "//"
s << "#{@userinfo}@" if @userinfo
s << @host.to_s
s << ":#{@port}" if @port && @port != default_port
end
s << @path.to_s
s << "?#{@query}" if @query
s << "##{@fragment}" if @fragment
s
end
def inspect
"#<#{self.class} #{self}>"
end
def ==(other)
other.is_a?(URI::Generic) && to_s == other.to_s
end
end
class HTTP < Generic
def default_port
80
end
def request_uri
p = @path.nil? || @path.empty? ? "/" : @path
@query ? "#{p}?#{@query}" : p
end
end
class HTTPS < HTTP
def default_port
443
end
end
class FTP < Generic
def default_port
21
end
end
SCHEME_CLASSES = {
"http" => HTTP,
"https" => HTTPS,
"ftp" => FTP,
}
def self.split(str)
m = %r{\A(?:([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?\z}.match(str)
raise InvalidURIError, "bad URI(is not URI?): #{str.inspect}" unless m
scheme = m[1]
authority = m[3]
path = m[4]
query = m[5]
fragment = m[6]
userinfo = nil
host = nil
port = nil
unless authority.nil?
auth = authority
if (at = auth.index("@"))
userinfo = auth[0...at]
auth = auth[(at + 1)..]
end
if (colon = auth.rindex(":"))
maybe_port = auth[(colon + 1)..]
if maybe_port =~ /\A\d*\z/
port = maybe_port.empty? ? nil : maybe_port.to_i
auth = auth[0...colon]
end
end
host = auth
end
[scheme, userinfo, host, port, path, query, fragment]
end
def self.parse(str)
scheme, userinfo, host, port, path, query, fragment = split(str)
klass = scheme ? SCHEME_CLASSES[scheme.downcase] || Generic : Generic
klass.new(scheme, userinfo, host, port, path, query, fragment)
end
def self.join(*parts)
parts = parts.map { |p| p.is_a?(Generic) ? p : parse(p.to_s) }
base = parts.shift
parts.each { |rel| base = merge(base, rel) }
base
end
def self.merge(base, rel)
return rel if rel.absolute?
scheme = base.scheme
host = base.host
port = base.port
userinfo = base.userinfo
if rel.host
host = rel.host
port = rel.port
userinfo = rel.userinfo
path = rel.path
elsif rel.path.nil? || rel.path.empty?
path = base.path
elsif rel.path.start_with?("/")
path = rel.path
else
dir = base.path.to_s.sub(%r{[^/]*\z}, "")
path = dir + rel.path
end
klass = scheme ? SCHEME_CLASSES[scheme.downcase] || Generic : Generic
klass.new(scheme, userinfo, host, port, path, rel.query, rel.fragment)
end
def self.encode_component(str)
out = +""
str.to_s.each_byte do |b|
c = b.chr
if UNRESERVED.include?(c)
out << c
else
out << format("%%%02X", b)
end
end
out
end
def self.decode_component(str)
str.to_s.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def self.encode_www_form(enum)
pairs = enum.is_a?(Hash) ? enum.to_a : enum
pairs.map { |k, v| "#{encode_www_form_component(k)}=#{encode_www_form_component(v)}" }.join("&")
end
def self.encode_www_form_component(str)
out = +""
str.to_s.each_byte do |b|
c = b.chr
if UNRESERVED.include?(c)
out << c
elsif c == " "
out << "+"
else
out << format("%%%02X", b)
end
end
out
end
def self.decode_www_form(str)
str.to_s.split("&").map do |pair|
k, v = pair.split("=", 2)
[decode_www_form_component(k.to_s), decode_www_form_component(v.to_s)]
end
end
def self.decode_www_form_component(str)
str.to_s.tr("+", " ").gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
class RFC2396_Parser
DEFAULT_UNSAFE = /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/
def parse(uri)
URI.parse(uri)
end
def split(uri)
URI.split(uri)
end
def escape(str, unsafe = DEFAULT_UNSAFE)
str.to_s.gsub(unsafe) do |c|
c.each_byte.map { |b| format("%%%02X", b) }.join
end
end
def unescape(str)
str.to_s.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def regexp(component = nil)
table = {
UNSAFE: DEFAULT_UNSAFE,
ESCAPED: /%[a-fA-F0-9]{2}/,
}
component ? table[component] : table
end
end
DEFAULT_PARSER = RFC2396_Parser.new
RFC2396_PARSER = DEFAULT_PARSER
Parser = RFC2396_Parser
RFC3986_Parser = RFC2396_Parser
RFC3986_PARSER = DEFAULT_PARSER
end
def URI(str)
str.is_a?(URI::Generic) ? str : URI.parse(str)
end