-
Notifications
You must be signed in to change notification settings - Fork 47
/
handler.ex
168 lines (134 loc) · 4.99 KB
/
handler.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
defmodule Plug.Cowboy.Handler do
@moduledoc false
if Code.ensure_loaded?(:cowboy_websocket) and
function_exported?(:cowboy_websocket, :behaviour_info, 1) do
@behaviour :cowboy_websocket
end
@connection Plug.Cowboy.Conn
@already_sent {:plug_conn, :sent}
def init(req, {plug, opts}) do
conn = @connection.conn(req)
try do
conn
|> plug.call(opts)
|> maybe_send(plug)
|> case do
%Plug.Conn{adapter: {@connection, %{upgrade: {:websocket, websocket_opts}} = req}} = conn ->
{handler, state, connection_opts} = websocket_opts
cowboy_opts =
connection_opts
|> Enum.flat_map(fn
{:timeout, timeout} -> [idle_timeout: timeout]
{:compress, _} = opt -> [opt]
{:max_frame_size, _} = opt -> [opt]
_other -> []
end)
|> Map.new()
handler_opts = Keyword.take(connection_opts, [:fullsweep_after])
triplet = {handler, handler_opts, state}
{:cowboy_websocket, copy_resp_headers(conn, req), triplet, cowboy_opts}
%Plug.Conn{adapter: {@connection, req}} ->
{:ok, req, {plug, opts}}
end
catch
kind, reason ->
exit_on_error(kind, reason, __STACKTRACE__, {plug, :call, [conn, opts]})
after
receive do
@already_sent -> :ok
after
0 -> :ok
end
end
end
defp copy_resp_headers(%Plug.Conn{} = conn, req) do
Enum.reduce(conn.resp_headers, req, fn {key, val}, acc ->
:cowboy_req.set_resp_header(key, val, acc)
end)
end
defp exit_on_error(
:error,
%Plug.Conn.WrapperError{kind: kind, reason: reason, stack: stack},
_stack,
call
) do
exit_on_error(kind, reason, stack, call)
end
defp exit_on_error(:error, value, stack, call) do
exception = Exception.normalize(:error, value, stack)
:erlang.raise(:exit, {{exception, stack}, call}, [])
end
defp exit_on_error(:throw, value, stack, call) do
:erlang.raise(:exit, {{{:nocatch, value}, stack}, call}, [])
end
defp exit_on_error(:exit, value, _stack, call) do
:erlang.raise(:exit, {value, call}, [])
end
defp maybe_send(%Plug.Conn{state: :unset}, _plug), do: raise(Plug.Conn.NotSentError)
defp maybe_send(%Plug.Conn{state: :set} = conn, _plug), do: Plug.Conn.send_resp(conn)
defp maybe_send(%Plug.Conn{} = conn, _plug), do: conn
defp maybe_send(other, plug) do
raise "Cowboy2 adapter expected #{inspect(plug)} to return Plug.Conn but got: " <>
inspect(other)
end
## Websocket callbacks
def websocket_init({handler, process_flags, state}) do
for {key, value} <- process_flags do
:erlang.process_flag(key, value)
end
handle_reply(handler, handler.init(state))
end
def websocket_handle({opcode, payload}, {:sock, handler, state})
when opcode in [:text, :binary] do
handle_reply(handler, handler.handle_in({payload, opcode: opcode}, state))
end
def websocket_handle({opcode, payload}, handler_state) when opcode in [:ping, :pong] do
handle_control_frame({payload, opcode: opcode}, handler_state)
end
def websocket_handle(opcode, handler_state) when opcode in [:ping, :pong] do
handle_control_frame({nil, opcode: opcode}, handler_state)
end
def websocket_handle(_other, handler_state) do
{:ok, handler_state}
end
def websocket_info(message, {:sock, handler, state}) do
handle_reply(handler, handler.handle_info(message, state))
end
defp handle_reply(handler, {:ok, state}), do: {:ok, {:sock, handler, state}}
defp handle_reply(handler, {:push, data, state}), do: {:reply, data, {:sock, handler, state}}
defp handle_reply(handler, {:reply, _status, data, state}),
do: {:reply, data, {:sock, handler, state}}
defp handle_reply(handler, {:stop, _reason, state}), do: {:stop, {:sock, handler, state}}
defp handle_control_frame(payload_with_opts, {:sock, handler, state}) do
reply =
if function_exported?(handler, :handle_control, 2) do
handler.handle_control(payload_with_opts, state)
else
{:ok, state}
end
handle_reply(handler, reply)
end
def terminate({:remote, :closed}, _req, {:sock, handler, state}) do
handler.terminate(:closed, state)
end
def terminate({:remote, code, _}, _req, {:sock, handler, state})
when code in 1000..1003 or code in 1005..1011 or code == 1015 do
handler.terminate(:remote, state)
end
def terminate(:remote, _req, {:sock, handler, state}) do
handler.terminate(:remote, state)
end
def terminate({:error, reason}, _req, {:sock, handler, state}) do
handler.terminate({:error, reason}, state)
end
def terminate(:stop, _req, {:sock, handler, state}) do
handler.terminate(:normal, state)
end
def terminate(reason, _req, {:sock, handler, state}) do
handler.terminate(reason, state)
end
# Note that this terminate/3 function is part of the Cowboy Handler API, not cowboy_websocket
def terminate(_reason, _req, _state) do
:ok
end
end