Skip to content

Commit

Permalink
feat: improve decompression middleware
Browse files Browse the repository at this point in the history
closes #598
  • Loading branch information
yordis committed Aug 10, 2023
1 parent cfee71b commit d921af3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/tesla/middleware/compression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ defmodule Tesla.Middleware.Compression do
def decompress({:error, reason}), do: {:error, reason}

def decompress(env) do
body = decompress_body(env.body, Tesla.get_header(env, "content-encoding"))

content_length =
body
|> byte_size()
|> to_string()

env
|> Tesla.put_body(decompress_body(env.body, Tesla.get_header(env, "content-encoding")))
|> Tesla.put_body(body)
|> Tesla.delete_header("content-encoding")
|> Tesla.put_header("content-length", content_length)
end

defp decompress_body(<<31, 139, 8, _::binary>> = body, "gzip"), do: :zlib.gunzip(body)
Expand Down

0 comments on commit d921af3

Please sign in to comment.