Skip to content

Commit 3f06af7

Browse files
authored
Update pages/Decompression.md (#403)
1 parent 80fd49d commit 3f06af7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pages/Decompression.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@ end
2424

2525
This function handles the response back to the blocked process that's waiting for the HTTP response. You'll see that it returns `{:ok, response}` with `response` being a map with `:status`, `:headers`, and `:data` fields.
2626

27-
We need to attempt to decompress the data if the `content-encoding` header is present. We're going to work with `content-encoding`, but the same applies if compression is used in `transfer-encoding`. First, we're going to find the header. Let's add a function to do that:
27+
We need to attempt to decompress the data if the `content-encoding` header is present. We're going to work with `content-encoding`, but the same applies if compression is used in `transfer-encoding`. Let's add a function that finds all applied compression algorithms.
2828

2929
```elixir
3030
# Returns a list of found compressions or [] if none found.
3131
defp get_content_encoding_header(headers) do
32-
Enum.find_value(headers, [], fn {name, value} ->
32+
headers
33+
|> Enum.flat_map([], fn {name, value} ->
3334
if String.downcase(name) == "content-encoding" do
3435
value
3536
|> String.downcase()
3637
|> String.split(",", trim: true)
3738
|> Stream.map(&String.trim/1)
38-
|> Enum.reverse()
3939
else
40-
nil
40+
[]
4141
end
4242
end)
43+
|> Enum.reverse()
4344
end
4445
```
4546

46-
Now we should have a list like `["gzip"]`. We reversed the compression algorithms so that we decompress from the last one to the first one. Let's use this in another function that handles the decompression. Thankfully, Erlang ships with built-in support for gzip and deflate algorithms.
47+
We use a combination of `Enum.flat_map/3` and `String.split/3` because the values can be comma-separated and spread over multiple headers. Now we should have a list like `["gzip"]`. We reversed the compression algorithms so that we decompress from the last one to the first one. Let's use this in another function that handles the decompression. Thankfully, Erlang ships with built-in support for gzip and deflate algorithms.
4748

4849
```elixir
4950
defp decompress_data(data, algorithms) do

0 commit comments

Comments
 (0)