diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-12-19 15:17:06 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-12-19 15:17:06 -0800 |
commit | a909fe71842b874305a3ca5aa2767a4986bcd4c1 (patch) | |
tree | f90a6ad162890f0687499778061c8e7c80750593 /lib | |
parent | 550132f4a37297d9b2b99dcf41ea9298506366ed (diff) | |
download | chef-a909fe71842b874305a3ca5aa2767a4986bcd4c1.tar.gz |
allow injecting tempfiles into Chef::HTTPlcg/chef-http-inject-tempfile
use this to inject the tempfile from
Chef::FileContentManagement::Tempfile
closes #2401
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/http.rb | 20 | ||||
-rw-r--r-- | lib/chef/provider/remote_file/http.rb | 5 |
2 files changed, 13 insertions, 12 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 14dd8b93a5..c9df4e1235 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -170,11 +170,10 @@ class Chef raise end - def streaming_request_with_progress(path, headers = {}, &progress_block) + def streaming_request_with_progress(path, headers = {}, tempfile = nil, &progress_block) http_attempts ||= 0 url = create_url(path) response, rest_request, return_value = nil, nil, nil - tempfile = nil data = nil method = :GET @@ -182,7 +181,7 @@ class Chef response, rest_request, return_value = send_http_request(method, url, processed_headers, data) do |http_response| if http_response.kind_of?(Net::HTTPSuccess) - tempfile = stream_to_tempfile(url, http_response, &progress_block) + tempfile = stream_to_tempfile(url, http_response, tempfile, &progress_block) end apply_stream_complete_middleware(http_response, rest_request, return_value) end @@ -217,11 +216,10 @@ class Chef # # @yield [tempfile] block to process the tempfile # @yieldparams [tempfile<Tempfile>] tempfile - def streaming_request(path, headers = {}) + def streaming_request(path, headers = {}, tempfile = nil) http_attempts ||= 0 url = create_url(path) response, rest_request, return_value = nil, nil, nil - tempfile = nil data = nil method = :GET @@ -229,7 +227,7 @@ class Chef response, rest_request, return_value = send_http_request(method, url, processed_headers, data) do |http_response| if http_response.kind_of?(Net::HTTPSuccess) - tempfile = stream_to_tempfile(url, http_response) + tempfile = stream_to_tempfile(url, http_response, tempfile) end apply_stream_complete_middleware(http_response, rest_request, return_value) end @@ -500,11 +498,13 @@ class Chef end # @api private - def stream_to_tempfile(url, response, &progress_block) + def stream_to_tempfile(url, response, tf = nil, &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 + if tf.nil? + tf = Tempfile.open("chef-rest") + if Chef::Platform.windows? + tf.binmode # required for binary files on Windows platforms + end end Chef::Log.debug("Streaming download from #{url} to tempfile #{tf.path}") # Stolen from http://www.ruby-forum.com/topic/166423 diff --git a/lib/chef/provider/remote_file/http.rb b/lib/chef/provider/remote_file/http.rb index ad044f9e3c..4732253e5b 100644 --- a/lib/chef/provider/remote_file/http.rb +++ b/lib/chef/provider/remote_file/http.rb @@ -61,12 +61,13 @@ class Chef def fetch http = Chef::HTTP::Simple.new(uri, http_client_opts) + tempfile = Chef::FileContentManagement::Tempfile.new(@new_resource).tempfile if want_progress? - tempfile = http.streaming_request_with_progress(uri, headers) do |size, total| + tempfile = http.streaming_request_with_progress(uri, headers, tempfile) do |size, total| events.resource_update_progress(new_resource, size, total, progress_interval) end else - tempfile = http.streaming_request(uri, headers) + tempfile = http.streaming_request(uri, headers, tempfile) end if tempfile update_cache_control_data(tempfile, http.last_response) |