Class: Waitress::QueryParser
- Inherits:
-
Object
- Object
- Waitress::QueryParser
- Defined in:
- lib/waitress/parse/query.rb
Overview
A lot of this class uses methods from Rack::QueryParser in order to Parse a given QueryString into a Hash of key/value pairs
Constant Summary collapse
- DEFAULT_SEP =
/[&;] */n
Class Method Summary collapse
- .normalize(hash, name, v) ⇒ Object
-
.parse(qs) ⇒ Object
Parse the given QueryString into a hash of key/value pairs.
-
.unescape(str, enc = Encoding::UTF_8) ⇒ Object
Unescape a HTTP URL to regular text.
Class Method Details
.normalize(hash, name, v) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/waitress/parse/query.rb', line 26 def self.normalize hash, name, v name =~ %r(\A[\[\]]*([^\[\]]+)\]*) k = $1 || '' after = $' || '' return if k.empty? if after == "" hash[k] = v elsif after == "[" hash[name] = v elsif after == "[]" hash[k] ||= [] raise TypeError, "expected Array (got #{hash[k].class.name}) for param `#{k}'" unless hash[k].is_a?(Array) hash[k] << v elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$) child_key = $1 hash[k] ||= [] raise TypeError, "expected Array (got #{hash[k].class.name}) for param `#{k}'" unless hash[k].is_a?(Array) if hash[k].last.is_a?(Hash) && !hash[k].last.key?(child_key) normalize(hash[k].last, child_key, v) else hash[k] << normalize({}, child_key, v) end else hash[k] ||= {} raise TypeError, "expected Hash (got #{hash[k].class.name}) for param `#{k}'" unless hash[k].is_a?(Hash) hash[k] = normalize(hash[k], after, v) end hash end |
.parse(qs) ⇒ Object
Parse the given QueryString into a hash of key/value pairs
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/waitress/parse/query.rb', line 15 def self.parse qs return {} if qs.nil? || qs.empty? results = {} (qs || '').split(DEFAULT_SEP).each do |p| k, v = p.split('='.freeze, 2).map! { |s| unescape(s) } normalize results, k, v end results end |
.unescape(str, enc = Encoding::UTF_8) ⇒ Object
Unescape a HTTP URL to regular text. e.g. %20 becomes a space (“ ”)
10 11 12 |
# File 'lib/waitress/parse/query.rb', line 10 def self.unescape str, enc=Encoding::UTF_8 URI.decode_www_form_component(str, enc) end |