summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-01-20 15:44:40 -0800
committerClaire McQuin <claire@getchef.com>2014-01-23 16:57:28 -0800
commitf837fd60720ca25a2ed1d16c79d5401fa0452228 (patch)
treebe86dd6fe691d1b6843ae3acdd154dad710edc30
parentc7c33f71e640e1690fd94b2fd923898da64010d1 (diff)
downloadchef-f837fd60720ca25a2ed1d16c79d5401fa0452228.tar.gz
adding validation of content-length field
-rw-r--r--lib/chef/http/validate_response.rb57
-rw-r--r--lib/chef/rest.rb2
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/chef/http/validate_response.rb b/lib/chef/http/validate_response.rb
new file mode 100644
index 0000000000..b38673ef3a
--- /dev/null
+++ b/lib/chef/http/validate_response.rb
@@ -0,0 +1,57 @@
+#--
+# Author:: Daniel DeLeo (<dan@opscode.com>)
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'pp'
+require 'chef/log'
+
+class Chef
+ class HTTP
+
+ # Middleware that takes an HTTP response, parses it as JSON if possible.
+ class ValidateResponse
+
+ def initialize(opts={})
+ end
+
+ def handle_request(method, url, headers={}, data=false)
+ [method, url, headers, data]
+ end
+
+ def handle_response(http_response, rest_request, return_value)
+ unless http_response['content-length']
+ Chef::Log.warn "HTTP server did not include a Content-Length header in response, cannot identify truncated downloads."
+ return [http_response, rest_request, return_value]
+ end
+ content_length = http_response['content-length'].is_a?(Array) ? http_response['content-length'].first.to_i : http_response['content-length'].to_i
+ Chef::Log.debug "Content-Length header = #{content_length}"
+ response_length = http_response.body.length # FIXME: use byte length to deal with encoding?
+ Chef::Log.debug "Response body length = #{response_length}"
+ if response_length != content_length
+ raise "Response body length #{response_length} does not match HTTP Content-Length header #{content_length}" #FIXME: real exception
+ end
+ return [http_response, rest_request, return_value]
+ end
+
+ def stream_response_handler(response)
+ nil
+ end
+
+ end
+ end
+end
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index 04ee0b0cb2..c96411203b 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -32,6 +32,7 @@ require 'chef/http/decompressor'
require 'chef/http/json_input'
require 'chef/http/json_to_model_output'
require 'chef/http/cookie_manager'
+require 'chef/http/validate_response'
require 'chef/config'
require 'chef/exceptions'
require 'chef/platform/query_helpers'
@@ -62,6 +63,7 @@ class Chef
@decompressor = Decompressor.new(options)
@authenticator = Authenticator.new(options)
+ @middlewares << ValidateResponse.new(options)
@middlewares << JSONInput.new(options)
@middlewares << JSONToModelOutput.new(options)
@middlewares << CookieManager.new(options)