diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-01-17 20:29:50 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-01-17 20:29:50 -0600 |
commit | ff0a2678c4bce9da348e1263915558795e3a3640 (patch) | |
tree | 3c5b6ab01afaa3d01198d8755e1dc38fa18f5af4 /actionpack/lib/action_controller/url_encoded_pair_parser.rb | |
parent | aab760c3df4c02377a59a418fc077cdbc07e9fdc (diff) | |
download | rails-ff0a2678c4bce9da348e1263915558795e3a3640.tar.gz rails-ff0a2678c4bce9da348e1263915558795e3a3640.tar.bz2 rails-ff0a2678c4bce9da348e1263915558795e3a3640.zip |
Build query string and POST params parser on top of Rack::Request. Also switch our multipart parser to use Racks. Moved XML, JSON, and YAML parsers into ActionController::ParamsParser middleware [#1661 state:resolved]
Diffstat (limited to 'actionpack/lib/action_controller/url_encoded_pair_parser.rb')
-rw-r--r-- | actionpack/lib/action_controller/url_encoded_pair_parser.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/url_encoded_pair_parser.rb b/actionpack/lib/action_controller/url_encoded_pair_parser.rb index 9883ad0d85..b63dca987d 100644 --- a/actionpack/lib/action_controller/url_encoded_pair_parser.rb +++ b/actionpack/lib/action_controller/url_encoded_pair_parser.rb @@ -1,5 +1,66 @@ module ActionController class UrlEncodedPairParser < StringScanner #:nodoc: + class << self + def parse_query_parameters(query_string) + return {} if query_string.blank? + + pairs = query_string.split('&').collect do |chunk| + next if chunk.empty? + key, value = chunk.split('=', 2) + next if key.empty? + value = value.nil? ? nil : CGI.unescape(value) + [ CGI.unescape(key), value ] + end.compact + + new(pairs).result + end + + def parse_hash_parameters(params) + parser = new + + params = params.dup + until params.empty? + for key, value in params + if key.blank? + params.delete(key) + elsif value.is_a?(Array) + parser.parse(key, get_typed_value(value.shift)) + params.delete(key) if value.empty? + else + parser.parse(key, get_typed_value(value)) + params.delete(key) + end + end + end + + parser.result + end + + private + def get_typed_value(value) + case value + when String + value + when NilClass + '' + when Array + value.map { |v| get_typed_value(v) } + when Hash + if value.has_key?(:tempfile) + upload = value[:tempfile] + upload.extend(UploadedFile) + upload.original_path = value[:filename] + upload.content_type = value[:type] + upload + else + nil + end + else + raise "Unknown form value: #{value.inspect}" + end + end + end + attr_reader :top, :parent, :result def initialize(pairs = []) |