Module: Puma::Util

Defined in:
lib/puma/util.rb

Defined Under Namespace

Classes: HeaderHash

Constant Summary collapse

DEFAULT_SEP =
/[&;] */n

Class Method Summary collapse

Class Method Details

.parse_query(qs, d = nil, &unescaper) ⇒ Object

Stolen from Mongrel, with some small modifications: Parses a query string by breaking it up at the '&' and ';' characters. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to '&;').


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puma/util.rb', line 33

def parse_query(qs, d = nil, &unescaper)
  unescaper ||= method(:unescape)

  params = {}

  (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
    next if p.empty?
    k, v = p.split('=', 2).map(&unescaper)

    if cur = params[k]
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  end

  return params
end

.pipeObject


9
10
11
# File 'lib/puma/util.rb', line 9

def pipe
  IO.pipe
end

.unescape(s, encoding = nil) ⇒ Object


16
17
18
# File 'lib/puma/util.rb', line 16

def unescape(s, encoding = Encoding::UTF_8)
  URI.decode_www_form_component(s, encoding)
end