Class: Rack::QueryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/query_parser.rb

Defined Under Namespace

Classes: InvalidParameterError, ParameterTypeError, Params, QueryLimitError

Constant Summary collapse

DEFAULT_SEP =
/& */n
COMMON_SEP =
{ ";" => /; */n, ";," => /[;,] */n, "&" => /& */n }
ParamsTooDeepError =

ParamsTooDeepError is the old name for the error that is raised when params are recursively nested over the specified limit. Make it the same as as QueryLimitError, so that code that rescues ParamsTooDeepError error to handle bad query strings also now handles other limits.

QueryLimitError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params_class, param_depth_limit, bytesize_limit: BYTESIZE_LIMIT, params_limit: PARAMS_LIMIT) ⇒ QueryParser

Returns a new instance of QueryParser.



62
63
64
65
66
67
# File 'lib/rack/query_parser.rb', line 62

def initialize(params_class, param_depth_limit, bytesize_limit: BYTESIZE_LIMIT, params_limit: PARAMS_LIMIT)
  @params_class = params_class
  @param_depth_limit = param_depth_limit
  @bytesize_limit = bytesize_limit
  @params_limit = params_limit
end

Instance Attribute Details

#bytesize_limitObject (readonly)

Returns the value of attribute bytesize_limit.



60
61
62
# File 'lib/rack/query_parser.rb', line 60

def bytesize_limit
  @bytesize_limit
end

#param_depth_limitObject (readonly)

Returns the value of attribute param_depth_limit.



40
41
42
# File 'lib/rack/query_parser.rb', line 40

def param_depth_limit
  @param_depth_limit
end

Class Method Details

.make_default(param_depth_limit, **options) ⇒ Object



36
37
38
# File 'lib/rack/query_parser.rb', line 36

def self.make_default(param_depth_limit, **options)
  new(Params, param_depth_limit, **options)
end

Instance Method Details

#make_paramsObject



196
197
198
# File 'lib/rack/query_parser.rb', line 196

def make_params
  @params_class.new
end

#new_depth_limit(param_depth_limit) ⇒ Object



200
201
202
# File 'lib/rack/query_parser.rb', line 200

def new_depth_limit(param_depth_limit)
  self.class.new @params_class, param_depth_limit
end

#normalize_params(params, name, v, _depth = nil) ⇒ Object

normalize_params recursively expands parameters into structural types. If the structural types represented by two different parameter names are in conflict, a ParameterTypeError is raised. The depth argument is deprecated and should no longer be used, it is kept for backwards compatibility with earlier versions of rack.



124
125
126
# File 'lib/rack/query_parser.rb', line 124

def normalize_params(params, name, v, _depth=nil)
  _normalize_params(params, name, v, 0)
end

#parse_nested_query(qs, separator = nil) ⇒ Object

parse_nested_query expands a query string into structural types. Supported types are Arrays, Hashes and basic value types. It is possible to supply query strings with parameters of conflicting types, in this case a ParameterTypeError is raised. Users are encouraged to return a 400 in this case.



109
110
111
112
113
114
115
116
117
# File 'lib/rack/query_parser.rb', line 109

def parse_nested_query(qs, separator = nil)
  params = make_params

  each_query_pair(qs, separator) do |k, v|
    _normalize_params(params, k, v, 0)
  end

  return params.to_h
end

#parse_query(qs, separator = nil, &unescaper) ⇒ Object

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rack/query_parser.rb', line 73

def parse_query(qs, separator = nil, &unescaper)
  params = make_params

  each_query_pair(qs, separator, unescaper) do |k, v|
    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_h
end

#parse_query_pairs(qs, separator = nil) ⇒ Object

Parses a query string by breaking it up at the ‘&’, returning all key-value pairs as an array of [key, value] arrays. Unlike parse_query, this preserves all duplicate keys rather than collapsing them.



94
95
96
97
98
99
100
101
102
# File 'lib/rack/query_parser.rb', line 94

def parse_query_pairs(qs, separator = nil)
  pairs = []

  each_query_pair(qs, separator) do |k, v|
    pairs << [k, v]
  end

  pairs
end