Skip to content

Commit

Permalink
Merge pull request #151 from jhonndabi/fix/removes-transfer-encoding-…
Browse files Browse the repository at this point in the history
…header-from-non-compliant-statuses

Removes transfer-encoding header from responses with non compliant status codes
  • Loading branch information
mwhitworth authored Oct 25, 2022
2 parents 4d8bc8e + d491a13 commit b103889
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/reverse_proxy_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,15 @@ defmodule ReverseProxyPlug do
end

%HTTPoison.AsyncHeaders{headers: headers} ->
additional_headers =
if conn.status >= 200 and conn.status != 204,
do: [{"transfer-encoding", "chunked"}],
else: []

headers
|> normalize_headers
|> Enum.reject(fn {header, _} -> header == "content-length" end)
|> Enum.concat([{"transfer-encoding", "chunked"}])
|> Enum.concat(additional_headers)
|> Enum.reduce(conn, fn {header, value}, conn ->
Conn.put_resp_header(conn, header, value)
end)
Expand Down
32 changes: 32 additions & 0 deletions test/reverse_proxy_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,38 @@ defmodule ReverseProxyPlugTest do
"deletes the content-length header"
end

test "does not sets transfer-encoding headers for informational status class" do
ReverseProxyPlug.HTTPClientMock
|> expect(:request, TestReuse.get_stream_responder(%{status_code: 100}))

conn =
conn(:get, "/")
|> ReverseProxyPlug.call(ReverseProxyPlug.init(@opts))

resp_header_names =
conn.resp_headers
|> Enum.map(fn {name, _val} -> name end)

refute "transfer-encoding" in resp_header_names,
"deletes the transfer-encoding header if status class is informational"
end

test "does not sets transfer-encoding headers for no content status code" do
ReverseProxyPlug.HTTPClientMock
|> expect(:request, TestReuse.get_stream_responder(%{status_code: 204}))

conn =
conn(:post, "/", nil)
|> ReverseProxyPlug.call(ReverseProxyPlug.init(@opts))

resp_header_names =
conn.resp_headers
|> Enum.map(fn {name, _val} -> name end)

refute "transfer-encoding" in resp_header_names,
"deletes the transfer-encoding header if status code is no content"
end

test "uses raw_body from assigns if body empty and raw_body present" do
raw_body = "name=Jane"
conn = conn(:post, "/users", nil)
Expand Down

0 comments on commit b103889

Please sign in to comment.