Created
June 8, 2012 15:36
-
-
Save lucasallan/2896202 to your computer and use it in GitHub Desktop.
Fixing problem when parameter's key is nil in Rack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# => This is a monkey patch to fix the problem with nil key params in rack 1.4.1 | |
# => It already be solved by Rack team but it will be release in rack 1.4.2 | |
# => https://github.com/rack/rack/pull/329 | |
# | |
module Rack::Utils | |
def self.parse_query(qs, d = nil) | |
params = KeySpaceConstrainedParams.new | |
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p| | |
k, v = p.split('=', 2).map { |x| unescape(x) } | |
next unless 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_params_hash | |
end | |
class KeySpaceConstrainedParams | |
def []=(key, value) | |
@size += key.size if key && !@params.key?(key) | |
raise RangeError, 'exceeded available parameter key space' if @size > @limit | |
@params[key] = value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment