From 28ba782f06d97085862f72227815d492fcfc83c9 Mon Sep 17 00:00:00 2001 From: Rafael Mendonça França Date: Fri, 18 Jul 2014 17:56:51 -0300 Subject: Fix media_type_params when Content-Type parameters contains quoted-strings According RFC 2616, the Content-Type parameters value can be even a token or a quoted-string: media-type = type "/" subtype *( ";" parameter ) type = token subtype = token parameter = attribute "=" value attribute = token value = token | quoted-string quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = >" quoted-pair = "\" CHAR --- lib/rack/request.rb | 2 +- test/spec_request.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rack/request.rb b/lib/rack/request.rb index 5a446acd..ac94026c 100644 --- a/lib/rack/request.rb +++ b/lib/rack/request.rb @@ -52,7 +52,7 @@ module Rack return {} if content_type.nil? Hash[*content_type.split(/\s*[;,]\s*/)[1..-1]. collect { |s| s.split('=', 2) }. - map { |k,v| [k.downcase, v] }.flatten] + map { |k,v| [k.downcase, v.gsub(/\A"|"\z/, "")] }.flatten] end # The character set of the request body if a "charset" media type diff --git a/test/spec_request.rb b/test/spec_request.rb index e5ec254e..09a308a6 100644 --- a/test/spec_request.rb +++ b/test/spec_request.rb @@ -611,7 +611,7 @@ describe Rack::Request do should "handle multiple media type parameters" do req = Rack::Request.new \ Rack::MockRequest.env_for("/", - "CONTENT_TYPE" => 'text/plain; foo=BAR,baz=bizzle dizzle;BLING=bam') + "CONTENT_TYPE" => 'text/plain; foo=BAR,baz=bizzle dizzle;BLING=bam;blong="boo";zump="zoo\"o"') req.should.not.be.form_data req.media_type_params.should.include 'foo' req.media_type_params['foo'].should.equal 'BAR' @@ -620,6 +620,8 @@ describe Rack::Request do req.media_type_params.should.not.include 'BLING' req.media_type_params.should.include 'bling' req.media_type_params['bling'].should.equal 'bam' + req.media_type_params['blong'].should.equal 'boo' + req.media_type_params['zump'].should.equal 'zoo\"o' end should "parse with junk before boundry" do -- cgit v1.2.3-24-ge0c7