Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / params_parser.rb
1 module ActionController
2 class ParamsParser
3 ActionController::Base.param_parsers[Mime::XML] = :xml_simple
4 ActionController::Base.param_parsers[Mime::JSON] = :json
5
6 def initialize(app)
7 @app = app
8 end
9
10 def call(env)
11 if params = parse_formatted_parameters(env)
12 env["action_controller.request.request_parameters"] = params
13 end
14
15 @app.call(env)
16 end
17
18 private
19 def parse_formatted_parameters(env)
20 request = Request.new(env)
21
22 return false if request.content_length.zero?
23
24 mime_type = content_type_from_legacy_post_data_format_header(env) || request.content_type
25 strategy = ActionController::Base.param_parsers[mime_type]
26
27 return false unless strategy
28
29 case strategy
30 when Proc
31 strategy.call(request.raw_post)
32 when :xml_simple, :xml_node
33 body = request.raw_post
34 body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
35 when :yaml
36 YAML.load(request.raw_post)
37 when :json
38 body = request.raw_post
39 if body.blank?
40 {}
41 else
42 data = ActiveSupport::JSON.decode(body)
43 data = {:_json => data} unless data.is_a?(Hash)
44 data.with_indifferent_access
45 end
46 else
47 false
48 end
49 rescue Exception => e # YAML, XML or Ruby code block errors
50 raise
51 { "body" => request.raw_post,
52 "content_type" => request.content_type,
53 "content_length" => request.content_length,
54 "exception" => "#{e.message} (#{e.class})",
55 "backtrace" => e.backtrace }
56 end
57
58 def content_type_from_legacy_post_data_format_header(env)
59 if x_post_format = env['HTTP_X_POST_DATA_FORMAT']
60 case x_post_format.to_s.downcase
61 when 'yaml'
62 return Mime::YAML
63 when 'xml'
64 return Mime::XML
65 end
66 end
67
68 nil
69 end
70 end
71 end