Skip to content

Commit

Permalink
Check legacy headers when streaming headers (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
gBillal authored Nov 12, 2024
1 parent 1c8ec06 commit a3c48b8
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/req/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ defmodule Req.Finch do
{:cont, {req, %{resp | status: status}}}

{:headers, fields}, {req, resp} ->
fields = finch_fields_to_map(fields)
resp = update_in(resp.headers, &Map.merge(&1, fields))
resp =
Enum.reduce(fields, resp, fn {name, value}, resp ->
Req.Response.put_header(resp, name, value)
end)

{:cont, {req, resp}}

{:data, data}, acc ->
Expand Down Expand Up @@ -136,8 +139,11 @@ defmodule Req.Finch do
{acc, req, %{resp | status: status}}

{:headers, fields}, {acc, req, resp} ->
fields = finch_fields_to_map(fields)
resp = update_in(resp.headers, &Map.merge(&1, fields))
resp =
Enum.reduce(fields, resp, fn {name, value}, resp ->
Req.Response.put_header(resp, name, value)
end)

{acc, req, resp}

{:data, data}, {acc, req, resp} ->
Expand Down Expand Up @@ -186,9 +192,7 @@ defmodule Req.Finch do
{^ref, message} ->
{:headers, headers} = message

Enum.reduce(headers, %{}, fn {name, value}, acc ->
Map.update(acc, name, [value], &(&1 ++ [value]))
end)
handle_finch_headers(headers)
end

async = %Req.Response.Async{
Expand Down Expand Up @@ -218,9 +222,7 @@ defmodule Req.Finch do
# TODO: handle trailers
{:headers, headers} = message

Enum.reduce(headers, %{}, fn {name, value}, acc ->
Map.update(acc, name, [value], &(&1 ++ [value]))
end)
handle_finch_headers(headers)
end

async = %Req.Response.Async{
Expand Down Expand Up @@ -268,6 +270,12 @@ defmodule Req.Finch do
end)
end

if Req.MixProject.legacy_headers_as_lists?() do
defp handle_finch_headers(headers), do: headers
else
defp handle_finch_headers(headers), do: finch_fields_to_map(headers)
end

defp finch_fields_to_map(fields) do
Enum.reduce(fields, %{}, fn {name, value}, acc ->
Map.update(acc, name, [value], &(&1 ++ [value]))
Expand Down

0 comments on commit a3c48b8

Please sign in to comment.