Skip to content

Commit

Permalink
Support :session in Cowboy 1
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Jun 1, 2019
1 parent a50dded commit ec99b02
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
40 changes: 2 additions & 38 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ defmodule Phoenix.Endpoint do

paths =
if websocket do
config = socket_config(websocket, Phoenix.Transports.WebSocket)
config = Phoenix.Socket.Transport.load_config(websocket, Phoenix.Transports.WebSocket)
{conn_ast, match_path} = socket_path(path, config)
[{match_path, :websocket, conn_ast, socket, config} | paths]
else
Expand All @@ -683,7 +683,7 @@ defmodule Phoenix.Endpoint do

paths =
if longpoll do
config = socket_config(longpoll, Phoenix.Transports.LongPoll)
config = Phoenix.Socket.Transport.load_config(longpoll, Phoenix.Transports.LongPoll)
plug_init = {endpoint, socket, config}
{conn_ast, match_path} = socket_path(path, config)
[{match_path, :plug, conn_ast, Phoenix.Transports.LongPoll, plug_init} | paths]
Expand Down Expand Up @@ -718,42 +718,6 @@ defmodule Phoenix.Endpoint do
{conn_ast, path}
end

defp socket_config(true, module),
do: module.default_config()

defp socket_config(config, module),
do: module.default_config() |> Keyword.merge(config) |> validate_config()

defp validate_config(config) do
{connect_info, config} = Keyword.pop(config, :connect_info, [])

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

{:session, session} ->
{:session, init_session(session)}

{_, _} = pair ->
pair

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

[connect_info: connect_info] ++ config
end

defp init_session(session) do
key = Keyword.fetch!(session, :key)
store = Plug.Session.Store.get(Keyword.fetch!(session, :store))
init = store.init(Keyword.drop(session, [:store, :key]))
{key, store, init}
end

## API

@doc """
Expand Down
7 changes: 2 additions & 5 deletions lib/phoenix/endpoint/cowboy_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule Phoenix.Endpoint.CowboyAdapter do

paths =
if websocket do
config = socket_config(websocket, Phoenix.Transports.WebSocket)
config = Phoenix.Socket.Transport.load_config(websocket, Phoenix.Transports.WebSocket)
init = {endpoint, socket, config}

[
Expand All @@ -118,7 +118,7 @@ defmodule Phoenix.Endpoint.CowboyAdapter do

paths =
if longpoll do
config = socket_config(longpoll, Phoenix.Transports.LongPoll)
config = Phoenix.Socket.Transport.load_config(longpoll, Phoenix.Transports.LongPoll)
init = {endpoint, socket, config}

[
Expand All @@ -138,9 +138,6 @@ defmodule Phoenix.Endpoint.CowboyAdapter do
"/" <> Path.join(parts)
end

defp socket_config(true, module), do: module.default_config()
defp socket_config(config, module), do: Keyword.merge(module.default_config(), config)

@doc false
def start_link(scheme, endpoint, {m, f, [ref | _] = a}) do
# ref is used by Ranch to identify its listeners, defaulting
Expand Down
37 changes: 37 additions & 0 deletions lib/phoenix/socket/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,43 @@ defmodule Phoenix.Socket.Transport do

require Logger

@doc false
def load_config(true, module),
do: module.default_config()

def load_config(config, module),
do: module.default_config() |> Keyword.merge(config) |> validate_config()

defp validate_config(config) do
{connect_info, config} = Keyword.pop(config, :connect_info, [])

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

{:session, session} ->
{:session, init_session(session)}

{_, _} = pair ->
pair

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

[connect_info: connect_info] ++ config
end

defp init_session(session) do
key = Keyword.fetch!(session, :key)
store = Plug.Session.Store.get(Keyword.fetch!(session, :store))
init = store.init(Keyword.drop(session, [:store, :key]))
{key, store, init}
end

@doc """
Runs the code reloader if enabled.
"""
Expand Down

0 comments on commit ec99b02

Please sign in to comment.