1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

refactor param parsing middleware to use request objects

this is another place that we should stop directly accessing the env
hash and let the request object take care of that for us
This commit is contained in:
Aaron Patterson 2015-08-07 07:55:09 -07:00
parent 85969c74cd
commit 9b7ecf0d6d
2 changed files with 17 additions and 10 deletions

View file

@ -339,6 +339,14 @@ module ActionDispatch
LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip
end
def request_parameters=(params)
env["action_dispatch.request.request_parameters".freeze] = params
end
def logger
env["action_dispatch.logger".freeze]
end
private
def check_method(name)
HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS[0...-1].join(', ')}, and #{HTTP_METHODS[-1]}")

View file

@ -26,30 +26,29 @@ module ActionDispatch
end
def call(env)
default = env["action_dispatch.request.request_parameters"]
env["action_dispatch.request.request_parameters"] = parse_formatted_parameters(env, @parsers, default)
request = Request.new(env)
request.request_parameters = parse_formatted_parameters(request, @parsers)
@app.call(env)
end
private
def parse_formatted_parameters(env, parsers, default)
request = Request.new(env)
def parse_formatted_parameters(request, parsers)
return if request.content_length.zero?
return default if request.content_length.zero?
strategy = parsers.fetch(request.content_mime_type) { return default }
strategy = parsers.fetch(request.content_mime_type) { return nil }
strategy.call(request.raw_post)
rescue => e # JSON or Ruby code block errors
logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
logger(request).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
raise ParseError.new(e.message, e)
end
def logger(env)
env['action_dispatch.logger'] || ActiveSupport::Logger.new($stderr)
def logger(request)
request.logger || ActiveSupport::Logger.new($stderr)
end
end
end