2009-12-22 18:11:21 -05:00
|
|
|
require 'action_dispatch/http/request'
|
2009-04-22 19:48:58 -04:00
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2015-08-15 14:41:19 -04:00
|
|
|
# ActionDispatch::ParamsParser works for all the requests having any Content-Length
|
|
|
|
# (like POST). It takes raw data from the request and puts it through the parser
|
|
|
|
# that is picked based on Content-Type header.
|
|
|
|
#
|
|
|
|
# In case of any error while parsing data ParamsParser::ParseError is raised.
|
2009-01-17 21:29:50 -05:00
|
|
|
class ParamsParser
|
2015-08-15 14:41:19 -04:00
|
|
|
# Raised when raw data from the request cannot be parsed by the parser
|
|
|
|
# defined for request's content mime type.
|
2012-08-27 17:46:53 -04:00
|
|
|
class ParseError < StandardError
|
|
|
|
attr_reader :original_exception
|
|
|
|
|
|
|
|
def initialize(message, original_exception)
|
|
|
|
super(message)
|
|
|
|
@original_exception = original_exception
|
|
|
|
end
|
|
|
|
end
|
2012-08-24 12:08:06 -04:00
|
|
|
|
2015-07-09 17:56:26 -04:00
|
|
|
DEFAULT_PARSERS = {
|
|
|
|
Mime::JSON => lambda { |raw_post|
|
|
|
|
data = ActiveSupport::JSON.decode(raw_post)
|
|
|
|
data = {:_json => data} unless data.is_a?(Hash)
|
2015-07-21 21:14:18 -04:00
|
|
|
Request::Utils.normalize_encode_params(data)
|
2015-07-09 17:56:26 -04:00
|
|
|
}
|
|
|
|
}
|
2009-01-17 21:29:50 -05:00
|
|
|
|
2015-08-15 14:41:19 -04:00
|
|
|
# Create a new +ParamsParser+ middleware instance.
|
|
|
|
#
|
|
|
|
# The +parsers+ argument can take Hash of parsers where key is identifying
|
|
|
|
# content mime type, and value is a lambda that is going to process data.
|
2009-08-21 17:49:33 -04:00
|
|
|
def initialize(app, parsers = {})
|
|
|
|
@app, @parsers = app, DEFAULT_PARSERS.merge(parsers)
|
2009-01-17 21:29:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2015-08-07 10:55:09 -04:00
|
|
|
request = Request.new(env)
|
|
|
|
|
|
|
|
request.request_parameters = parse_formatted_parameters(request, @parsers)
|
2009-01-17 21:29:50 -05:00
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-08-07 10:55:09 -04:00
|
|
|
def parse_formatted_parameters(request, parsers)
|
|
|
|
return if request.content_length.zero?
|
2009-01-17 21:29:50 -05:00
|
|
|
|
2015-08-07 10:55:09 -04:00
|
|
|
strategy = parsers.fetch(request.content_mime_type) { return nil }
|
2009-01-17 21:29:50 -05:00
|
|
|
|
2015-07-09 17:56:26 -04:00
|
|
|
strategy.call(request.raw_post)
|
2009-01-17 21:29:50 -05:00
|
|
|
|
2014-11-23 16:04:31 -05:00
|
|
|
rescue => e # JSON or Ruby code block errors
|
2015-08-07 10:55:09 -04:00
|
|
|
logger(request).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
|
2009-08-11 00:56:46 -04:00
|
|
|
|
2012-08-27 17:46:53 -04:00
|
|
|
raise ParseError.new(e.message, e)
|
2009-01-17 21:29:50 -05:00
|
|
|
end
|
|
|
|
|
2015-08-07 10:55:09 -04:00
|
|
|
def logger(request)
|
|
|
|
request.logger || ActiveSupport::Logger.new($stderr)
|
2009-08-11 00:56:46 -04:00
|
|
|
end
|
2009-01-17 21:29:50 -05:00
|
|
|
end
|
2010-04-07 14:42:07 -04:00
|
|
|
end
|