module Bundler::URI
def self._decode_uri_component(regexp, str, enc)
def self._decode_uri_component(regexp, str, enc) raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/.match?(str) str.b.gsub(regexp, TBLDECWWWCOMP_).force_encoding(enc) end
def self._encode_uri_component(regexp, table, str, enc)
def self._encode_uri_component(regexp, table, str, enc) str = str.to_s.dup if str.encoding != Encoding::ASCII_8BIT if enc && enc != Encoding::ASCII_8BIT str.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace) str.encode!(enc, fallback: ->(x){"&##{x.ord};"}) end str.force_encoding(Encoding::ASCII_8BIT) end str.gsub!(regexp, table) str.force_encoding(Encoding::US_ASCII) end
def self.decode_uri_component(str, enc=Encoding::UTF_8)
def self.decode_uri_component(str, enc=Encoding::UTF_8) _decode_uri_component(/%\h\h/, str, enc) end
def self.decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false)
# => [["foo", "0"], ["bar", "1"], ["baz", ""]]
Bundler::URI.decode_www_form('foo=0--bar=1--baz', separator: '--')
A different separator may be specified:
# => [["foo", "0"], ["", ""], ["bar", "1"], ["", ""], ["baz", "2"]]
Bundler::URI.decode_www_form('foo=0&&bar=1&&baz=2')
The given string may contain consecutive separators:
# => [["f#o", "/"], ["b-r", "$"], ["b z", "@"]]
Bundler::URI.decode_www_form('f%23o=%2F&b-r=%24&b+z=%40')
similar to those performed in Bundler::URI.decode_www_form_component:
The returned strings have certain conversions,
# => [["foo", "0"], ["bar", "1"], ["baz", ""]]
Bundler::URI.decode_www_form('foo=0&bar=1&baz')
A simple example:
{String#scrub}[https://docs.ruby-lang.org/en/master/String.html#method-i-scrub].
and has had invalid characters removed via
Each returned string has encoding +enc+,
each subarray is a name/value pair (both are strings).
The returned data is an array of 2-element subarrays;
for which res['Content-Type'] is 'application/x-www-form-urlencoded'.
The method may be used to decode the body of Net::HTTPResponse object +res+
which must be an ASCII string.
Returns name/value pairs derived from the given string +str+,
def self.decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false) raise ArgumentError, "the input of #{self.name}.#{__method__} must be ASCII only string" unless str.ascii_only? ary = [] return ary if str.empty? enc = Encoding.find(enc) str.b.each_line(separator) do |string| string.chomp!(separator) key, sep, val = string.partition('=') if isindex if sep.empty? val = key key = +'' end isindex = false end if use__charset_ and key == '_charset_' and e = get_encoding(val) enc = e use__charset_ = false end key.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_) if val val.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_) else val = +'' end ary << [key, val] end ary.each do |k, v| k.force_encoding(enc) k.scrub! v.force_encoding(enc) v.scrub! end ary end
def self.decode_www_form_component(str, enc=Encoding::UTF_8)
# => "Here are some punctuation characters: ,;?:"
Bundler::URI.decode_www_form_component('Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A')
Example:
- Each "percent notation" to an ASCII character.
- Character '+' to character ' '.
- Converts:
# => "*.-_azAZ09"
Bundler::URI.decode_www_form_component('*.-_azAZ09')
Example:
and '0'..'9'.
- Character in ranges 'a'..'z', 'A'..'Z',
- Characters '*', '.', '-', and '_'.
- Preserves:
The returned string:
then decoded (as below), and finally force-encoded to the given encoding +enc+.
The given string is first encoded as Encoding::ASCII-8BIT (using String#b),
Returns a string decoded from the given \URL-encoded string +str+.
def self.decode_www_form_component(str, enc=Encoding::UTF_8) _decode_uri_component(/\+|%\h\h/, str, enc) end
def self.encode_uri_component(str, enc=nil)
Like Bundler::URI.encode_www_form_component, except that ' ' (space)
def self.encode_uri_component(str, enc=nil) _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCURICOMP_, str, enc) end
def self.encode_www_form(enum, enc=nil)
# => "foo=0&foo=1&bar=2"
Bundler::URI.encode_www_form({foo: [0, 1], bar: 2})
The elements of a Hash-like +enum+ may be mixture:
# => "foo=0&bar=1&baz=2"
Bundler::URI.encode_www_form({foo: 0, bar: 1, baz: 2})
Example:
"#{name}=#{value}"
value = Bundler::URI.encode_www_form_component(value, enc)
name = Bundler::URI.encode_www_form_component(key, enc)
- Otherwise, +key+ and +value+ are paired to form a field:
# => "foo=bar&foo=1&baz=bat&baz=bam&baz=2"
Bundler::URI.encode_www_form({foo: [:bar, 1], baz: [:bat, :bam, 2]})
Example:
"#{name}=#{value}"
value = Bundler::URI.encode_www_form_component(ele, enc)
name = Bundler::URI.encode_www_form_component(key, enc)
each element +ele+ in +value+ is paired with +key+ to form a field:
{Array-convertible}[https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html#label-Array-Convertible+Objects],
- If +value+ is
each +key+/+value+ pair is converted to one or more fields:
When +enum+ is Hash-like,
# => "foo=0&bar=1&baz&bat"
Bundler::URI.encode_www_form([['foo', 0], ['bar', 1, 2], ['baz'], :bat])
The elements of an Array-like +enum+ may be mixture:
# => "foo&bar&0"
Bundler::URI.encode_www_form(['foo', :bar, 0])
Example:
Bundler::URI.encode_www_form_component(ele)
- Otherwise the field is formed from +ele+:
# => "foo&bar&0"
Bundler::URI.encode_www_form([['foo'], [:bar], [0]])
Example:
Bundler::URI.encode_www_form_component(ele[0])
the field is formed from ele[0]:
- If +ele+ is an array of one element,
# => "foo=0&bar=baz"
Bundler::URI.encode_www_form([['foo', 0], ['bar', :baz, 'bat']])
# => "foo=bar&baz=bat"
Bundler::URI.encode_www_form([%w[foo bar], %w[baz bat bah]])
Examples:
"#{name}=#{value}"
value = Bundler::URI.encode_www_form_component(ele[1], enc)
name = Bundler::URI.encode_www_form_component(ele[0], enc)
(and any additional elements are ignored):
the field is formed from its first two elements
- If +ele+ is an array of two or more elements,
When +enum+ is Array-like, each element +ele+ is converted to a field:
# => "f%23o=%2F&b-r=%24&b+z=%40"
Bundler::URI.encode_www_form('f#o': '/', 'b-r': '$', 'b z': '@')
which converts certain characters:
The returned string is formed using method Bundler::URI.encode_www_form_component,
# => "foo=0&bar=1&baz=2"
Bundler::URI.encode_www_form({foo: 0, bar: 1, baz: 2})
# => "foo=0&bar=1&baz=2"
Bundler::URI.encode_www_form([['foo', 0], ['bar', 1], ['baz', 2]])
Simple examples:
and all joined with character '&'.
each converted to one or more URL-encoded strings,
The returned string consists of the elements of +enum+,
'application/x-www-form-urlencoded'.
for an \HTTP request whose Content-Type is
The result is suitable for use as form data
+enum+.
{Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
Returns a URL-encoded string derived from the given
def self.encode_www_form(enum, enc=nil) enum.map do |k,v| if v.nil? encode_www_form_component(k, enc) elsif v.respond_to?(:to_ary) v.to_ary.map do |w| str = encode_www_form_component(k, enc) unless w.nil? str << '=' str << encode_www_form_component(w, enc) end end.join('&') else str = encode_www_form_component(k, enc) str << '=' str << encode_www_form_component(v, enc) end end.join('&') end
def self.encode_www_form_component(str, enc=nil)
In either case, the returned string has forced encoding Encoding::US_ASCII.
and then to encoding +enc+.
(with suitable character replacements),
- Otherwise +str+ is converted first to Encoding::UTF_8
- If +str+ has encoding Encoding::ASCII_8BIT, argument +enc+ is ignored.
Encoding:
# => "Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A"
Bundler::URI.encode_www_form_component('Here are some punctuation characters: ,;?:')
Example:
the percent notation for character c is '%%%X' % c.ord.
- Any other character to "percent notation";
- Character ' ' to character '+'.
- Converts:
# => "*.-_azAZ09"
Bundler::URI.encode_www_form_component('*.-_azAZ09')
Example:
and '0'..'9'.
- Character in ranges 'a'..'z', 'A'..'Z',
- Characters '*', '.', '-', and '_'.
- Preserves:
The returned string:
Returns a URL-encoded string derived from the given string +str+.
def self.encode_www_form_component(str, enc=nil) _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_, str, enc) end
def self.extract(str, schemes = nil, &block) # :nodoc:
# => ["http://foo.example.com/bla", "mailto:test@example.com"]
Bundler::URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
require "bundler/vendor/uri/lib/uri"
== Usage
Returns nil if block given or array with matches.
Extracts URIs from a string. If block given, iterates through all matched URIs.
== Description
Limit Bundler::URI matching to specific schemes.
+schemes+::
String to extract URIs from.
+str+::
== Args
Bundler::URI::extract(str[, schemes][,&blk])
== Synopsis
def self.extract(str, schemes = nil, &block) # :nodoc: warn "Bundler::URI.extract is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.extract(str, schemes, &block) end
def self.for(scheme, *arguments, default: Generic)
# => #
Bundler::URI.for('foo', *values, default: Bundler::URI::HTTP)
# => #
Bundler::URI.for('https', *values)
values = ['john.doe', 'www.example.com', '123', nil, '/forum/questions/', nil, 'tag=networking&order=newest', 'top']
Examples:
See Bundler::URI::Generic.new.
using +scheme+ and +arguments+.
- The object is initialized by calling the class initializer
- The new object is an instance of Bundler::URI.scheme_list[scheme.upcase].
and +default+:
Returns a new object constructed from the given +scheme+, +arguments+,
def self.for(scheme, *arguments, default: Generic) const_name = scheme.to_s.upcase uri_class = INITIAL_SCHEMES[const_name] uri_class ||= if /\A[A-Z]\w*\z/.match?(const_name) && Schemes.const_defined?(const_name, false) Schemes.const_get(const_name, false) end uri_class ||= default return uri_class.new(scheme, *arguments) end
def self.get_encoding(label)
return encoding or nil
:nodoc:
def self.get_encoding(label) Encoding.find(WEB_ENCODINGS_[label.to_str.strip.downcase]) rescue nil end
def self.join(*str)
# => #
Bundler::URI.join('http://example.com', '/foo/', 'bar')
# => #
Bundler::URI.join('http://example.com', '/foo', 'bar')
# => #
Bundler::URI.join('http://example.com', '/foo', '/bar')
# => #
Bundler::URI.join('http://example.com', 'foo')
# => #
Bundler::URI.join("http://example.com/","main.rbx")
Examples:
{RFC3986 Bundler::URI}[https://www.rfc-editor.org/rfc/rfc3986.html] before being merged.
Each string in +str+ is converted to an
per {RFC 2396}[https://www.rfc-editor.org/rfc/rfc2396.html].
Merges the given Bundler::URI strings +str+
def self.join(*str) RFC3986_PARSER.join(*str) end
def self.parse(uri)
if it may contain invalid Bundler::URI characters.
It's recommended to first ::escape string +uri+
# => #
Bundler::URI.parse('http://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
# => #
Bundler::URI.parse('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
Returns a new \Bundler::URI object constructed from the given string +uri+:
def self.parse(uri) RFC3986_PARSER.parse(uri) end
def self.regexp(schemes = nil)# :nodoc:
end
p $&
html_string.scan(Bundler::URI.regexp) do |*matches|
# You should not rely on the number of parentheses
html_string.sub(Bundler::URI.regexp(['ftp']), '')
# remove ftp URIs
html_string.slice(Bundler::URI.regexp)
# extract first Bundler::URI from html_string
require 'bundler/vendor/uri/lib/uri'
== Usage
number of capture group (parentheses). Never rely on its number.
The Regexp object returned by this method includes arbitrary
Returns a Regexp object which matches to Bundler::URI-like strings.
== Description
whose scheme is one of the match_schemes.
Array of schemes. If given, resulting regexp matches to URIs
+match_schemes+::
== Args
Bundler::URI::regexp([match_schemes])
== Synopsis
def self.regexp(schemes = nil)# :nodoc: warn "Bundler::URI.regexp is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.make_regexp(schemes) end
def self.register_scheme(scheme, klass)
Note that after calling String#upcase on +scheme+, it must be a valid
Bundler::URI.scheme_list['MS_SEARCH'] # => Bundler::URI::Generic
Bundler::URI.register_scheme('MS_SEARCH', Bundler::URI::Generic) # => Bundler::URI::Generic
when parsing a \Bundler::URI with the given +scheme+:
Registers the given +klass+ as the class to be instantiated
def self.register_scheme(scheme, klass) Schemes.const_set(scheme.to_s.upcase, klass) end
def self.scheme_list
"FTP"=>Bundler::URI::FTP}
"FILE"=>Bundler::URI::File,
"LDAP"=>Bundler::URI::LDAP,
"HTTPS"=>Bundler::URI::HTTPS,
"HTTP"=>Bundler::URI::HTTP,
"WS"=>Bundler::URI::WS,
"LDAPS"=>Bundler::URI::LDAPS,
{"MAILTO"=>Bundler::URI::MailTo,
# =>
Bundler::URI.scheme_list
Returns a hash of the defined schemes:
def self.scheme_list Schemes.constants.map { |name| [name.to_s.upcase, Schemes.const_get(name)] }.to_h end
def self.split(uri)
["fragment", "top"]]
["query", "tag=networking&order=newest"],
["opaque", nil],
["path", "/forum/questions/"],
["registry", nil],
["port", "123"],
["host", "www.example.com"],
["userinfo", "john.doe"],
[["scheme", "https"],
# =>
names.zip(values)
values = Bundler::URI.split('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
names = %w[scheme userinfo host port registry path opaque query fragment]
each array element is a string or +nil+:
formed from the string +uri+;
Returns a 9-element array representing the parts of the \Bundler::URI
def self.split(uri) RFC3986_PARSER.split(uri) end