socketry / async-http

MIT License
298 stars 45 forks source link

[Client] Response headers are included in response body when downloading an AWS S3 object #8

Closed janko closed 6 years ago

janko commented 6 years ago

When downloading an S3 object with Async::HTTP::Client, the response headers are included in the response body. Here is a minimal self-contained example showing the problem:

require "async"
require "async/http"
require "aws-sdk-s3"

resource = Aws::S3::Resource.new(
  access_key_id:     ENV.fetch("S3_ACCESS_KEY_ID"),
  secret_access_key: ENV.fetch("S3_SECRET_ACCESS_KEY"),
  region:            ENV.fetch("S3_REGION"),
)
bucket = resource.bucket(ENV.fetch("S3_BUCKET"))
object = bucket.object("foo")
object.put(body: "this is some content")

uri      = URI.parse(object.presigned_url(:get))
endpoint = Async::HTTP::URLEndpoint.parse("#{uri.scheme}://#{uri.host}")
client   = Async::HTTP::Client.new(endpoint)

Async.run do
  response = client.get(uri.request_uri)
  puts response.read
end
x-amz-request-id: 96FBFDFFB421E753                                                                                                                            
Date: Wed, 04 Jul 2018 19:57:33 GMT                                                                                                                           
Last-Modified: Wed, 04 Jul 2018 19:57:33 GMT                                                                                                                  
ETag: "736db904ad222bf88ee6b8d103fceb8e"                                                                                                                      
Accept-Ranges: bytes                                                                                                                                          
Content-Type:                                                                                                                                                 
Content-Length: 20                                                                                                                                            
Server: AmazonS3                                                                                                                                              

this is some content 

So far I was able to reproduce this only with AWS S3 objects, I'm guessing something in their response trips up async-http's HTTP parser.