diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index cdbd8abae..dbcd3a0e8 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -28,19 +28,24 @@ module Fog }.merge(params) uri = URI.parse(params[:url]) path = "#{uri.path}" - path << "?#{uri.query}" if uri.query + if uri.query + path << "?#{uri.query}" + end host = "#{uri.host}" - host << ":#{uri.port}" if uri.scheme == "http" && uri.port != 80 - host << ":#{uri.port}" if uri.scheme == "https" && uri.port != 443 + if (uri.scheme == "http" && uri.port != 80) || + (uri.scheme == 'https' && uri.port != 443) + host << ":#{uri.port}" + end request = "#{params[:method]} #{path} HTTP/1.1\r\n" params[:headers]['Host'] = uri.host - params[:headers]['Content-Length'] = (params[:body].length) if params[:body] + if params[:body] + params[:headers]['Content-Length'] = params[:body].length + end for key, value in params[:headers] request << "#{key}: #{value}\r\n" end - request << "\r\n" - request << params[:body] if params[:body] + request << "\r\n#{params[:body]}" @connection.write(request) response = AWS::Response.new @@ -48,7 +53,9 @@ module Fog response.status = $1.to_i while true data = @connection.readline - break if data == "\r\n" + if data == "\r\n" + break + end if header = data.match(/(.*):\s(.*)\r\n/) response.headers[header[1]] = header[2] end @@ -61,7 +68,9 @@ module Fog @connection.readline =~ /([a-f0-9]*)\r\n/i chunk_size = $1.to_i(16) + 2 # 2 = "/r/n".length response.body << @connection.read(chunk_size) - break if $1.to_i(16) == 0 + if $1.to_i(16) == 0 + break + end end end response diff --git a/lib/fog/aws/s3.rb b/lib/fog/aws/s3.rb index 8748a9b08..5b3f410ab 100644 --- a/lib/fog/aws/s3.rb +++ b/lib/fog/aws/s3.rb @@ -207,8 +207,21 @@ module Fog end def canonicalize_amz_headers(headers) - headers = headers.select {|key,value| key.match(/^x-amz-/iu)}.sort {|x,y| x[0] <=> y[0]}.collect {|header| header.join(':')}.join("\n").downcase - headers.empty? ? nil : headers + amz_headers, canonical_amz_headers = {}, '' + for key, value in amz_headers + if key[0..5] == 'x-amz-' + amz_headers[key] = value + end + end + amz_headers = amz_headers.sort {|x, y| x[0] <=> y[0]} + for pair in amz_headers + canonical_amz_headers << "#{pair[0]}: #{pair[1]}\r\n" + end + if canonical_amz_headers.empty? + nil + else + canonical_amz_headers.chomp! + end end def canonicalize_resource(uri) @@ -216,10 +229,10 @@ module Fog if match = uri.host.match(/(.*).s3.amazonaws.com/) resource << "#{match[1]}/" end - resource << "#{uri.path[1..-1]}" if uri.path - resource << "?acl" if uri.to_s.include?('?acl') - resource << "?location" if uri.to_s.include?('?location') - resource << "?torrent" if uri.to_s.include?('?torrent') + resource << "#{uri.path[1..-1]}" + # resource << "?acl" if uri.to_s.include?('?acl') + # resource << "?location" if uri.to_s.include?('?location') + # resource << "?torrent" if uri.to_s.include?('?torrent') resource end @@ -251,9 +264,14 @@ module Fog canonicalized_amz_headers = canonicalize_amz_headers(headers), canonicalized_resource = canonicalize_resource(uri) ] - string_to_sign = params.delete_if {|value| value.nil?}.join("\n") - hmac = @hmac.update(string_to_sign) - signature = Base64.encode64(hmac.digest).strip + string_to_sign = '' + for value in params + unless value.nil? + string_to_sign << "#{value}\n" + end + end + hmac = @hmac.update(string_to_sign.chomp!) + signature = Base64.encode64(hmac.digest).strip! headers['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}" response = @connection.request({