Skip to content

Commit c869d85

Browse files
authored
Add content auto decoding for download() (#986)
* Add content auto decoding for download * Address review and run and make pass download.jl
1 parent 561c80c commit c869d85

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/download.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using .Pairs
2+
using CodecZlib
23

34
"""
45
safer_joinpath(basepart, parts...)
@@ -68,12 +69,6 @@ function determine_file(path, resp, hdrs)
6869
)
6970

7071

71-
# get the extension, if we are going to save it in encoded form.
72-
# unlike a web-browser we don't automatically decompress
73-
if header(resp, "Content-Encoding") == "gzip"
74-
filename *= ".gz"
75-
end
76-
7772
safer_joinpath(path, filename)
7873
else
7974
# We have been given a full filepath
@@ -121,6 +116,11 @@ function download(url::AbstractString, local_path=nothing, headers=Header[]; upd
121116
start_time = now()
122117
prev_time = now()
123118

119+
if header(resp, "Content-Encoding") == "gzip"
120+
stream = GzipDecompressorStream(stream) # auto decoding
121+
total_bytes = NaN # We don't know actual total bytes if the content is zipped.
122+
end
123+
124124
function report_callback()
125125
prev_time = now()
126126
taken_time = (prev_time - start_time).value / 1000 # in seconds

test/download.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ import ..httpbin
7373
# Add gz extension if we are determining the filename
7474
gzip_content_encoding_fn = HTTP.download("https://$httpbin/gzip")
7575
@test isfile(gzip_content_encoding_fn)
76-
@test last(splitext(gzip_content_encoding_fn)) == ".gz"
76+
77+
# Check content auto decoding
78+
open(gzip_content_encoding_fn, "r") do f
79+
@test HTTP.sniff(read(f, String)) == "application/json; charset=utf-8"
80+
end
7781

7882
# But not if the local name is fully given. HTTP#573
7983
mktempdir() do dir

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ include(joinpath(dir, "resources/TestRequest.jl"))
1313
"chunking.jl",
1414
"utils.jl",
1515
"client.jl",
16+
"download.jl",
1617
"multipart.jl",
1718
"parsemultipart.jl",
1819
"sniff.jl",

0 commit comments

Comments
 (0)