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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
= gun(3)
== Name
gun - asynchronous HTTP client
== Description
The `gun` module provides an asynchronous interface for
connecting and communicating with Web servers over SPDY,
HTTP or Websocket.
== Types
=== opts() = map()
Configuration for the connection.
The following keys are defined:
connect_timeout => timeout()::
Connection timeout. Defaults to `infinity`.
http_opts => http_opts()::
Options specific to the HTTP protocol. See below.
protocols => [http | spdy]::
Ordered list of preferred protocols. When the transport is tcp,
this list must contain exactly one protocol. When the transport
is ssl, this list must contain at least one protocol and will be
used using the NPN protocol negotiation method. When the server
does not support NPN then http will always be used. Defaults to
[http] when the transport is tcp, and [spdy, http] when the
transport is ssl.
retry => non_neg_integer()::
Number of times Gun will try to reconnect on failure before giving up.
Defaults to 5.
retry_timeout => pos_integer()::
Time between retries in milliseconds. Defaults to 5000.
spdy_opts => spdy_opts()::
Options specific to the SPDY protocol. See below.
trace => boolean()::
Whether to enable `dbg` tracing of the connection process. Should
only be used during debugging. Defaults to false.
transport => tcp | ssl::
Whether to use SSL or plain TCP. The default varies depending on the
port used. Port 443 defaults to ssl. All other ports default to tcp.
transport_opts => proplists:proplist()::
Transport options. They are TCP options or SSL options depending on
the selected transport.
ws_opts => ws_opts()::
Options specific to the Websocket protocol. See below.
=== http_opts() = map()
Configuration for the HTTP protocol.
The following keys are defined:
keepalive => pos_integer()::
Time between pings in milliseconds. Since the HTTP protocol has
no standardized way to ping the server, Gun will simply send an
empty line when the connection is idle. Gun only makes a best
effort here as servers usually have configurable limits to drop
idle connections. Defaults to 5000.
version => 'HTTP/1.1' | 'HTTP/1.0'::
HTTP version to use. Defaults to 'HTTP/1.1'.
=== spdy_opts() = map()
Configuration for the SPDY protocol.
The following keys are defined:
keepalive => pos_integer()::
Time between pings in milliseconds. Defaults to 5000.
=== ws_opts() = map()
Configuration for the Websocket protocol.
The following keys are defined:
compress => boolean()::
Whether to enable permessage-deflate compression. This does
not guarantee that compression will be used as it is the
server that ultimately decides. Defaults to false.
== Messages
Calling functions from this module may result in the following
messages being sent.
=== {gun_up, ConnPid, Protocol}
ConnPid = pid():: The pid of the Gun connection process.
Protocol = http | spdy:: The protocol selected for this connection.
The connection is up.
This message informs the owner process that the connection or
reconnection completed.
The protocol selected during the connection is sent in this
message. It can be used to determine the capabilities of the
server.
Gun will now start processing the messages it received while
waiting for the connection to be up. If this is a reconnection,
then this may not be desirable for all requests. Those requests
should be cancelled when the connection goes down, and any
subsequent messages ignored.
=== {gun_down, ConnPid, Protocol, Reason, KilledStreams, UnprocessedStreams}
ConnPid = pid():: The pid of the Gun connection process.
Protocol = http | spdy | ws:: The protocol in use when the connection was lost.
Reason = normal | closed | {error, atom()}:: The reason for the loss of the connection.
KilledStreams = [reference()]:: List of streams that have been brutally terminated.
UnprocessedStreams = [reference()]:: List of streams that have not been processed by the server.
The connection is down.
This message informs the owner process that the connection is
currently down. Gun will automatically attempt to reconnect
depending on the `retry` and `retry_timeout` options.
The reason of the termination is there for debugging purposes
only. You should not rely on this value to know what streams
were processed or completed.
The _killed streams_ are the active streams that did not complete
before the closing of the connection. Whether they can be retried
safely depends on the protocol used and the idempotence property
of the requests.
The _unprocessed streams_ are streams that the server did not
start processing yet. They may be retried safely depending on
what streams were killed before.
When the connection goes back up, Gun will not attempt to retry
requests. It will also not upgrade to Websocket automatically
if that was the protocol in use when the connection was lost.
=== {gun_push, ConnPid, StreamRef, NewStreamRef, URI, Headers}
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream initiated by the owner process.
NewStreamRef = reference():: Identifier of the stream being pushed.
URI = binary():: URI of the resource.
Headers = [{binary(), binary()}]:: Headers @todo
A resource pushed alongside an HTTP response.
This message can only be sent when the protocol is SPDY.
@todo I fear we also need the scheme; resource is identified by URI
@todo Perhaps we really should send the URI entirely, because cache
@todo relies on URI to work and this feature is for caching...
@todo Not sure why Method is there, spec says it is only for GET
=== {gun_response, ConnPid, StreamRef, IsFin, Status, Headers}
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream initiated by the owner process.
IsFin = fin | nofin:: Whether this message terminates the response.
Status = binary():: Status line for the response.
Headers = [{binary(), binary()}]:: Headers sent with the response.
A response to an HTTP request.
=== {gun_data, ConnPid, StreamRef, IsFin, Data}
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream this data belongs to.
IsFin = fin | nofin:: Whether this message terminates the response.
Data = binary():: Data from the stream.
Data associated with a stream.
The stream in question can be either one initiated by the owner
process or a stream initiated by the server through the push
mechanism. In any case a `gun_response` or a `gun_push` message
will be sent before any `gun_data` message.
=== {gun_error, ConnPid, StreamRef, Reason}
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream this error relates to.
Reason = any():: Error reason.
Stream-specific error.
=== {gun_error, ConnPid, Reason}
ConnPid = pid():: The pid of the Gun connection process.
Reason = any():: Error reason.
General error.
=== {gun_ws_upgrade, ConnPid, ok, Headers}
ConnPid = pid():: The pid of the Gun connection process.
Headers = [{binary(), binary()}]:: Headers sent with the response.
Successful upgrade to the Websocket protocol.
=== {gun_ws, ConnPid, Frame}
ConnPid = pid():: The pid of the Gun connection process.
Frame = @todo:: Frame.
Websocket frame.
== Exports
=== open(Host, Port) -> open(Host, Port, [])
Alias of `gun:open/3`.
=== open(Host, Port, Opts) -> {ok, ConnPid} | {error, Reason}
Host = inet:hostname():: Host to connect to.
Port = inet:port_number():: Port to connect to.
Opts = opts():: Options for this connection.
ConnPid = pid():: The pid of the Gun connection process.
Reason = any():: Error reason. @todo really any?
Open a connection to the given host and port.
=== close(ConnPid) -> ok
ConnPid = pid():: The pid of the Gun connection process.
Brutally close the connection.
=== shutdown(ConnPid) -> ok
ConnPid = pid():: The pid of the Gun connection process.
Gracefully close the connection.
A monitor can be used to be notified when the connection is
effectively closed.
=== delete(ConnPid, Path) -> delete(ConnPid, Path, [])
Alias of `gun:delete/3`.
=== delete(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Delete a resource.
=== get(ConnPid, Path) -> get(ConnPid, Path, [])
Alias of `gun:get/3`.
=== get(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Get a resource.
=== head(ConnPid, Path) -> head(ConnPid, Path, [])
Alias of `gun:head/3`.
=== head(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Get headers of a resource.
This function performs the same operation as `get/{2,3}` except
the server will not send the resource representation, only the
response's status line and headers.
While servers should send the same headers they would if the
request was a GET, like `content-length`, it is not always
the case and differences may exist.
=== options(ConnPid, Path) -> options(ConnPid, Path, [])
Alias of `gun:options/3`.
=== options(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Obtain information about the capabilities of the server or of a resource.
The special path `"*"` can be used to obtain information about
the server as a whole. Any other path will return information
about the resource only.
=== patch(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Request that a set of changes be applied to the resource.
This function expects either `content-length` or `content-type`
to be set to know a body is going to be sent afterwards.
Gun will assume the request has no body otherwise. It is
highly recommended to set both when possible.
The body sent in this request should be a patch document
with instructions on how to update the resource.
You can use the `gun:data/4` function to send the body, if any.
=== patch(ConnPid, Path, Headers, Body) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
Body = iodata():: Body of the request.
StreamRef = reference():: Identifier of the stream for this request.
Request that a set of changes be applied to the resource.
It is highly recommended to set the `content-type` header
to inform the server what media type the body contains.
Gun will automatically set the `content-length` header.
The body sent in this request should be a patch document
with instructions on how to update the resource.
The complete request is sent when calling this function.
It is not possible to stream more of the body after
calling it.
=== post(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Process the enclosed representation according to the resource's own semantics.
This function expects either `content-length` or `content-type`
to be set to know a body is going to be sent afterwards.
Gun will assume the request has no body otherwise. It is
highly recommended to set both when possible.
The body sent in this request will be processed
according to the resource's own semantics. A new
resource may be created as a result, and may be
located at a different URI.
You can use the `gun:data/4` function to send the body, if any.
=== post(ConnPid, Path, Headers, Body) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
Body = iodata():: Body of the request.
StreamRef = reference():: Identifier of the stream for this request.
Process the enclosed representation according to the resource's own semantics.
It is highly recommended to set the `content-type` header
to inform the server what media type the body contains.
Gun will automatically set the `content-length` header.
The body sent in this request will be processed
according to the resource's own semantics. A new
resource may be created as a result, and may be
located at a different URI.
The complete request is sent when calling this function.
It is not possible to stream more of the body after
calling it.
=== put(ConnPid, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Create or replace a resource.
The body of the request is the entire representation of the resource.
This function expects either `content-length` or `content-type`
to be set to know a body is going to be sent afterwards.
Gun will assume the request has no body otherwise. It is
highly recommended to set both when possible.
You can use the `gun:data/4` function to send the body, if any.
=== put(ConnPid, Path, Headers, Body) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
Body = iodata():: Body of the request.
StreamRef = reference():: Identifier of the stream for this request.
Create or replace a resource.
The body of the request is the entire representation of the resource.
It is highly recommended to set the `content-type` header
to inform the server what media type the body contains.
Gun will automatically set the `content-length` header.
The complete request is sent when calling this function.
It is not possible to stream more of the body after
calling it.
=== request(ConnPid, Method, Path, Headers) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Method = iodata():: Request method.
Path = iodata():: Path of the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
StreamRef = reference():: Identifier of the stream for this request.
Perform the given request.
This is a general purpose function that should only be used
when existing method-specific functions don't apply.
This function expects either `content-length` or `content-type`
to be set to know a body is going to be sent afterwards.
Gun will assume the request has no body otherwise. It is
highly recommended to set both when possible.
You can use the `gun:data/4` function to send the body, if any.
=== request(ConnPid, Method, Path, Headers, Body) -> StreamRef
ConnPid = pid():: The pid of the Gun connection process.
Method = iodata():: Request method.
Path = iodata():: Path of the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
Body = iodata():: Body of the request.
StreamRef = reference():: Identifier of the stream for this request.
Perform the given request.
This is a general purpose function that should only be used
when existing method-specific functions don't apply.
It is highly recommended to set the `content-type` header
to inform the server what media type the body contains.
Gun will automatically set the `content-length` header.
The complete request is sent when calling this function.
It is not possible to stream more of the body after
calling it.
=== data(ConnPid, StreamRef, IsFin, Data) -> ok
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream this data belongs to.
IsFin = fin | nofin:: Whether this message terminates the request.
Data = iodata():: Data to be sent with the request.
Stream the body of a request.
@todo empty chunks
This function can only be used if the request identified by
`StreamRef` came with headers indicating the presence of a
body and that body not being given when creating the request.
All calls to this function must use `nofin` except for the
last which must use `fin` to indicate the end of the request
body.
Empty data is allowed regardless of the value of `IsFin`.
Gun will not send empty data chunks unless required to
indicate the request body is finished, however.
=== await(ConnPid, StreamRef) -> await(ConnPid, StreamRef, 5000, MonitorRef)
Alias of `gun:await/4`.
A monitor `MonitorRef` is automatically created for the duration of
this call and an error will be returned if the Gun connection process
terminates.
=== await(ConnPid, StreamRef, MonitorRef) -> await(ConnPid, StreamRef, 5000, MonitorRef)
Alias of `gun:await/4`.
=== await(ConnPid, StreamRef, Timeout) -> await(ConnPid, StreamRef, Timeout, MonitorRef)
Alias of `gun:await/4`.
A monitor `MonitorRef` is automatically created for the duration of
this call and an error will be returned if the Gun connection process
terminates.
=== await(ConnPid, StreamRef, Timeout, MonitorRef) -> tuple() -- see below
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream to await messages from.
Timeout = timeout():: How long this function will wait for messages.
MonitorRef = reference():: Monitor reference for the Gun connection process.
Wait for a response message.
This function can be used when a synchronous handling of
responses is desired. It will only return when a message
for the given stream is received, on error or on timeout.
The return values are described in the next few subsections.
==== {response, IsFin, Status, Headers}
IsFin = fin | nofin:: Whether this message terminates the response.
Status = binary():: Status line for the response.
Headers = [{binary(), binary()}]:: Headers sent with the response.
Equivalent of a `gun_response` message.
==== {data, IsFin, Data}
IsFin = fin | nofin:: Whether this message terminates the response.
Data = binary():: Data from the stream.
Equivalent of a `gun_data` message.
==== {push, NewStreamRef, URI, Headers}
NewStreamRef = reference():: Identifier of the stream being pushed.
URI = binary():: URI of the resource.
Headers = [{binary(), binary()}]:: Headers @todo
Equivalent of a `gun_push` message.
@todo Same changes as gun_push
==== {error, Reason}
Reason = any():: Error reason. @todo any?
Equivalent of a `gun_error` message.
@todo I think we want to distinguish a stream error, a general error,
@todo a DOWN and a timeout error
=== await_body(ConnPid, StreamRef) -> await_body(ConnPid, StreamRef, 5000, MonitorRef)
Alias of `gun:await_body/4`.
A monitor `MonitorRef` is automatically created for the duration of
this call and an error will be returned if the Gun connection process
terminates.
=== await_body(ConnPid, StreamRef, MonitorRef) -> await_body(ConnPid, StreamRef, 5000, MonitorRef)
Alias of `gun:await_body/4`.
=== await_body(ConnPid, StreamRef, Timeout) -> await_body(ConnPid, StreamRef, Timeout, MonitorRef)
Alias of `gun:await_body/4`.
A monitor `MonitorRef` is automatically created for the duration of
this call and an error will be returned if the Gun connection process
terminates.
=== await_body(ConnPid, StreamRef, Timeout, MonitorRef) -> {ok, Body} | {error, Reason}
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream to await messages from.
Timeout = timeout():: How long this function will wait for each message.
MonitorRef = reference():: Monitor reference for the Gun connection process.
Body = binary():: Body for the given stream.
Reason = any():: Error reason. @todo any?
Wait for a response body.
This function can be used when a synchronous handling of
responses is desired. It will only return when it has
finished fetching the entire response body.
The timeout value is *per message*. The actual function call
can last much longer for large bodies.
@todo I think we want to distinguish a stream error, a general error,
@todo a DOWN and a timeout error
@todo guide might be a little incorrect about await/await_body
=== flush(ConnPid) -> ok
ConnPid = pid():: The pid of the Gun connection process.
Flush all messages from the Gun connection process from the mailbox.
=== flush(StreamRef) -> ok
StreamRef = reference():: Stream identifier.
Flush all messages related to the given stream.
=== cancel(ConnPid, StreamRef) -> ok
ConnPid = pid():: The pid of the Gun connection process.
StreamRef = reference():: Identifier of the stream to cancel.
Cancel the given stream.
HTTP/1.1 streams can't be cancelled. Gun will simply silence
the stream and stop relaying messages.
@todo Depending on the length
@todo of a response Gun may also attempt to reconnect rather than
@todo receive the entire response body.
SPDY streams can however be cancelled at any time.
=== ws_upgrade(ConnPid, Path) -> ws_upgrade(ConnPid, Path, [], #{})
Alias of `gun:ws_upgrade/3`.
=== ws_upgrade(ConnPid, Path, Headers) -> ok
Similar to `gun:ws_upgrade/4`, except `WsOpts` is taken from
the options given in the `gun:open/{2,3}` call when opening
the connection.
=== ws_upgrade(ConnPid, Path, Headers, WsOpts) -> ok
ConnPid = pid():: The pid of the Gun connection process.
Path = iodata():: Path to the resource.
Headers = [{binary(), iodata()}]:: Additional request headers.
WsOpts = map():: Options for the Websocket connection.
Request the connection to be upgraded to the Websocket protocol.
This function can only be used when the current protocol is `http`.
=== ws_send(ConnPid, Frames) -> ok
ConnPid = pid():: The pid of the Gun connection process.
Frames = @todo:: @todo
Send one or more Websocket frames.
This function can only be used following a successful `ws_upgrade` call.
|