Created
February 21, 2012 15:47
-
-
Save jalada/1877098 to your computer and use it in GitHub Desktop.
Silently ignore bad cookies
This file contains hidden or 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 cookies method to not raise an error on a | |
# malformed cookie. This is because the utag cookie set by The Times | |
# can contain malformed unicode characters. | |
alert = "WARNING: You are monkey-patching Rack::Utils.parse_query to dump broken cookies. BE CAREFUL!" | |
puts alert | |
puts "*" * alert.length | |
module Rack | |
module Utils | |
def parse_query(qs, d = nil) | |
params = {} | |
max_key_space = Utils.key_space_limit | |
bytes = 0 | |
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p| | |
begin | |
k, v = p.split('=', 2).map { |x| unescape(x) } | |
rescue ArgumentError => e | |
# Ignore invalid (key,value) pairs | |
next if /^invalid %-encoding \(.*\)$/.match e.message | |
# Reraise other errors | |
raise | |
end | |
if k | |
bytes += k.size | |
if bytes > max_key_space | |
raise RangeError, "exceeded available parameter key space" | |
end | |
end | |
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 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment