From 11c8f1976b403ba45f5fb607e908bc8d14385ef2 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 29 Sep 2021 10:41:10 -0500 Subject: [PATCH] Revise legacy response to work with #470 --- src/deprecated.jl | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/deprecated.jl b/src/deprecated.jl index 388f8fab1..466fd1867 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -5,6 +5,10 @@ function legacy_response( response_dict_type = something(request.response_dict_type, LittleDict) return_headers = something(return_headers, false) + # When a user defined I/O stream is passed in use the actual `HTTP.Response` body + # instead of the `AWS.Response` body which requires the I/O stream to be seekable. + body = request.response_stream !== nothing ? response.response.body : response.body + # The stored service name is always lowercase and may not match the module name # specified by the user. We'll assume that the typical casing used is titlecase. alt_service = "@service $(titlecase(request.service)) use_response_type=true" @@ -45,12 +49,19 @@ function legacy_response( end # Emulate HTTP 0.9.14 behavior of always closing the passed in stream. Doing this is - # particularly important for `Base.BufferStream` + # particularly important for `Base.BufferStream`. if request.backend isa HTTPBackend close(request.response_stream) end return request.response_stream + elseif request.response_stream !== nothing + # Emulate HTTP 0.9.14 behavior of always closing the passed in stream and the + # `read_body` behavior of closing all non-`IOBuffer` streams when `return_stream` is + # not `true`. Doing this is particularly important for `Base.BufferStream`. + if request.backend isa HTTPBackend || !(request.response_stream isa IOBuffer) + close(request.response_stream) + end end # Return raw data if requested... @@ -73,19 +84,19 @@ function legacy_response( ) end - return (return_headers ? (response.body, response.headers) : response.body) + return (return_headers ? (body, response.headers) : body) end # Parse response data according to mimetype... mime = HTTP.header(response.response, "Content-Type", "") if isempty(mime) - if length(response.body) > 5 && response.body[1:5] == b" 5 && body[1:5] == b"