-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft: support ingesting gzipped data for logs
Close #1699
- Loading branch information
Showing
9 changed files
with
90 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/logflare_web/controllers/plugs/compressed_body_reader.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Plug.Parsers.CompressedBodyReader do | ||
def read_body(conn, opts \\ []) do | ||
content_encoding = Plug.Conn.get_req_header(conn, "content-encoding") | ||
|
||
with {:ok, body, conn} <- Plug.Conn.read_body(conn, opts) do | ||
case try_decompress(body, content_encoding) do | ||
{:ok, data} -> {:ok, data, conn} | ||
{:more, data} -> {:more, data, conn} | ||
{:error, _} = error -> error | ||
end | ||
end | ||
end | ||
|
||
defp try_decompress(data, []), do: {:ok, data} | ||
defp try_decompress(data, ["gzip"]), do: safe_gunzip(data) | ||
|
||
defp safe_gunzip(data) do | ||
z = :zlib.open() | ||
try do | ||
:zlib.inflateInit(z, 31) | ||
result = :zlib.safeInflate(z, data) | ||
:zlib.inflateEnd(z) | ||
|
||
result | ||
after | ||
:zlib.close(z) | ||
else | ||
{:finished, data} -> {:ok, IO.iodata_to_binary(data)} | ||
{:continue, data} -> {:more, IO.iodata_to_binary(data)} | ||
{:need_dictionary, _, _} -> {:error, :not_supported} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule LogflareWeb.Plugs.CompressedBodyReaderTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
@subject Plug.Parsers.CompressedBodyReader | ||
|
||
doctest @subject | ||
|
||
def conn(body, headers \\ []) do | ||
conn = Plug.Test.conn("POST", "/example", body) | ||
|
||
Enum.reduce(headers, conn, fn {key, value}, conn -> | ||
Plug.Conn.put_req_header(conn, key, value) | ||
end) | ||
end | ||
|
||
property "with no `content-encoding` header data is passed through as is" do | ||
check all(data <- binary()) do | ||
assert {:ok, read, _} = @subject.read_body(conn(data)) | ||
assert read == data | ||
end | ||
end | ||
|
||
property "with `content-encoding: gzip` header data is passed through as is" do | ||
check all(data <- binary()) do | ||
compressed = :zlib.gzip(data) | ||
conn = conn(compressed, [{"content-encoding", "gzip"}]) | ||
|
||
assert {:ok, read, _} = @subject.read_body(conn) | ||
assert read == data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters