Created
June 15, 2010 09:50
-
-
Save brianjriddle/438922 to your computer and use it in GitHub Desktop.
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
# | |
#monkey patch fix from rack 1.20 to remove escaping of " and ' | |
#from parameters. | |
# | |
#following from rack 1.20 is removed i the funciotns in this | |
#file | |
# | |
#if v =~ /^("|')(.*)\1$/ | |
# v = $2.gsub('\\'+$1, $1) | |
#end | |
# see http://github.com/bahuvrihi/rack/commit/dae12e088592ee69545b5f2f81b87f4959859164 | |
module Rack | |
module Utils | |
# Stolen from Mongrel, with some small modifications: | |
# Parses a query string by breaking it up at the '&' | |
# and ';' characters. You can also use this to parse | |
# cookies by changing the characters used in the second | |
# parameter (which defaults to '&;'). | |
def parse_query(qs, d = nil) | |
params = {} | |
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p| | |
k, v = p.split('=', 2).map { |x| unescape(x) } | |
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 | |
end | |
module_function :parse_query | |
def normalize_params(params, name, v = nil) | |
name =~ %r(\A[\[\]]*([^\[\]]+)\]*) | |
k = $1 || '' | |
after = $' || '' | |
return if k.empty? | |
if after == "" | |
params[k] = v | |
elsif after == "[]" | |
params[k] ||= [] | |
raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) | |
params[k] << v | |
elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$) | |
child_key = $1 | |
params[k] ||= [] | |
raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) | |
if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key) | |
normalize_params(params[k].last, child_key, v) | |
else | |
params[k] << normalize_params({}, child_key, v) | |
end | |
else | |
params[k] ||= {} | |
raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash) | |
params[k] = normalize_params(params[k], after, v) | |
end | |
return params | |
end | |
module_function :normalize_params | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment