Module: URI

Defined in:
lib/rack/backports/uri/common_18.rb,
lib/rack/backports/uri/common_192.rb,
lib/rack/backports/uri/common_193.rb

Overview

Constant Summary collapse

TBLENCWWWCOMP_ =

:nodoc:

{}
TBLDECWWWCOMP_ =

:nodoc:

{}
WFKV_ =

:nodoc:

'(?:[^%#=;&]*(?:%\h\h[^%#=;&]*)*)'

Class Method Summary collapse

Class Method Details

.decode_www_form(str, enc = Encoding::UTF_8) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/backports/uri/common_192.rb', line 33

def self.decode_www_form(str, enc=Encoding::UTF_8)
  return [] if str.empty?
  unless /\A#{WFKV_}=#{WFKV_}(?:[;&]#{WFKV_}=#{WFKV_})*\z/o =~ str
    raise ArgumentError, "invalid data of application/x-www-form-urlencoded (#{str})"
  end
  ary = []
  $&.scan(/([^=;&]+)=([^;&]*)/) do
    ary << [decode_www_form_component($1, enc), decode_www_form_component($2, enc)]
  end
  ary
end

.decode_www_form_component(str, enc = Encoding::UTF_8) ⇒ Object

Decode given str of URL-encoded form data.

This decodes + to SP.

See URI.encode_www_form_component, URI.decode_www_form

Raises:

  • (ArgumentError)


52
53
54
55
# File 'lib/rack/backports/uri/common_18.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_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/rack/backports/uri/common_18.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