Class: Chef::HTTP::ValidateContentLength

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http/validate_content_length.rb

Overview

Middleware that validates the Content-Length header against the downloaded number of bytes.

This must run before the decompressor middleware, since otherwise we will count the uncompressed streamed bytes, rather than the on-the-wire compressed bytes.

Defined Under Namespace

Classes: ContentLengthCounter

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ValidateContentLength

Returns a new instance of ValidateContentLength.



44
# File 'lib/chef/http/validate_content_length.rb', line 44

def initialize(opts = {}); end

Instance Method Details

#handle_request(method, url, headers = {}, data = false) ⇒ Object



46
47
48
# File 'lib/chef/http/validate_content_length.rb', line 46

def handle_request(method, url, headers = {}, data = false)
  [method, url, headers, data]
end

#handle_response(http_response, rest_request, return_value) ⇒ Object



50
51
52
53
# File 'lib/chef/http/validate_content_length.rb', line 50

def handle_response(http_response, rest_request, return_value)
  validate(http_response, http_response.body.bytesize) if http_response && http_response.body
  [http_response, rest_request, return_value]
end

#handle_stream_complete(http_response, rest_request, return_value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/http/validate_content_length.rb', line 55

def handle_stream_complete(http_response, rest_request, return_value)
  if @content_length_counter.nil?
    Chef::Log.trace("No content-length information collected for the streamed download, cannot identify streamed download.")
  else
    validate(http_response, @content_length_counter.content_length)
  end

  # Make sure the counter is reset since this object might get used
  # again. See CHEF-5100
  @content_length_counter = nil
  [http_response, rest_request, return_value]
end

#stream_response_handler(response) ⇒ Object



68
69
70
# File 'lib/chef/http/validate_content_length.rb', line 68

def stream_response_handler(response)
  @content_length_counter = ContentLengthCounter.new
end