Module: URI
- Defined in:
- lib/raven/backports/uri.rb
Overview
Stolen from ruby core’s uri/common.rb, with modifications to support 1.8.x
Constant Summary collapse
- TBLENCWWWCOMP_ =
:nodoc:
{}
- TBLDECWWWCOMP_ =
:nodoc:
{}
Class Method Summary collapse
-
.decode_www_form_component(str, enc = nil) ⇒ Object
Decode given
str
of URL-encoded form data. -
.encode_www_form(enum) ⇒ Object
Generate URL-encoded form data from given
enum
. -
.encode_www_form_component(s) ⇒ Object
Encode given
s
to URL-encoded form data.
Class Method Details
.decode_www_form_component(str, enc = nil) ⇒ Object
Decode given str
of URL-encoded form data.
This decodes + to SP.
See URI.encode_www_form_component, URI.decode_www_form
52 53 54 55 |
# File 'lib/raven/backports/uri.rb', line 52 def self.decode_www_form_component(str, enc=nil) raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ =~ str str.gsub(/\+|%[0-9a-fA-F]{2}/) {|m| TBLDECWWWCOMP_[m]} end |
.encode_www_form(enum) ⇒ Object
Generate URL-encoded form data from given enum
.
This generates application/x-www-form-urlencoded data defined in HTML5 from given an Enumerable object.
This internally uses URI.encode_www_form_component(str).
This method doesn’t convert the encoding of given items, so convert them before call this method if you want to send data as other than original encoding or mixed encoding data. (Strings which are encoded in an HTML5 ASCII incompatible encoding are converted to UTF-8.)
This method doesn’t handle files. When you send a file, use multipart/form-data.
This is an implementation of www.w3.org/TR/html5/forms.html#url-encoded-form-data
URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => "ruby", "lang" => "en")
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
#=> "q=ruby&q=perl&lang=en"
URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
#=> "q=ruby&q=perl&lang=en"
See URI.encode_www_form_component, URI.decode_www_form
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/raven/backports/uri.rb', line 85 def self.encode_www_form(enum) enum.map do |k,v| if v.nil? encode_www_form_component(k) elsif v.respond_to?(:to_ary) v.to_ary.map do |w| str = encode_www_form_component(k) unless w.nil? str << '=' str << encode_www_form_component(w) end end.join('&') else str = encode_www_form_component(k) str << '=' str << encode_www_form_component(v) end end.join('&') end |
.encode_www_form_component(s) ⇒ Object
Encode given s
to URL-encoded form data.
This method doesn’t convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP (ASCII space) to + and converts others to %XX.
This is an implementation of www.w3.org/TR/html5/forms.html#url-encoded-form-data
See URI.decode_www_form_component, URI.encode_www_form
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/raven/backports/uri.rb', line 36 def self.encode_www_form_component(s) str = s.to_s if RUBY_VERSION < "1.9" && $KCODE =~ /u/i str.gsub(/([^ a-zA-Z0-9_.-]+)/) do '%' + $1.unpack('H2' * Rack::Utils.bytesize($1)).join('%').upcase end.tr(' ', '+') else str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]} end end |