Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing of JSON/XML data from multipart requests #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ begin
gem.authors = ["Niko Dittmann"]

gem.add_development_dependency "shoulda", ">= 0"
gem.add_runtime_dependency "rack", ">= 1.0"
gem.add_runtime_dependency "activesupport", ">= 2.3"
end
Jeweler::GemcutterTasks.new
Expand Down
28 changes: 27 additions & 1 deletion lib/rack/post-body-to-params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# gem 'activesupport', '=3.0.0.beta4' # for tests
require 'active_support'
require 'active_support/core_ext/hash'
require 'rack/multipart/parser'

module Rack

Expand Down Expand Up @@ -100,8 +101,29 @@ def xml_error_response(error)
def call(env)
content_type = env[CONTENT_TYPE] && env[CONTENT_TYPE].split(';').first

# Check for multipart and use body from multipart
files = {}
if content_type.eql?("multipart/mixed")
root = env[CONTENT_TYPE][/.* start=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni] && ($1 || $2)
part_type = env[CONTENT_TYPE][/.* type=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni] && ($1 || $2)
if @content_types.include?(part_type)
content_type = part_type

unless env[FORM_HASH]
# This should only happen if Rack isn't available (like in tests)
multipart_parser = Rack::Multipart::Parser.new(env)
env[FORM_HASH] = multipart_parser.parse
end

post_body = env[FORM_HASH].delete(root)

# Get files and put them in a hash to be merged with the params later
env[FORM_HASH].each {|k,v| files[k] = v if v.key?(:filename) && v.key?(:tempfile)}
end
end

if content_type && @content_types.include?(content_type)
post_body = env[POST_BODY].read
post_body ||= env[POST_BODY].read

unless post_body.blank?
begin
Expand All @@ -110,6 +132,10 @@ def call(env)
logger.warn "#{self.class} #{content_type} parsing error: #{error.to_s}" if respond_to? :logger
return error_responses[content_type].call error
end

# Merge files from multipart (merges empty hash when not multipart)
new_form_hash.merge!(files)

env.update(FORM_HASH => new_form_hash, FORM_INPUT => env[POST_BODY])
end

Expand Down
31 changes: 31 additions & 0 deletions test/test_post-body-to-params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ class TestPostBodyToParams < Test::Unit::TestCase
end
end

should "process multipart requests that contain a root part of proper type" do
body = <<-EOS
--MultipartBoundary\r
Content-Disposition: form-data; name="json"\r
Content-Type: "application/json; charset=UTF-8"\r
\r
{"bla":"blub"}\r
\r
--MultipartBoundary\r
Content-Disposition: form-data; name="multipart_file"; filename="multipart_file"\r
Content-Length: 22\r
Content-Type: text/plain\r
Content-Transfer-Encoding: binary\r
\r
file content goes here\r
--MultipartBoundary--\r
EOS

env = {
'CONTENT_TYPE' => 'multipart/mixed; boundary="MultipartBoundary"; type="application/json"; start="json"',
'rack.input' => StringIO.new(body)
}
new_env = @app.call(env)
assert_equal(["bla", "multipart_file"].sort, new_env['rack.request.form_hash'].keys.sort)
assert_equal(new_env['rack.request.form_hash']["bla"], "blub")
file = new_env['rack.request.form_hash']["multipart_file"]
assert file && file.is_a?(Hash)
assert file.key?(:filename)
assert file.key?(:tempfile)
end

end

end