aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2013-08-13 13:20:36 +0200
committerGibheer <gibheer@gmail.com>2013-08-13 13:20:36 +0200
commit00e0c0170385cc2bd8a9ac599f5462d75292efde (patch)
tree70132191b1977e04a18383cef551ba826a65ce3c
parent30215c37d1da0cf611854fcc246fe115d166008b (diff)
fix paramter extraction with charset
This fixes a bug where a charset in the content type blocked the extraction of the payload. A browser may send a string like multipart/form-data; charset=UTF-8 which include? could not find the defined types. Now it gets split and then checked against the defined types.
-rw-r--r--lib/zero/request/parameter.rb13
-rw-r--r--spec/unit/zero/request/parameter/payload_spec.rb10
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/zero/request/parameter.rb b/lib/zero/request/parameter.rb
index c8d324c..faae5c7 100644
--- a/lib/zero/request/parameter.rb
+++ b/lib/zero/request/parameter.rb
@@ -16,6 +16,9 @@ module Zero
ENV_KEY_CUSTOM = 'zero.params.custom'
# the key for the content type
ENV_KEY_CONTENT_TYPE = 'CONTENT_TYPE'
+ # the separator of the type and charset
+ # for example multipart/form-data; charset=UTF-8
+ CONTENT_TYPE_SEPERATOR = ';'
# all content types which used for using the body as a parameter input
PAYLOAD_CONTENT_TYPES = [
'application/x-www-form-urlencoded',
@@ -87,12 +90,20 @@ module Zero
# extracts the key value pairs from the body
# @return Hash all key value pairs from the payload
def extract_payload_params(environment)
- unless PAYLOAD_CONTENT_TYPES.include?(environment[ENV_KEY_CONTENT_TYPE])
+ unless matches_payload_types?(environment[ENV_KEY_CONTENT_TYPE])
return {}
end
parse_string(environment[ENV_KEY_PAYLOAD].read)
end
+ # check if the content-type matches one of the payload types
+ # @param [String] type - the content type string
+ # @return Boolean true if it matches
+ def matches_payload_types?(type)
+ return false if type.nil?
+ PAYLOAD_CONTENT_TYPES.include?(type.split(CONTENT_TYPE_SEPERATOR)[0])
+ end
+
# parse the query string like input to a hash
# @param query [String] the query string
# @return [Hash] the key/valuie pairs
diff --git a/spec/unit/zero/request/parameter/payload_spec.rb b/spec/unit/zero/request/parameter/payload_spec.rb
index 9cf1198..6f54361 100644
--- a/spec/unit/zero/request/parameter/payload_spec.rb
+++ b/spec/unit/zero/request/parameter/payload_spec.rb
@@ -50,6 +50,16 @@ describe Zero::Request::Parameter, '#payload' do
its(:payload) { should == {'bar[]' => ['foo', 'bar']} }
end
+ context 'with encoding in the content type' do
+ let(:env) do
+ EnvGenerator.post('/foo', {
+ :input => 'foo=bar',
+ 'CONTENT_TYPE' => 'multipart/form-data; charset=UTF-8'
+ })
+ end
+ its(:payload) { should == {'foo' => 'bar'} }
+ end
+
# TODO behaves like this, but is this really good like this?
context 'with a post body and content type application/json' do
let(:env) do