Skip to content

Commit 2dea3f3

Browse files
author
Wesley Beary
committed
more consistency, move sign stuff to its own method
1 parent d2ab5aa commit 2dea3f3

4 files changed

Lines changed: 67 additions & 49 deletions

File tree

‎benchs/stripping.rb‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'rubygems'
2+
require 'benchwarmer'
3+
4+
COUNT = 1_000_000
5+
data = "Content-Length: 100\r\n"
6+
Benchmark.bmbm(25) do |bench|
7+
bench.report('chomp') do
8+
COUNT.times do
9+
data.chomp
10+
end
11+
end
12+
bench.report('chop') do
13+
COUNT.times do
14+
data.chop
15+
end
16+
end
17+
bench.report('strip') do
18+
COUNT.times do
19+
data.strip
20+
end
21+
end
22+
bench.report('index') do
23+
COUNT.times do
24+
data[0..-3]
25+
end
26+
end
27+
end

‎lib/fog/aws.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def request(params)
4949
response = AWS::Response.new
5050
response.status = @connection.readline[9..11].to_i
5151
while true
52-
data = @connection.readline[0..-3]
52+
data = @connection.readline.chomp!
5353
if data == ""
5454
break
5555
end

‎lib/fog/aws/s3.rb‎

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -213,38 +213,6 @@ def url(bucket_name = nil, path = nil)
213213
url
214214
end
215215

216-
def canonicalize_amz_headers(headers)
217-
amz_headers, canonical_amz_headers = {}, ''
218-
for key, value in amz_headers
219-
if key[0..5] == 'x-amz-'
220-
amz_headers[key] = value
221-
end
222-
end
223-
amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]}
224-
for pair in amz_headers
225-
canonical_amz_headers << "#{pair[0]}: #{pair[1]}\r\n"
226-
end
227-
if canonical_amz_headers.empty?
228-
nil
229-
else
230-
canonical_amz_headers.chomp!
231-
end
232-
end
233-
234-
def canonicalize_resource(uri)
235-
resource = "/"
236-
# [0..-18] is anything prior to .s3.amazonaws.com
237-
subdomain = uri.host[0..-18]
238-
unless subdomain.empty?
239-
resource << "#{subdomain}/"
240-
end
241-
resource << "#{uri.path[1..-1]}"
242-
# resource << "?acl" if uri.to_s.include?('?acl')
243-
# resource << "?location" if uri.to_s.include?('?location')
244-
# resource << "?torrent" if uri.to_s.include?('?torrent')
245-
resource
246-
end
247-
248216
def parse_file(file)
249217
metadata = {
250218
:body => nil,
@@ -262,26 +230,49 @@ def parse_file(file)
262230
metadata
263231
end
264232

265-
def request(params)
233+
def sign(params)
266234
uri = URI.parse(params[:url])
267235
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
268-
params_to_sign = [
269-
params[:method],
270-
content_md5 = params[:headers]['Content-MD5'] || '',
271-
content_type = params[:headers]['Content-Type'] || '',
272-
params[:headers]['Date'],
273-
canonicalized_amz_headers = canonicalize_amz_headers(params[:headers]),
274-
canonicalized_resource = canonicalize_resource(uri)
275-
]
276-
string_to_sign = ''
277-
for value in params_to_sign
278-
unless value.nil?
279-
string_to_sign << "#{value}\n"
236+
237+
string_to_sign = "#{params[:method]}\n"
238+
string_to_sign << "#{params[:headers]['Content-MD5'] || ''}\n"
239+
string_to_sign << "#{params[:headers]['Content-Type'] || ''}\n"
240+
string_to_sign << "#{params[:headers]['Date']}\n"
241+
242+
amz_headers, canonical_amz_headers = {}, ''
243+
for key, value in amz_headers
244+
if key[0..5] == 'x-amz-'
245+
amz_headers[key] = value
280246
end
281247
end
282-
hmac = @hmac.update(string_to_sign.chomp!)
283-
signature = Base64.encode64(hmac.digest).strip!
248+
amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]}
249+
for pair in amz_headers
250+
canonical_amz_headers << "#{pair[0]}: #{pair[1]}\r\n"
251+
end
252+
unless canonical_amz_headers.empty?
253+
string_to_sign << "#{canonical_amz_headers}\n"
254+
end
255+
256+
canonical_resource = "/"
257+
# [0..-18] is anything prior to .s3.amazonaws.com
258+
subdomain = uri.host[0..-18]
259+
unless subdomain.empty?
260+
canonical_resource << "#{subdomain}/"
261+
end
262+
canonical_resource << "#{uri.path[1..-1]}"
263+
# canonical_resource << "?acl" if uri.to_s.include?('?acl')
264+
# canonical_resource << "?location" if uri.to_s.include?('?location')
265+
# canonical_resource << "?torrent" if uri.to_s.include?('?torrent')
266+
string_to_sign << "#{canonical_resource}"
267+
268+
hmac = @hmac.update(string_to_sign)
269+
signature = Base64.encode64(hmac.digest).chomp!
284270
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
271+
params
272+
end
273+
274+
def request(params)
275+
params = sign(params)
285276

286277
response = @connection.request({
287278
:body => params[:body],

‎lib/fog/aws/simpledb.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def request(params, parser)
298298
method = 'GET'
299299
string_to_sign = "#{method}\n#{@host}\n/\n" << query.chop
300300
hmac = @hmac.update(string_to_sign)
301-
query << "Signature=#{CGI.escape(Base64.encode64(hmac.digest).strip).gsub(/\+/, '%20')}"
301+
query << "Signature=#{CGI.escape(Base64.encode64(hmac.digest).chomp!).gsub(/\+/, '%20')}"
302302

303303
response = @connection.request({
304304
:method => method,

0 commit comments

Comments
 (0)