2010-01-16 07:17:03 -05:00
|
|
|
module ActionDispatch
|
|
|
|
module Http
|
|
|
|
module Parameters
|
2016-02-22 15:20:37 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
PARAMETERS_KEY = "action_dispatch.request.path_parameters"
|
2014-05-27 17:46:04 -04:00
|
|
|
|
2015-09-18 14:46:51 -04:00
|
|
|
DEFAULT_PARSERS = {
|
2016-02-20 04:33:05 -05:00
|
|
|
Mime[:json].symbol => -> (raw_post) {
|
2015-09-18 14:46:51 -04:00
|
|
|
data = ActiveSupport::JSON.decode(raw_post)
|
2016-08-16 03:30:11 -04:00
|
|
|
data.is_a?(Hash) ? data : { _json: data }
|
2015-09-18 14:46:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 21:09:41 -04:00
|
|
|
# Raised when raw data from the request cannot be parsed by the parser
|
|
|
|
# defined for request's content mime type.
|
|
|
|
class ParseError < StandardError
|
|
|
|
def initialize
|
|
|
|
super($!.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-22 15:20:37 -05:00
|
|
|
included do
|
|
|
|
class << self
|
|
|
|
attr_reader :parameter_parsers
|
2015-09-18 14:46:51 -04:00
|
|
|
end
|
|
|
|
|
2016-02-22 15:20:37 -05:00
|
|
|
self.parameter_parsers = DEFAULT_PARSERS
|
2015-09-18 14:46:51 -04:00
|
|
|
end
|
2016-02-22 15:20:37 -05:00
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def parameter_parsers=(parsers) # :nodoc:
|
|
|
|
@parameter_parsers = parsers.transform_keys { |key| key.respond_to?(:symbol) ? key.symbol : key }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
# Returns both GET and POST \parameters in a single hash.
|
|
|
|
def parameters
|
2015-08-22 18:40:43 -04:00
|
|
|
params = get_header("action_dispatch.request.parameters")
|
|
|
|
return params if params
|
|
|
|
|
|
|
|
params = begin
|
|
|
|
request_parameters.merge(query_parameters)
|
|
|
|
rescue EOFError
|
|
|
|
query_parameters.dup
|
|
|
|
end
|
|
|
|
params.merge!(path_parameters)
|
2016-12-21 14:21:01 -05:00
|
|
|
params = set_binary_encoding(params)
|
2015-08-22 18:40:43 -04:00
|
|
|
set_header("action_dispatch.request.parameters", params)
|
|
|
|
params
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
alias :params :parameters
|
|
|
|
|
|
|
|
def path_parameters=(parameters) #:nodoc:
|
2016-08-06 12:51:43 -04:00
|
|
|
delete_header("action_dispatch.request.parameters")
|
2016-07-13 13:44:04 -04:00
|
|
|
|
|
|
|
# If any of the path parameters has an invalid encoding then
|
|
|
|
# raise since it's likely to trigger errors further on.
|
|
|
|
Request::Utils.check_param_encoding(parameters)
|
|
|
|
|
2015-08-22 18:40:43 -04:00
|
|
|
set_header PARAMETERS_KEY, parameters
|
2016-07-13 13:44:04 -04:00
|
|
|
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
|
|
|
|
raise ActionController::BadRequest.new("Invalid path parameters: #{e.message}")
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a hash with the \parameters used to form the \path of the request.
|
|
|
|
# Returned hash keys are strings:
|
|
|
|
#
|
|
|
|
# {'action' => 'my_action', 'controller' => 'my_controller'}
|
|
|
|
def path_parameters
|
2015-12-21 11:20:51 -05:00
|
|
|
get_header(PARAMETERS_KEY) || set_header(PARAMETERS_KEY, {})
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
2015-09-18 14:46:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-21 14:21:01 -05:00
|
|
|
def set_binary_encoding(params)
|
2016-08-12 18:48:17 -04:00
|
|
|
action = params[:action]
|
2016-12-21 14:21:01 -05:00
|
|
|
if controller_class.binary_params_for?(action)
|
|
|
|
ActionDispatch::Request::Utils.each_param_value(params) do |param|
|
|
|
|
param.force_encoding ::Encoding::ASCII_8BIT
|
2016-08-12 18:48:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def parse_formatted_parameters(parsers)
|
|
|
|
return yield if content_length.zero? || content_mime_type.nil?
|
2015-09-18 14:46:51 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
strategy = parsers.fetch(content_mime_type.symbol) { return yield }
|
2015-09-18 14:46:51 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
begin
|
|
|
|
strategy.call(raw_post)
|
|
|
|
rescue # JSON or Ruby code block errors
|
|
|
|
my_logger = logger || ActiveSupport::Logger.new($stderr)
|
|
|
|
my_logger.debug "Error occurred while parsing request parameters.\nContents:\n\n#{raw_post}"
|
2015-09-18 14:46:51 -04:00
|
|
|
|
2016-06-19 21:09:41 -04:00
|
|
|
raise ParseError
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2015-09-18 14:46:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def params_parsers
|
|
|
|
ActionDispatch::Request.parameter_parsers
|
|
|
|
end
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
end
|
2016-06-19 21:09:41 -04:00
|
|
|
|
|
|
|
module ParamsParser
|
|
|
|
ParseError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("ActionDispatch::ParamsParser::ParseError", "ActionDispatch::Http::Parameters::ParseError")
|
|
|
|
end
|
2010-04-21 00:58:58 -04:00
|
|
|
end
|