1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Merge pull request #597 from stanhu/sh-handle-lowercase-http-headers

Fix handling of lowercased HTTP headers
This commit is contained in:
Wesley Beary 2021-03-02 09:42:54 -06:00 committed by GitHub
commit 1ee291f465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View file

@ -17,6 +17,15 @@ module Fog
model Fog::AWS::Storage::File
DASHED_HEADERS = %w(
Cache-Control
Content-Disposition
Content-Encoding
Content-Length
Content-MD5
Content-Type
).freeze
def all(options = {})
requires :directory
options = {
@ -114,8 +123,29 @@ module Fog
end
def normalize_headers(data)
data.headers['Last-Modified'] = Time.parse(data.get_header('Last-Modified'))
data.headers['ETag'] = data.get_header('ETag').gsub('"','')
data.headers['Last-Modified'] = Time.parse(fetch_and_delete_header(data, 'Last-Modified'))
etag = fetch_and_delete_header(data, 'ETag').gsub('"','')
data.headers['ETag'] = etag
DASHED_HEADERS.each do |header|
value = fetch_and_delete_header(data, header)
data.headers[header] = value if value
end
end
private
def fetch_and_delete_header(response, header)
value = response.get_header(header)
return unless value
response.headers.keys.each do |key|
response.headers.delete(key) if key.downcase == header.downcase
end
value
end
end
end

View file

@ -50,6 +50,38 @@ Shindo.tests("Storage[:aws] | files", ["aws"]) do
end
end
tests('#normalize_headers') do
files = @directory.files
response = Excon::Response.new
current_time = Time.new(2021, 02, 21)
response.headers['last-modified'] = current_time.to_s
response.headers['etag'] = '12345'
response.headers['ETAG'] = '12345'
response.headers['Cache-Control'] = 'no-cache'
response.headers['Content-disposition'] = 'attachment'
response.headers['content-length'] = 100
response.headers['content-Encoding'] = 'gzip'
response.headers['content-md5'] = 'ABCDEAB'
response.headers['content-Md5'] = 'ABCDEAB'
response.headers['ConTent-Type'] = 'application/json'
expected = {
'Last-Modified' => current_time,
'ETag' => '12345',
'Cache-Control' => 'no-cache',
'Content-Disposition' => 'attachment',
'Content-Length' => 100,
'Content-Encoding' => 'gzip',
'Content-MD5' => 'ABCDEAB',
'Content-Type' => 'application/json'
}
tests('header keys are normalized').returns(expected) do
files.normalize_headers(response)
response.headers
end
end
end
@directory.versions.each(&:destroy)