Module: Noder::HTTP::Utils

Defined in:
lib/noder/http/utils.rb

Defined Under Namespace

Classes: KeySpaceConstrainedParams

Constant Summary collapse

DEFAULT_SEP =
/[&;] */n

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.key_space_limitObject

Returns the value of attribute key_space_limit.



9
10
11
# File 'lib/noder/http/utils.rb', line 9

def key_space_limit
  @key_space_limit
end

Class Method Details

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/noder/http/utils.rb', line 62

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

  params = KeySpaceConstrainedParams.new

  (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.to_params_hash
end

.parse_headers(string) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/noder/http/utils.rb', line 86

def parse_headers(string)
  string.split("\x00").reduce({}) do |hash, string|
    key, value = string.split(': ', 2)
    hash[key] = value
    hash
  end
end

.unescape(s, encoding = nil) ⇒ Object



52
53
54
# File 'lib/noder/http/utils.rb', line 52

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