Module: HTTP::Cookie::URIParser

Defined in:
lib/http/cookie/uri_parser.rb

Constant Summary collapse

URIREGEX =

Regular Expression taken from RFC 3986 Appendix B

%r{
  \A
  (?: (?<scheme> [^:/?\#]+ ) : )?
  (?: // (?<authority> [^/?\#]* ) )?
  (?<path> [^?\#]* )
  (?: \? (?<query> [^\#]* ) )?
  (?: \# (?<fragment> .* ) )?
  \z
}x

Class Method Summary collapse

Class Method Details

.escape_path(path) ⇒ Object

Escape RFC 3986 “reserved” characters minus valid characters for path More specifically, gen-delims minus “/” / “?” / “#”



17
18
19
# File 'lib/http/cookie/uri_parser.rb', line 17

def escape_path(path)
  path.sub(/\A[^?#]+/) { |p| p.gsub(/[:\[\]@]+/) { |r| CGI.escape(r) } }
end

.parse(uri) ⇒ Object

Parse a URI string or object, relaxing the constraints on the path component



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/http/cookie/uri_parser.rb', line 22

def parse(uri)
  URI(uri)
rescue URI::InvalidURIError
  str = String.try_convert(uri) or
    raise ArgumentError, "bad argument (expected URI object or URI string)"

  m = URIREGEX.match(str) or raise

  path = m[:path]
  str[m.begin(:path)...m.end(:path)] = escape_path(path)
  uri = URI.parse(str)
  uri.__send__(:set_path, path)
  uri
end