Skip to content

Commit

Permalink
Better compress detection to avoid chunked transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Jan 11, 2017
1 parent a69cea2 commit 0c6d903
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/client.jl
Expand Up @@ -70,7 +70,9 @@ function send!(client::Client, request::Request; history::Vector{Response}=Respo
# ensure all Request options are set, using client.options if necessary
# this works because request.options are null by default whereas client.options always have a default
update!(request.options, client.options)
length(request.body) > request.options.chunksize && (request.options.chunksize = length(request.body) + 1)
# if the provided request body is compressed, avoid any chunked transfer since it ruins the compression scheme
length(request.body) > 3 && iscompressed(String(request.body)[1:4]) &&
length(request.body) > request.options.chunksize && (request.options.chunksize = length(request.body) + 1)
client.logger != STDOUT && (verbose = true)
return scheme(request.uri) == "http" ? send!(client, request, getconn(http, client, request , verbose), history, stream, verbose) :
send!(client, request, getconn(https, client, request, verbose), history, stream, verbose)
Expand Down
6 changes: 6 additions & 0 deletions src/sniff.jl
@@ -1,3 +1,9 @@
# compression detection
const ZIP = UInt8[0x50, 0x4b, 0x03, 0x04]
const GZIP = UInt8[0x1f, 0x8b, 0x08]

iscompressed(bytes) = all(bytes[1:4] .== ZIP) || all(bytes[1:3] .== GZIP)

# Based on the net/http/sniff.go implementation of DetectContentType
# sniff implements the algorithm described
# at http://mimesniff.spec.whatwg.org/ to determine the
Expand Down

0 comments on commit 0c6d903

Please sign in to comment.