mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
some cleanup
This commit is contained in:
parent
22995aff83
commit
f8a38661a8
2 changed files with 44 additions and 17 deletions
|
@ -28,19 +28,24 @@ module Fog
|
||||||
}.merge(params)
|
}.merge(params)
|
||||||
uri = URI.parse(params[:url])
|
uri = URI.parse(params[:url])
|
||||||
path = "#{uri.path}"
|
path = "#{uri.path}"
|
||||||
path << "?#{uri.query}" if uri.query
|
if uri.query
|
||||||
|
path << "?#{uri.query}"
|
||||||
|
end
|
||||||
host = "#{uri.host}"
|
host = "#{uri.host}"
|
||||||
host << ":#{uri.port}" if uri.scheme == "http" && uri.port != 80
|
if (uri.scheme == "http" && uri.port != 80) ||
|
||||||
host << ":#{uri.port}" if uri.scheme == "https" && uri.port != 443
|
(uri.scheme == 'https' && uri.port != 443)
|
||||||
|
host << ":#{uri.port}"
|
||||||
|
end
|
||||||
|
|
||||||
request = "#{params[:method]} #{path} HTTP/1.1\r\n"
|
request = "#{params[:method]} #{path} HTTP/1.1\r\n"
|
||||||
params[:headers]['Host'] = uri.host
|
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]
|
for key, value in params[:headers]
|
||||||
request << "#{key}: #{value}\r\n"
|
request << "#{key}: #{value}\r\n"
|
||||||
end
|
end
|
||||||
request << "\r\n"
|
request << "\r\n#{params[:body]}"
|
||||||
request << params[:body] if params[:body]
|
|
||||||
@connection.write(request)
|
@connection.write(request)
|
||||||
|
|
||||||
response = AWS::Response.new
|
response = AWS::Response.new
|
||||||
|
@ -48,7 +53,9 @@ module Fog
|
||||||
response.status = $1.to_i
|
response.status = $1.to_i
|
||||||
while true
|
while true
|
||||||
data = @connection.readline
|
data = @connection.readline
|
||||||
break if data == "\r\n"
|
if data == "\r\n"
|
||||||
|
break
|
||||||
|
end
|
||||||
if header = data.match(/(.*):\s(.*)\r\n/)
|
if header = data.match(/(.*):\s(.*)\r\n/)
|
||||||
response.headers[header[1]] = header[2]
|
response.headers[header[1]] = header[2]
|
||||||
end
|
end
|
||||||
|
@ -61,7 +68,9 @@ module Fog
|
||||||
@connection.readline =~ /([a-f0-9]*)\r\n/i
|
@connection.readline =~ /([a-f0-9]*)\r\n/i
|
||||||
chunk_size = $1.to_i(16) + 2 # 2 = "/r/n".length
|
chunk_size = $1.to_i(16) + 2 # 2 = "/r/n".length
|
||||||
response.body << @connection.read(chunk_size)
|
response.body << @connection.read(chunk_size)
|
||||||
break if $1.to_i(16) == 0
|
if $1.to_i(16) == 0
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
response
|
response
|
||||||
|
|
|
@ -207,8 +207,21 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def canonicalize_amz_headers(headers)
|
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
|
amz_headers, canonical_amz_headers = {}, ''
|
||||||
headers.empty? ? nil : 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
|
end
|
||||||
|
|
||||||
def canonicalize_resource(uri)
|
def canonicalize_resource(uri)
|
||||||
|
@ -216,10 +229,10 @@ module Fog
|
||||||
if match = uri.host.match(/(.*).s3.amazonaws.com/)
|
if match = uri.host.match(/(.*).s3.amazonaws.com/)
|
||||||
resource << "#{match[1]}/"
|
resource << "#{match[1]}/"
|
||||||
end
|
end
|
||||||
resource << "#{uri.path[1..-1]}" if uri.path
|
resource << "#{uri.path[1..-1]}"
|
||||||
resource << "?acl" if uri.to_s.include?('?acl')
|
# resource << "?acl" if uri.to_s.include?('?acl')
|
||||||
resource << "?location" if uri.to_s.include?('?location')
|
# resource << "?location" if uri.to_s.include?('?location')
|
||||||
resource << "?torrent" if uri.to_s.include?('?torrent')
|
# resource << "?torrent" if uri.to_s.include?('?torrent')
|
||||||
resource
|
resource
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -251,9 +264,14 @@ module Fog
|
||||||
canonicalized_amz_headers = canonicalize_amz_headers(headers),
|
canonicalized_amz_headers = canonicalize_amz_headers(headers),
|
||||||
canonicalized_resource = canonicalize_resource(uri)
|
canonicalized_resource = canonicalize_resource(uri)
|
||||||
]
|
]
|
||||||
string_to_sign = params.delete_if {|value| value.nil?}.join("\n")
|
string_to_sign = ''
|
||||||
hmac = @hmac.update(string_to_sign)
|
for value in params
|
||||||
signature = Base64.encode64(hmac.digest).strip
|
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}"
|
headers['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
|
||||||
|
|
||||||
response = @connection.request({
|
response = @connection.request({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue