Sha256: a071a06f709e4803513bfedf0e7a1c18ff89e041ba263351c1aaafae6b72e415

Contents?: true

Size: 991 Bytes

Versions: 4

Compression:

Stored size: 991 Bytes

Contents

module HTTP::Cookie::URIParser
  module_function

  # Regular Expression taken from RFC 3986 Appendix B
  URIREGEX = %r{
    \A
    (?: (?<scheme> [^:/?\#]+ ) : )?
    (?: // (?<authority> [^/?\#]* ) )?
    (?<path> [^?\#]* )
    (?: \? (?<query> [^\#]* ) )?
    (?: \# (?<fragment> .* ) )?
    \z
  }x

  # Escape RFC 3986 "reserved" characters minus valid characters for path
  # More specifically, gen-delims minus "/" / "?" / "#"
  def escape_path(path)
    path.sub(/\A[^?#]+/) { |p| p.gsub(/[:\[\]@]+/) { |r| CGI.escape(r) } }
  end

  # Parse a URI string or object, relaxing the constraints on the path component
  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
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
http-cookie-1.0.8 lib/http/cookie/uri_parser.rb
http-cookie-1.0.7 lib/http/cookie/uri_parser.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/http-cookie-1.0.6/lib/http/cookie/uri_parser.rb
http-cookie-1.0.6 lib/http/cookie/uri_parser.rb