29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/chef/streaming_cookbook_uploader.rb', line 29
def make_request(http_verb, to_url, user_id, secret_key_filename, params = {}, = {})
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?
params.each do |key, value|
if value.kind_of?(File)
content_file = value
filepath = value.path
filename = File.basename(filepath)
parts << StringPart.new( "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" + key.to_s + "\"; filename=\"" + filename + "\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n")
parts << StreamPart.new(value, File.size(filepath))
parts << StringPart.new("\r\n")
else
parts << StringPart.new( "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" + key.to_s + "\"\r\n\r\n")
parts << StringPart.new(value.to_s + "\r\n")
end
end
parts << StringPart.new("--" + boundary + "--\r\n")
end
body_stream = MultipartStream.new(parts)
timestamp = Time.now.utc.iso8601
url = URI.parse(to_url)
Chef::Log.logger.debug("Signing: method: #{http_verb}, path: #{url.path}, file: #{content_file}, User-id: #{user_id}, Timestamp: #{timestamp}")
content_body = parts.inject("") { |result,part| result + part.read(0, part.size) }
content_file.rewind if content_file
signing_options = {
: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 || ""))
.merge!(Mixlib::Authentication::SignedHeaderAuth.signing_object(signing_options).sign(secret_key))
content_file.rewind if content_file
= DefaultHeaders.merge(Hash[*.map{ |k,v| [k.to_s, v] }.flatten])
req = case http_verb
when :put
Net::HTTP::Put.new(url.path, )
when :post
Net::HTTP::Post.new(url.path, )
end
req.content_length = body_stream.size
req.content_type = 'multipart/form-data; boundary=' + boundary unless parts.empty?
req.body_stream = body_stream
http = Net::HTTP.new(url.host, url.port)
if url.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = http.request(req)
class << res
alias :to_s :body
def
self
end
def status
code.to_i
end
end
res
end
|