Skip to content

Instantly share code, notes, and snippets.

@loren
Created August 17, 2012 17:37
Show Gist options
  • 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
@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