summaryrefslogtreecommitdiff
path: root/lib/chef/http.rb
diff options
context:
space:
mode:
authorThom May <thom@may.lt>2016-03-15 21:05:49 +0000
committerThom May <thom@may.lt>2016-03-15 21:05:49 +0000
commitc1a389c2a8452e9b796aa1d34c4d9e51f4af30c7 (patch)
treeed2cc9b0c226a21ba3b9ab6b101fa76cb2db4891 /lib/chef/http.rb
parent47cd0cb9f2c14ced5a17ea0d1da34b9aeaaf36d8 (diff)
parentff539423f067ee83c07dcf73cbf688c6a07f64ae (diff)
downloadchef-c1a389c2a8452e9b796aa1d34c4d9e51f4af30c7.tar.gz
Merge pull request #4658 from chef/tm/remote_file_download_progress11.9
Remote file download progress
Diffstat (limited to 'lib/chef/http.rb')
-rw-r--r--lib/chef/http.rb32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb
index 15fb20d588..c6afa97d23 100644
--- a/lib/chef/http.rb
+++ b/lib/chef/http.rb
@@ -74,6 +74,7 @@ class Chef
attr_reader :redirect_limit
attr_reader :options
+
attr_reader :middlewares
# Create a HTTP client object. The supplied +url+ is used as the base for
@@ -153,6 +154,33 @@ class Chef
raise
end
+ def streaming_request_with_progress(path, headers = {}, &progress_block)
+ url = create_url(path)
+ response, rest_request, return_value = nil, nil, nil
+ tempfile = nil
+
+ method = :GET
+ method, url, headers, data = apply_request_middleware(method, url, headers, data)
+
+ response, rest_request, return_value = send_http_request(method, url, headers, data) do |http_response|
+ if http_response.kind_of?(Net::HTTPSuccess)
+ tempfile = stream_to_tempfile(url, http_response, &progress_block)
+ end
+ apply_stream_complete_middleware(http_response, rest_request, return_value)
+ end
+ return nil if response.kind_of?(Net::HTTPRedirection)
+ unless response.kind_of?(Net::HTTPSuccess)
+ response.error!
+ end
+ tempfile
+ rescue Exception => e
+ log_failed_request(response, return_value) unless response.nil?
+ if e.respond_to?(:chef_rest_request=)
+ e.chef_rest_request = rest_request
+ end
+ raise
+ end
+
# Makes a streaming download request, streaming the response body to a
# tempfile. If a block is given, the tempfile is passed to the block and
# the tempfile will automatically be unlinked after the block is executed.
@@ -392,7 +420,8 @@ class Chef
headers
end
- def stream_to_tempfile(url, response)
+ def stream_to_tempfile(url, response, &progress_block)
+ content_length = response["Content-Length"]
tf = Tempfile.open("chef-rest")
if Chef::Platform.windows?
tf.binmode # required for binary files on Windows platforms
@@ -405,6 +434,7 @@ class Chef
response.read_body do |chunk|
tf.write(stream_handler.handle_chunk(chunk))
+ yield tf.size, content_length if block_given?
end
tf.close
tf