Skip to content

Instantly share code, notes, and snippets.

@jalada
Created February 21, 2012 15:47
Show Gist options
  • Save jalada/1877098 to your computer and use it in GitHub Desktop.
Save jalada/1877098 to your computer and use it in GitHub Desktop.
Silently ignore bad cookies
# 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