From 16419cdcb5c593fadeaa7d132b3a1cf1498626b6 Mon Sep 17 00:00:00 2001 From: ericphanson <5846501+ericphanson@users.noreply.github.com> Date: Mon, 12 Jul 2021 15:00:34 +0000 Subject: [PATCH] use our own global downloader; don't follow redirects; verbose=true for now --- src/AWS.jl | 2 +- src/utilities/request.jl | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/AWS.jl b/src/AWS.jl index 047620454..0245acc06 100644 --- a/src/AWS.jl +++ b/src/AWS.jl @@ -3,7 +3,7 @@ module AWS using Compat: Compat, @something using Base64 using Dates -using Downloads: Downloads +using Downloads: Downloads, Downloader, Curl using HTTP using MbedTLS using Mocking diff --git a/src/utilities/request.jl b/src/utilities/request.jl index ab8d01836..959528615 100644 --- a/src/utilities/request.jl +++ b/src/utilities/request.jl @@ -24,6 +24,23 @@ default_backend() = DownloadsBackend() submit_request(aws::AbstractAWSConfig, request::Request; return_headers::Bool=false) = submit_request(default_backend(), aws, request; return_headers) +const AWS_DOWNLOADER = Ref{Union{Nothing, Downloader}}(nothing) +const AWS_DOWNLOAD_LOCK = ReentrantLock() + +# https://github.com/JuliaLang/Downloads.jl/blob/84e948c02b8a0625552a764bf90f7d2ee97c949c/src/Downloads.jl#L293-L301 +function get_downloader(downloader=nothing) + lock(AWS_DOWNLOAD_LOCK) do + yield() # let other downloads finish + downloader isa Downloader && return + while true + downloader = AWS_DOWNLOADER[] + downloader isa Downloader && return + AWS_DOWNLOADER[] = Downloader() + end + end + return downloader +end + function _http_request(::DownloadsBackend, request) # If we pass `output`, Downloads.jl will expect a message # body in the response. Specifically, it sets @@ -58,11 +75,15 @@ function _http_request(::DownloadsBackend, request) input_arg = NamedTuple() end + downloader = @something(request.downloader, get_downloader()) + # set the hook so that we don't follow redirects + downloader.easy_hook = (easy, info) -> Curl.setopt(easy, Curl.CURLOPT_FOLLOWLOCATION, false) + + response = Downloads.request(request.url; input_arg..., output_arg..., method = request.request_method, - request.headers, verbose=false, throw=true, - request.downloader) - + request.headers, verbose=true, throw=true, + downloader) http_response = HTTP.Response(response.status, response.headers; body_arg()..., request=nothing) if HTTP.iserror(http_response)