Skip to content

Commit b103889

Browse files
authored
Merge pull request #151 from jhonndabi/fix/removes-transfer-encoding-header-from-non-compliant-statuses
Removes transfer-encoding header from responses with non compliant status codes
2 parents 4d8bc8e + d491a13 commit b103889

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/reverse_proxy_plug.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,15 @@ defmodule ReverseProxyPlug do
199199
end
200200

201201
%HTTPoison.AsyncHeaders{headers: headers} ->
202+
additional_headers =
203+
if conn.status >= 200 and conn.status != 204,
204+
do: [{"transfer-encoding", "chunked"}],
205+
else: []
206+
202207
headers
203208
|> normalize_headers
204209
|> Enum.reject(fn {header, _} -> header == "content-length" end)
205-
|> Enum.concat([{"transfer-encoding", "chunked"}])
210+
|> Enum.concat(additional_headers)
206211
|> Enum.reduce(conn, fn {header, value}, conn ->
207212
Conn.put_resp_header(conn, header, value)
208213
end)

test/reverse_proxy_plug_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ defmodule ReverseProxyPlugTest do
152152
"deletes the content-length header"
153153
end
154154

155+
test "does not sets transfer-encoding headers for informational status class" do
156+
ReverseProxyPlug.HTTPClientMock
157+
|> expect(:request, TestReuse.get_stream_responder(%{status_code: 100}))
158+
159+
conn =
160+
conn(:get, "/")
161+
|> ReverseProxyPlug.call(ReverseProxyPlug.init(@opts))
162+
163+
resp_header_names =
164+
conn.resp_headers
165+
|> Enum.map(fn {name, _val} -> name end)
166+
167+
refute "transfer-encoding" in resp_header_names,
168+
"deletes the transfer-encoding header if status class is informational"
169+
end
170+
171+
test "does not sets transfer-encoding headers for no content status code" do
172+
ReverseProxyPlug.HTTPClientMock
173+
|> expect(:request, TestReuse.get_stream_responder(%{status_code: 204}))
174+
175+
conn =
176+
conn(:post, "/", nil)
177+
|> ReverseProxyPlug.call(ReverseProxyPlug.init(@opts))
178+
179+
resp_header_names =
180+
conn.resp_headers
181+
|> Enum.map(fn {name, _val} -> name end)
182+
183+
refute "transfer-encoding" in resp_header_names,
184+
"deletes the transfer-encoding header if status code is no content"
185+
end
186+
155187
test "uses raw_body from assigns if body empty and raw_body present" do
156188
raw_body = "name=Jane"
157189
conn = conn(:post, "/users", nil)

0 commit comments

Comments
 (0)