Class: URI::RFC3986_Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/uri/rfc3986_parser.rb

Overview

:nodoc:

Constant Summary collapse

HOST =

URI defined in RFC3986

%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
USERINFO =
/(?:%\h\h|[!$&-.0-9:;=A-Z_a-z~])*+/
SCHEME =
%r[[A-Za-z][+\-.0-9A-Za-z]*+].source
SEG =
%r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/])].source
SEG_NC =
%r[(?:%\h\h|[!$&-.0-9;=@A-Z_a-z~])].source
FRAGMENT =
%r[(?:%\h\h|[!$&-.0-9:;=@A-Z_a-z~/?])*+].source
RFC3986_URI =
%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 =
%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
@@to_s =
Kernel.instance_method(:to_s)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRFC3986_Parser

Returns a new instance of RFC3986_Parser.



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

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

Instance Method Details

#inspectObject



146
147
148
# File 'lib/uri/rfc3986_parser.rb', line 146

def inspect
  @@to_s.bind_call(self)
end

#join(*uris) ⇒ Object

:nodoc:



139
140
141
142
# 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) ⇒ Object

:nodoc:



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

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

#split(uri) ⇒ Object

:nodoc:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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