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


44
45
# 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


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

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

#handle_response(http_response, rest_request, return_value) ⇒ Object


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

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


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

def handle_stream_complete(http_response, rest_request, return_value)
  if @content_length_counter.nil?
    Chef::Log.debug("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


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

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