123456789_123456789_123456789_123456789_123456789_

Class: URI::RFC3986_Parser

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/uri/rfc3986_parser.rb

Constant Summary

  • FRAGMENT =
    # File 'lib/uri/rfc3986_parser.rb', line 33
    %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+].source
  • HOST =

    ::URI defined in RFC3986

    # File 'lib/uri/rfc3986_parser.rb', line 5
    %r[
      (?<IP-literal>\[(?:
          (?<IPv6address>
            (?:\h{1,4}:){6}
            (?<ls32>\h{1,4}:\h{1,4}
            | (?<IPv4address>(?<dec-octet>[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]|\d)
                \.\g<dec-octet>\.\g<dec-octet>\.\g<dec-octet>)
            )
          | ::(?:\h{1,4}:){5}\g<ls32>
          | \h{1,4}?::(?:\h{1,4}:){4}\g<ls32>
          | (?:(?:\h{1,4}:)?\h{1,4})?::(?:\h{1,4}:){3}\g<ls32>
          | (?:(?:\h{1,4}:){,2}\h{1,4})?::(?:\h{1,4}:){2}\g<ls32>
          | (?:(?:\h{1,4}:){,3}\h{1,4})?::\h{1,4}:\g<ls32>
          | (?:(?:\h{1,4}:){,4}\h{1,4})?::\g<ls32>
          | (?:(?:\h{1,4}:){,5}\h{1,4})?::\h{1,4}
          | (?:(?:\h{1,4}:){,6}\h{1,4})?::
          )
        | (?<IPvFuture>v\h\.[!$&-.0-9:;=A-Z_a-z~])
        )\])
    | \g<IPv4address>
    | (?<reg-name>(?:%\h\h|[!$&-.0-9;=A-Z_a-z~])*+)
    ]x
  • RFC3986_URI =
    # File 'lib/uri/rfc3986_parser.rb', line 35
    %r[\A
    (?<seg>#{SEG}){0}
    (?<URI>
      (?<scheme>#{SCHEME}):
      (?<hier-part>//
        (?<authority>
          (?:(?<userinfo>#{USERINFO.source})@)?
          (?<host>#{HOST.source.delete(" \n")})
          (?::(?<port>\d*+))?
        )
        (?<path-abempty>(?:/\g<seg>*+)?)
      | (?<path-absolute>/((?!/)\g<seg>++)?)
      | (?<path-rootless>(?!/)\g<seg>++)
      | (?<path-empty>)
      )
      (?:\?(?<query>[^\#]*+))?
      (?:\#(?<fragment>#{FRAGMENT}))?
    )\z]x
  • RFC3986_relative_ref =
    # File 'lib/uri/rfc3986_parser.rb', line 54
    %r[\A
    (?<seg>#{SEG}){0}
    (?<relative-ref>
      (?<relative-part>//
        (?<authority>
          (?:(?<userinfo>#{USERINFO.source})@)?
          (?<host>#{HOST.source.delete(" \n")}(?<!/))?
          (?::(?<port>\d*+))?
        )
        (?<path-abempty>(?:/\g<seg>*+)?)
      | (?<path-absolute>/\g<seg>*+)
      | (?<path-noscheme>#{SEG_NC}(?:/\g<seg>*)?)
      | (?<path-empty>)
      )
      (?:\?(?<query>[^#]*+))?
      (?:\#(?<fragment>#{FRAGMENT}))?
    )\z]x
  • SCHEME =
    # File 'lib/uri/rfc3986_parser.rb', line 30
    %r[[A-Za-z][\-.0-9A-Za-z]*].source
  • SEG =
    # File 'lib/uri/rfc3986_parser.rb', line 31
    %r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/])].source
  • SEG_NC =
    # File 'lib/uri/rfc3986_parser.rb', line 32
    %r[(?:%\h\h|[!$&-.0-9;=@A-Z_a-z~])].source
  • USERINFO =
    # File 'lib/uri/rfc3986_parser.rb', line 28
    /(?:%\h\h|[!$&-.0-9:;=A-Z_a-z~])*+/

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newRFC3986_Parser

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 73

def initialize
  @regexp = default_regexp.each_value(&:freeze).freeze
end

Instance Attribute Details

#regexp (readonly)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 71

attr_reader :regexp

Instance Method Details

#convert_to_uri(uri) (private)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 171

def convert_to_uri(uri)
  if uri.is_a?(URI::Generic)
    uri
  elsif uri = String.try_convert(uri)
    parse(uri)
  else
    raise ArgumentError,
      "bad argument (expected URI object or URI string)"
  end
end

#default_regexp (private)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 157

def default_regexp # :nodoc:
  {
    SCHEME: %r[\A#{SCHEME}\z]o,
    USERINFO: %r[\A#{USERINFO}\z]o,
    HOST: %r[\A#{HOST}\z]o,
    ABS_PATH: %r[\A/#{SEG}*+\z]o,
    REL_PATH: %r[\A(?!/)#{SEG}++\z]o,
    QUERY: %r[\A(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+\z],
    FRAGMENT: %r[\A#{FRAGMENT}\z]o,
    OPAQUE: %r[\A(?:[^/].*)?\z],
    PORT: /\A[\x09\x0a\x0c\x0d ]*+\d*[\x09\x0a\x0c\x0d ]*\z/,
  }
end

#inspect

See additional method definition at line 146.

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 150

def inspect
  @@to_s.bind_call(self)
end

#join(*uris)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 139

def join(*uris) # :nodoc:
  uris[0] = convert_to_uri(uris[0])
  uris.inject :merge
end

#parse(uri)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 134

def parse(uri) # :nodoc:
  URI.for(*self.split(uri), self)
end

#split(uri)

[ GitHub ]

  
# File 'lib/uri/rfc3986_parser.rb', line 77

def split(uri) #:nodoc:
  begin
    uri = uri.to_str
  rescue NoMethodError
    raise InvalidURIError, "bad URI(is not URI?): #{uri.inspect}"
  end
  uri.ascii_only? or
    raise InvalidURIError, "URI must be ascii only #{uri.dump}"
  if m = RFC3986_URI.match(uri)
    query = m["query"]
    scheme = m["scheme"]
    opaque = m["path-rootless"]
    if opaque
      opaque << "?#{query}" if query
      [ scheme,
        nil, # userinfo
        nil, # host
        nil, # port
        nil, # registry
        nil, # path
        opaque,
        nil, # query
        m["fragment"]
      ]
    else # normal
      [ scheme,
        m["userinfo"],
        m["host"],
        m["port"],
        nil, # registry
        (m["path-abempty"] ||
         m["path-absolute"] ||
         m["path-empty"]),
        nil, # opaque
        query,
        m["fragment"]
      ]
    end
  elsif m = RFC3986_relative_ref.match(uri)
    [ nil, # scheme
      m["userinfo"],
      m["host"],
      m["port"],
      nil, # registry,
      (m["path-abempty"] ||
       m["path-absolute"] ||
       m["path-noscheme"] ||
       m["path-empty"]),
      nil, # opaque
      m["query"],
      m["fragment"]
    ]
  else
    raise InvalidURIError, "bad URI(is not URI?): #{uri.inspect}"
  end
end