From f2a7ca9b7de3df1caf821d95f801c01ff572b8d9 Mon Sep 17 00:00:00 2001 From: Tanmay Mohapatra Date: Mon, 13 Dec 2021 21:51:50 +0530 Subject: [PATCH] should also look into headers for input_size (#167) If no content length is set while uploading some contents, Curl defaults to use chunked transfer encoding. In some cases we want to prevent that because the server may not support chunked transfers. With this change, the request method will also look at the headers while determining the input size and if found call `set_upload_size` as usual. So to switch off chunked transfers, one must also know and set the content length header while invoking `download` or `request` methods. (cherry picked from commit ab628ab9c971bc79be755c18f12f842eaf45f65c) --- src/Downloads.jl | 13 +++++++++++++ test/runtests.jl | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/Downloads.jl b/src/Downloads.jl index b56561d..f1733f7 100644 --- a/src/Downloads.jl +++ b/src/Downloads.jl @@ -305,6 +305,10 @@ function request( input = something(input, devnull) output = something(output, devnull) input_size = arg_read_size(input) + if input_size === nothing + # take input_size from content-length header if one is supplied + input_size = content_length(headers) + end progress = p_func(progress, input, output) arg_read(input) do input arg_write(output) do output @@ -395,4 +399,13 @@ arg_read_size(io::Base.GenericIOBuffer) = bytesavailable(io) arg_read_size(::Base.DevNull) = 0 arg_read_size(::Any) = nothing +function content_length(headers::Union{AbstractVector, AbstractDict}) + for (key, value) in headers + if lowercase(key) == "content-length" && isa(value, AbstractString) + return tryparse(Int, value) + end + end + return nothing +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 0b1bab5..23ef3cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -513,6 +513,8 @@ include("setup.jl") @test Downloads.arg_read_size(IOBuffer("αa")) == 3 @test Downloads.arg_read_size(IOBuffer(codeunits("αa"))) == 3 # Issue #142 @test Downloads.arg_read_size(devnull) == 0 + @test Downloads.content_length(["Accept"=>"*/*",]) === nothing + @test Downloads.content_length(["Accept"=>"*/*", "Content-Length"=>"100"]) == 100 end end