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

WebSockets: support passing through sec-websocket-protocol header #5904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions lib/phoenix/socket/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ defmodule Phoenix.Socket.Transport do

connect_info =
Enum.map(connect_info, fn
key when key in [:peer_data, :trace_context_headers, :uri, :user_agent, :x_headers] ->
key when key in [:peer_data, :trace_context_headers, :uri, :user_agent, :x_headers, :sec_websocket_protocol] ->
key

{:session, session} ->
Expand All @@ -268,7 +268,7 @@ defmodule Phoenix.Socket.Transport do

other ->
raise ArgumentError,
":connect_info keys are expected to be one of :peer_data, :trace_context_headers, :x_headers, :uri, or {:session, config}, " <>
":connect_info keys are expected to be one of :peer_data, :trace_context_headers, :x_headers, :user_agent, :sec_websocket_protocol, :uri, or {:session, config}, " <>
"optionally followed by custom keyword pairs, got: #{inspect(other)}"
end)

Expand Down Expand Up @@ -458,6 +458,8 @@ defmodule Phoenix.Socket.Transport do

* `:user_agent` - the value of the "user-agent" request header

* `:sec_websocket_protocol` - the value of the "sec-websocket-protocol" header

"""
def connect_info(conn, endpoint, keys) do
for key <- keys, into: %{} do
Expand All @@ -477,6 +479,9 @@ defmodule Phoenix.Socket.Transport do
:user_agent ->
{:user_agent, fetch_user_agent(conn)}

:sec_websocket_protocol ->
{:sec_websocket_protocol, Plug.Conn.get_req_header(conn, "sec-websocket-protocol")}

{:session, session} ->
{:session, connect_session(conn, endpoint, session)}

Expand Down