Skip to content

Instantly share code, notes, and snippets.

@loren
Created August 17, 2012 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loren/3380888 to your computer and use it in GitHub Desktop.
Save loren/3380888 to your computer and use it in GitHub Desktop.
Fix for ArgumentError: invalid byte sequence in UTF-8
require 'action_dispatch/routing/route_set'
# Based on https://gist.github.com/2830082
module ActionDispatch
module Routing
class RouteSet
class Dispatcher
def call_with_invalid_char_handling(env)
uri = CGI::unescape(env["REQUEST_URI"].force_encoding("UTF-8"))
# If anything in the REQUEST_URI has an invalid encoding, then raise since it's likely to trigger errors further on.
return [400, {'X-Cascade' => 'pass'}, []] if uri.is_a?(String) and !uri.valid_encoding?
call_without_invalid_char_handling(env)
end
alias_method_chain :call, :invalid_char_handling
end
end
end
end
@radixhound
Copy link

tried this with rails 3.2.11 and when it gets to line 8 there is no REQUEST_URI param.

@lulalala
Copy link

@radixhound That would be weird. I am using 3.2.11 but REQUEST_URI is there. Or you can try "ORIGINAL_FULLPATH" instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment