@@ -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 ] ,
0 commit comments