aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_event.erl
blob: ab6020b7b928b164b09bf6a349a60b2b54634fc1 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
%% Copyright (c) 2019-2020, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(gun_event).

%% init.

-type init_event() :: #{
	owner := pid(),
	transport := tcp | tls,
	origin_scheme := binary(),
	origin_host := inet:hostname() | inet:ip_address(),
	origin_port := inet:port_number(),
	opts := gun:opts()
}.
-export_type([init_event/0]).

-callback init(init_event(), State) -> State.

%% domain_lookup_start/domain_lookup_end.

-type domain_lookup_event() :: #{
	host := inet:hostname() | inet:ip_address(),
	port := inet:port_number(),
	tcp_opts := [gen_tcp:connect_option()],
	timeout := timeout(),
	lookup_info => gun_tcp:lookup_info(),
	error => any()
}.
-export_type([domain_lookup_event/0]).

-callback domain_lookup_start(domain_lookup_event(), State) -> State.
-callback domain_lookup_end(domain_lookup_event(), State) -> State.

%% connect_start/connect_end.

-type connect_event() :: #{
	lookup_info := gun_tcp:lookup_info(),
	timeout := timeout(),
	socket => inet:socket(),
	protocol => http | http2 | socks | raw, %% Only when transport is tcp.
	error => any()
}.
-export_type([connect_event/0]).

-callback connect_start(connect_event(), State) -> State.
-callback connect_end(connect_event(), State) -> State.

%% tls_handshake_start/tls_handshake_end.
%%
%% These events occur when connecting to a TLS server or when
%% upgrading the connection or stream to use TLS, for example
%% using CONNECT. The stream_ref/reply_to values are only
%% present when the TLS handshake occurs in the scope of a request.

-type tls_handshake_event() :: #{
	stream_ref => gun:stream_ref(),
	reply_to => pid(),
	socket := inet:socket() | ssl:sslsocket() | pid(), %% The socket before/after will be different.
	tls_opts := [ssl:tls_client_option()],
	timeout := timeout(),
	protocol => http | http2 | socks | raw,
	error => any()
}.
-export_type([tls_handshake_event/0]).

-callback tls_handshake_start(tls_handshake_event(), State) -> State.
-callback tls_handshake_end(tls_handshake_event(), State) -> State.

%% request_start/request_headers.

-type request_start_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	function := headers | request | ws_upgrade, %% @todo connect?
	method := iodata(),
	scheme => binary(),
	authority := iodata(),
	path => iodata(),
	headers := [{binary(), iodata()}]
}.
-export_type([request_start_event/0]).

-callback request_start(request_start_event(), State) -> State.
-callback request_headers(request_start_event(), State) -> State.

%% request_end.

-type request_end_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid()
}.
-export_type([request_end_event/0]).

-callback request_end(request_end_event(), State) -> State.

%% push_promise_start.

-type push_promise_start_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid()
}.
-export_type([push_promise_start_event/0]).

-callback push_promise_start(push_promise_start_event(), State) -> State.

%% push_promise_end.

-type push_promise_end_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	%% No stream is created if we receive the push_promise while
	%% in the process of gracefully shutting down the connection.
	%% The promised stream is canceled immediately.
	promised_stream_ref => gun:stream_ref(),
	method := binary(),
	uri := binary(),
	headers := [{binary(), iodata()}]
}.
-export_type([push_promise_end_event/0]).

-callback push_promise_end(push_promise_end_event(), State) -> State.

%% response_start.

-type response_start_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid()
}.
-export_type([response_start_event/0]).

-callback response_start(response_start_event(), State) -> State.

%% response_inform/response_headers.

-type response_headers_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	status := non_neg_integer(),
	headers := [{binary(), binary()}]
}.
-export_type([response_headers_event/0]).

-callback response_inform(response_headers_event(), State) -> State.
-callback response_headers(response_headers_event(), State) -> State.

%% response_trailers.

-type response_trailers_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	headers := [{binary(), binary()}]
}.
-export_type([response_trailers_event/0]).

-callback response_trailers(response_trailers_event(), State) -> State.

%% response_end.

-type response_end_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid()
}.
-export_type([response_end_event/0]).

-callback response_end(response_end_event(), State) -> State.

%% ws_upgrade.
%%
%% This event is a signal that the following request and response
%% result from a gun:ws_upgrade/2,3,4 call.
%%
%% There is no corresponding "end" event. Instead, the success is
%% indicated by a protocol_changed event following the informational
%% response.

-type ws_upgrade_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	opts := gun:ws_opts()
}.
-export_type([ws_upgrade_event/0]).

-callback ws_upgrade(ws_upgrade_event(), State) -> State.

%% ws_recv_frame_start.

-type ws_recv_frame_start_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	frag_state := cow_ws:frag_state(),
	extensions := cow_ws:extensions()
}.
-export_type([ws_recv_frame_start_event/0]).

-callback ws_recv_frame_start(ws_recv_frame_start_event(), State) -> State.

%% ws_recv_frame_header.

-type ws_recv_frame_header_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	frag_state := cow_ws:frag_state(),
	extensions := cow_ws:extensions(),
	type := cow_ws:frame_type(),
	rsv := cow_ws:rsv(),
	len := non_neg_integer(),
	mask_key := cow_ws:mask_key()
}.
-export_type([ws_recv_frame_header_event/0]).

-callback ws_recv_frame_header(ws_recv_frame_header_event(), State) -> State.

%% ws_recv_frame_end.

-type ws_recv_frame_end_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	extensions := cow_ws:extensions(),
	close_code := undefined | cow_ws:close_code(),
	payload := binary()
}.
-export_type([ws_recv_frame_end_event/0]).

-callback ws_recv_frame_end(ws_recv_frame_end_event(), State) -> State.

%% ws_send_frame_start/ws_send_frame_end.

-type ws_send_frame_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	extensions := cow_ws:extensions(),
	frame := gun:ws_frame()
}.
-export_type([ws_send_frame_event/0]).

-callback ws_send_frame_start(ws_send_frame_event(), State) -> State.
-callback ws_send_frame_end(ws_send_frame_event(), State) -> State.

%% protocol_changed.
%%
%% This event can occur either following a successful ws_upgrade
%% event, following a successful CONNECT request or a SOCKS tunnel.

-type protocol_changed_event() :: #{
	stream_ref => gun:stream_ref(),
	protocol := http | http2 | socks | raw | ws
}.
-export_type([protocol_changed_event/0]).

-callback protocol_changed(protocol_changed_event(), State) -> State.

%% origin_changed.

-type origin_changed_event() :: #{
	stream_ref => gun:stream_ref(),
	type := connect | socks5,
	origin_scheme := binary(),
	origin_host := inet:hostname() | inet:ip_address(),
	origin_port := inet:port_number()
}.
-export_type([origin_changed_event/0]).

-callback origin_changed(origin_changed_event(), State) -> State.

%% cancel.
%%
%% In the case of HTTP/1.1 we cannot actually cancel the stream,
%% we only silence the stream to the user. Further response events
%% may therefore be received and they provide a useful metric as
%% these canceled requests monopolize the connection.
%%
%% For HTTP/2 both the client and the server may cancel streams.
%% Events may still occur for a short time after the cancel.

-type cancel_event() :: #{
	stream_ref := gun:stream_ref(),
	reply_to := pid(),
	endpoint := local | remote,
	reason := atom()
}.
-export_type([cancel_event/0]).

-callback cancel(cancel_event(), State) -> State.

%% disconnect.

-type disconnect_event() :: #{
	reason := normal | closed | {error, any()}
}.
-export_type([disconnect_event/0]).

-callback disconnect(disconnect_event(), State) -> State.

%% terminate.

-type terminate_event() :: #{
	state := not_connected
        | domain_lookup | connecting | initial_tls_handshake | tls_handshake
        | connected | connected_data_only | connected_ws_only,
	reason := normal | shutdown | {shutdown, any()} | any()
}.
-export_type([terminate_event/0]).

-callback terminate(terminate_event(), State) -> State.