class Rack::QueryParser
Constants
- COMMON_SEP
- DEFAULT_SEP
- ParamsTooDeepError
-
ParamsTooDeepErroris the old name for the error that is raised when params are recursively nested over the specified limit. Make it the same as asQueryLimitError, so that code that rescuesParamsTooDeepErrorerror to handle bad query strings also now handles other limits.
Attributes
Public Class Methods
# File lib/rack/query_parser.rb, line 30 def self.make_default(_key_space_limit=(not_deprecated = true; nil), param_depth_limit, **options) unless not_deprecated warn("`first argument `key_space limit` is deprecated and no longer has an effect. Please call with only one argument, which will be required in a future version of Rack", uplevel: 1) end new Params, param_depth_limit, **options end
# File lib/rack/query_parser.rb, line 58 def initialize(params_class, _key_space_limit=(not_deprecated = true; nil), param_depth_limit, bytesize_limit: BYTESIZE_LIMIT, params_limit: PARAMS_LIMIT) unless not_deprecated warn("`second argument `key_space limit` is deprecated and no longer has an effect. Please call with only two arguments, which will be required in a future version of Rack", uplevel: 1) end @params_class = params_class @param_depth_limit = param_depth_limit @bytesize_limit = bytesize_limit @params_limit = params_limit end
Public Instance Methods
Source
# File lib/rack/query_parser.rb, line 194 def make_params @params_class.new end
# File lib/rack/query_parser.rb, line 198 def new_depth_limit(param_depth_limit) self.class.new @params_class, param_depth_limit end
# File lib/rack/query_parser.rb, line 122 def normalize_params(params, name, v, _depth=nil) _normalize_params(params, name, v, 0) end
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.
# File lib/rack/query_parser.rb, line 101 def parse_nested_query(qs, separator = nil) params = make_params unless qs.nil? || qs.empty? check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p| k, v = p.split('=', 2).map! { |s| unescape(s) } _normalize_params(params, k, v, 0) end end return params.to_h rescue ArgumentError => e raise InvalidParameterError, e.message, e.backtrace end
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.
# File lib/rack/query_parser.rb, line 73 def parse_query(qs, separator = nil, &unescaper) unescaper ||= method(:unescape) params = make_params check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */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_h end
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 ‘&’).