diff options
author | danielsdeleo <dan@getchef.com> | 2014-07-29 15:36:53 -0700 |
---|---|---|
committer | danielsdeleo <dan@getchef.com> | 2014-07-30 14:07:07 -0700 |
commit | e2423a426a79196f327feea16ada95f88d2df9fb (patch) | |
tree | da743c2ad3db546d8b04b53131024a4381da361c /lib/chef/http | |
parent | c8a54df7ca24f1482a701d5f39879cdc6c836f8a (diff) | |
download | chef-e2423a426a79196f327feea16ada95f88d2df9fb.tar.gz |
Allow requests to opt-out from JSON encoding the body
* JSONInput middleware now checks the incoming headers for a
Content-Type; if set to a non-json type, the body is passed through
unchanged. This allows us to use our normal HTTP client objects to
make requests with non-JSON bodies, such as file uploads.
* Add test coverage for the JSONInput middleware.
Diffstat (limited to 'lib/chef/http')
-rw-r--r-- | lib/chef/http/json_input.rb | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/chef/http/json_input.rb b/lib/chef/http/json_input.rb index 1e4e736030..23ccc3a8a7 100644 --- a/lib/chef/http/json_input.rb +++ b/lib/chef/http/json_input.rb @@ -29,7 +29,8 @@ class Chef end def handle_request(method, url, headers={}, data=false) - if data + if data && should_encode_as_json?(headers) + headers.delete_if { |key, _value| key.downcase == 'content-type' } headers["Content-Type"] = 'application/json' data = Chef::JSONCompat.to_json(data) # Force encoding to binary to fix SSL related EOFErrors @@ -52,6 +53,16 @@ class Chef [http_response, rest_request, return_value] end + private + + def should_encode_as_json?(headers) + # ruby/Net::HTTP don't enforce capitalized headers (it normalizes them + # for you before sending the request), so we have to account for all + # the variations we might find + requested_content_type = headers.find {|k, v| k.downcase == "content-type" } + requested_content_type.nil? || requested_content_type.last.include?("json") + end + end end end |