Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning {:ok, payload} from adapter.inform/3 #1193

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1320,10 +1320,19 @@ defmodule Plug.Conn do
`get_http_protocol/1` to retrieve the protocol and version.
"""
@spec inform(t, status, Keyword.t()) :: t
def inform(%Conn{} = conn, status, headers \\ []) do
def inform(%Conn{adapter: {adapter, _}} = conn, status, headers \\ []) do
status_code = Plug.Conn.Status.code(status)
adapter_inform(conn, status_code, headers)
conn

case adapter_inform(conn, status_code, headers) do
:ok ->
conn

{:ok, payload} ->
put_in(conn.adapter, {adapter, payload})

{:error, :not_supported} ->
conn
Comment on lines +1333 to +1334
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a catch-all clause for backwards compatibility, let me know. If so perhaps with a warning?

Suggested change
{:error, :not_supported} ->
conn
{:error, :not_supported} ->
conn
_other ->
conn

there's no need for a catch-all clause in inform!/3 as it already was rasing.

end
end

@doc """
Expand All @@ -1339,7 +1348,10 @@ defmodule Plug.Conn do
:ok ->
conn

_ ->
{:ok, payload} ->
put_in(conn.adapter, {adapter, payload})

{:error, :not_supported} ->
wojtekmach marked this conversation as resolved.
Show resolved Hide resolved
raise "inform is not supported by #{inspect(adapter)}." <>
"You should either delete the call to `inform!/3` or switch to an " <>
"adapter that does support informational such as Plug.Cowboy"
Expand All @@ -1356,8 +1368,9 @@ defmodule Plug.Conn do
raise AlreadySentError
end

defp adapter_inform(%Conn{adapter: {adapter, payload}}, status, headers),
do: adapter.inform(payload, status, headers)
defp adapter_inform(%Conn{adapter: {adapter, payload}}, status, headers) do
adapter.inform(payload, status, headers)
end

@doc """
Request a protocol upgrade from the underlying adapter.
Expand Down
2 changes: 1 addition & 1 deletion lib/plug/conn/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ defmodule Plug.Conn.Adapter do
should be returned.
"""
@callback inform(payload, status :: Conn.status(), headers :: Keyword.t()) ::
:ok | {:error, term}
:ok | {:ok, payload()} | {:error, term()}

@doc """
Attempt to upgrade the connection with the client.
Expand Down
5 changes: 5 additions & 0 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ defmodule Plug.ConnTest do
)
end

test "inform!/3 performs an informational request" do
conn = conn(:get, "/foo") |> inform!(103, [{"link", "</style.css>; rel=preload; as=style"}])
assert {103, [{"link", "</style.css>; rel=preload; as=style"}]} in sent_informs(conn)
end

test "upgrade_adapter/3 requests an upgrade" do
conn = conn(:get, "/foo") |> upgrade_adapter(:supported, opt: :supported)
assert {:supported, [opt: :supported]} in sent_upgrades(conn)
Expand Down
Loading