lib/rack/query_parser.rb in rack-3.0.6.1 vs lib/rack/query_parser.rb in rack-3.0.7
- old
+ new
@@ -37,46 +37,23 @@
@params_class = params_class
@param_depth_limit = param_depth_limit
end
- # Originally stolen from Mongrel, now with some modifications:
+ # 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 '&').
- #
- # Returns an array of 2-element arrays, where the first element is the
- # key and the second element is the value.
- def split_query(qs, separator = nil, &unescaper)
- pairs = []
-
- if qs && !qs.empty?
- unescaper ||= method(:unescape)
-
- qs.split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p|
- next if p.empty?
- pair = p.split('=', 2).map!(&unescaper)
- pair << nil if pair.length == 1
- pairs << pair
- end
- end
-
- pairs
- rescue ArgumentError => e
- raise InvalidParameterError, e.message, e.backtrace
- end
-
- # 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 '&').
- #
- # Returns a hash where each value is a string (when a key only appears once)
- # or an array of strings (when a key appears more than once).
def parse_query(qs, separator = nil, &unescaper)
+ unescaper ||= method(:unescape)
+
params = make_params
- split_query(qs, separator, &unescaper).each do |k, v|
+ (qs || '').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]
@@ -84,26 +61,32 @@
else
params[k] = v
end
end
- params.to_h
+ return params.to_h
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.
def parse_nested_query(qs, separator = nil)
params = make_params
- split_query(qs, separator).each do |k, v|
- _normalize_params(params, k, v, 0)
+ unless qs.nil? || qs.empty?
+ (qs || '').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
- params.to_h
+ return params.to_h
+ rescue ArgumentError => e
+ raise InvalidParameterError, e.message, e.backtrace
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
@@ -111,18 +94,10 @@
# earlier versions of rack.
def normalize_params(params, name, v, _depth=nil)
_normalize_params(params, name, v, 0)
end
- # This value is used by default when a parameter is missing (nil). This
- # usually happens when a parameter is specified without an `=value` part.
- # The default value is an empty string, but this can be overridden by
- # subclasses.
- def missing_value
- String.new
- end
-
private def _normalize_params(params, name, v, depth)
raise ParamsTooDeepError if depth >= param_depth_limit
if !name
# nil name, treat same as empty string (required by tests)
@@ -152,11 +127,9 @@
k = name
after = ''
end
return if k.empty?
-
- v ||= missing_value
if after == ''
if k == '[]' && depth != 0
return [v]
else