Last active
August 29, 2015 14:10
-
-
Save floehopper/7e347a1fe5e709b84127 to your computer and use it in GitHub Desktop.
Patches for Rails and Rack for ArgumentError: invalid byte sequence
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
# Based on these Rack commits [1,2] which originated from this Rack PR [3] and | |
# mentioned in this Rails PR [4]. | |
# | |
# [1]: https://github.com/rack/rack/commit/cc8279f4a158e51975838e6202581c3d5e33f1c4 | |
# [2]: https://github.com/rack/rack/commit/975ccac7e56dcd765cb102016b97ef13d15feba8 | |
# [3]: https://github.com/rack/rack/pull/713 | |
# [4]: https://github.com/rails/rails/pull/11795 | |
require 'rack/utils' | |
module Rack | |
module Utils | |
# InvalidParameterError is the error that is raised when incoming structural | |
# parameters (parsed by parse_nested_query) contain invalid format or byte | |
# sequence. | |
class InvalidParameterError < ArgumentError; end | |
def parse_nested_query(qs, d = nil) | |
params = KeySpaceConstrainedParams.new | |
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p| | |
k, v = p.split('=', 2).map { |s| unescape(s) } | |
normalize_params(params, k, v) | |
end | |
return params.to_params_hash | |
rescue ArgumentError => e | |
raise InvalidParameterError, e.message | |
end | |
module_function :parse_nested_query | |
end | |
end | |
# Based on this Rails commmit [1] referenced in this Rails PR [2] | |
# | |
# [1]: https://github.com/rails/rails/commit/d59a24d543b4fd34d453e8209caae5fef315ea78 | |
# [2]: https://github.com/rails/rails/pull/11795 | |
require 'action_dispatch/http/request' | |
module ActionDispatch | |
class Request < Rack::Request | |
# Override Rack's GET method to support indifferent access | |
def GET | |
@env["action_dispatch.request.query_parameters"] ||= Utils.deep_munge((normalize_encode_params(super) || {})) | |
rescue TypeError, Rack::Utils::InvalidParameterError => e | |
raise ActionController::BadRequest.new(:query, e) | |
end | |
alias :query_parameters :GET | |
# Override Rack's POST method to support indifferent access | |
def POST | |
@env["action_dispatch.request.request_parameters"] ||= Utils.deep_munge((normalize_encode_params(super) || {})) | |
rescue TypeError, Rack::Utils::InvalidParameterError => e | |
raise ActionController::BadRequest.new(:request, e) | |
end | |
alias :request_parameters :POST | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment