diff options
Diffstat (limited to 'lib/chef/cookbook_site_streaming_uploader.rb')
-rw-r--r-- | lib/chef/cookbook_site_streaming_uploader.rb | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/lib/chef/cookbook_site_streaming_uploader.rb b/lib/chef/cookbook_site_streaming_uploader.rb index 2be189e942..c0e85ff984 100644 --- a/lib/chef/cookbook_site_streaming_uploader.rb +++ b/lib/chef/cookbook_site_streaming_uploader.rb @@ -1,8 +1,8 @@ # # Author:: Stanislav Vitvitskiy -# Author:: Nuo Yan (nuo@opscode.com) -# Author:: Christopher Walters (<cw@opscode.com>) -# Copyright:: Copyright (c) 2009, 2010 Opscode, Inc. +# Author:: Nuo Yan (nuo@chef.io) +# Author:: Christopher Walters (<cw@chef.io>) +# Copyright:: Copyright 2009-2016, 2010-2016 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,10 +18,10 @@ # limitations under the License. # -require 'uri' -require 'net/http' -require 'mixlib/authentication/signedheaderauth' -require 'openssl' +require "uri" +require "net/http" +require "mixlib/authentication/signedheaderauth" +require "openssl" class Chef # == Chef::CookbookSiteStreamingUploader @@ -31,7 +31,7 @@ class Chef # inspired by http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html class CookbookSiteStreamingUploader - DefaultHeaders = { 'accept' => 'application/json', 'x-chef-version' => ::Chef::VERSION } + DefaultHeaders = { "accept" => "application/json", "x-chef-version" => ::Chef::VERSION } # rubocop:disable Style/ConstantName class << self @@ -73,11 +73,10 @@ class Chef end def make_request(http_verb, to_url, user_id, secret_key_filename, params = {}, headers = {}) - boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ' + boundary = "----RubyMultipartClient" + rand(1000000).to_s + "ZZZZZ" parts = [] content_file = nil - timestamp = Time.now.utc.iso8601 secret_key = OpenSSL::PKey::RSA.new(File.read(secret_key_filename)) unless params.nil? || params.empty? @@ -114,14 +113,14 @@ class Chef # TODO: tim: 2009-12-28: It'd be nice to remove this special case, and # always hash the entire request body. In the file case it would just be # expanded multipart text - the entire body of the POST. - content_body = parts.inject("") { |result,part| result + part.read(0, part.size) } + content_body = parts.inject("") { |result, part| result + part.read(0, part.size) } content_file.rewind if content_file # we consumed the file for the above operation, so rewind it. signing_options = { - :http_method=>http_verb, - :path=>url.path, - :user_id=>user_id, - :timestamp=>timestamp} + :http_method => http_verb, + :path => url.path, + :user_id => user_id, + :timestamp => timestamp } (content_file && signing_options[:file] = content_file) || (signing_options[:body] = (content_body || "")) headers.merge!(Mixlib::Authentication::SignedHeaderAuth.signing_object(signing_options).sign(secret_key)) @@ -129,7 +128,7 @@ class Chef content_file.rewind if content_file # net/http doesn't like symbols for header keys, so we'll to_s each one just in case - headers = DefaultHeaders.merge(Hash[*headers.map{ |k,v| [k.to_s, v] }.flatten]) + headers = DefaultHeaders.merge(Hash[*headers.map { |k, v| [k.to_s, v] }.flatten]) req = case http_verb when :put @@ -138,7 +137,7 @@ class Chef Net::HTTP::Post.new(url.path, headers) end req.content_length = body_stream.size - req.content_type = 'multipart/form-data; boundary=' + boundary unless parts.empty? + req.content_type = "multipart/form-data; boundary=" + boundary unless parts.empty? req.body_stream = body_stream http = Chef::HTTP::BasicClient.new(url).http_client @@ -150,11 +149,11 @@ class Chef alias :to_s :body # BUGBUG this makes the response compatible with what respsonse_steps expects to test headers (response.headers[] -> response[]) - def headers + def headers # rubocop:disable Lint/NestedMethodDefinition self end - def status + def status # rubocop:disable Lint/NestedMethodDefinition code.to_i end end @@ -201,12 +200,12 @@ class Chef end def size - @parts.inject(0) {|size, part| size + part.size} + @parts.inject(0) { |size, part| size + part.size } end def read(how_much, dst_buf = nil) if @part_no >= @parts.size - dst_buf.replace('') if dst_buf + dst_buf.replace("") if dst_buf return dst_buf end @@ -229,14 +228,14 @@ class Chef next_part = read(how_much_next_part) result = current_part + if next_part next_part - else - '' - end + else + "" + end else @part_offset += how_much_current_part result = current_part end - dst_buf ? dst_buf.replace(result || '') : result + dst_buf ? dst_buf.replace(result || "") : result end end |