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
|
gun
===
The `gun` module provides an asynchronous interface for
connecting and communicating with Web servers over SPDY,
HTTP or Websocket.
Types
-----
### opts() = [{keepalive, pos_integer()}
| {retry, non_neg_integer()}
| {retry_timeout, pos_integer()}
| {type, ssl | tcp | tcp_spdy}].
> Configuration for the connection.
Option descriptions
-------------------
The default value is given next to the option name.
- keepalive (5000)
- Time between pings in milliseconds.
- retry (5)
- Number of times Gun will try to reconnect on failure before giving up.
- retry_timeout (5000)
- Time between retries in milliseconds.
- type (ssl)
- Whether to use SSL, plain TCP (for HTTP/Websocket) or SPDY over TCP.
Exports
-------
### open(Host, Port) -> open(Host, Port, [])
### open(Host, Port, Opts) -> {ok, ServerPid} | {error, any()}
> Types:
> * Host = inet:hostname()
> * Port = inet:port_number()
> * Opts = opts()
> * ServerPid = pid()
>
> Open a connection to the given host.
### close(ServerPid) -> ok
> Types:
> * ServerPid = pid()
>
> Brutally close the connection.
### shutdown(ServerPid) -> ok
> Types:
> * ServerPid = pid()
>
> Gracefully close the connection.
>
> A monitor can be used to be notified when the connection is
> effectively closed.
### delete(ServerPid, Path) -> delete(ServerPid, Path, [])
### delete(ServerPid, Path, Headers) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
>
> Delete a resource.
### get(ServerPid, Path) -> delete(ServerPid, Path, [])
### get(ServerPid, Path, Headers) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
>
> Fetch a resource.
### head(ServerPid, Path) -> delete(ServerPid, Path, [])
### head(ServerPid, Path, Headers) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
>
> Fetch a resource's headers.
>
> The server will not send the resource content, only headers.
### options(ServerPid, Path) -> delete(ServerPid, Path, [])
### options(ServerPid, Path, Headers) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
>
> Obtain information about the capabilities of the server or 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(ServerPid, Path, Headers) -> StreamRef
### patch(ServerPid, Path, Headers, Body) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
> * Body = iodata()
>
> Partially update a resource.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.
### post(ServerPid, Path, Headers) -> StreamRef
### post(ServerPid, Path, Headers, Body) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
> * Body = iodata()
>
> Create or update a resource.
>
> The resource may be created at a different URL than the one
> given.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.
### put(ServerPid, Path, Headers) -> StreamRef
### put(ServerPid, Path, Headers, Body) -> StreamRef
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
> * Body = iodata()
>
> Create or update a resource.
>
> The resource will be created at this exact URL.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.
### request(ServerPid, Method, Path, Headers) -> StreamRef
### request(ServerPid, Method, Path, Headers, Body) -> StreamRef
> Types:
> * ServerPid = pid()
> * Method = iodata()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
> * StreamRef = reference()
> * Body = iodata()
>
> Perform the given request on a resource.
>
> This is a general purpose function that should only be used
> when no other function can be used.
>
> Method names are case sensitive.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.
### data(ServerPid, StreamRef, IsFin, Data) -> ok
> Types:
> * ServerPid = pid()
> * StreamRef = reference()
> * IsFin = fin | nofin
> * Data = iodata()
>
> Stream data.
>
> The `StreamRef` argument is the one returned by any of the
> request functions beforehand and uniquely identifies a request.
>
> Use `nofin` for all chunks except the last which should be `fin`.
> The last chunk may be empty.
### cancel(ServerPid, StreamRef) -> ok
> Types:
> * ServerPid = pid()
> * StreamRef = reference()
>
> Cancel the given stream.
>
> The `StreamRef` argument is the one returned by any of the
> request functions beforehand and uniquely identifies a request.
>
> This function will do a best effort at canceling the stream,
> depending on the capabilities of the protocol.
### ws_upgrade(ServerPid, Path) -> ws_upgrade(ServerPid, Path, [])
### ws_upgrade(ServerPid, Path, Headers) -> ok
> Types:
> * ServerPid = pid()
> * Path = iodata()
> * Headers = [{iodata(), iodata()}]
>
> Upgrade the connection to Websocket.
### ws_send(ServerPid, Frames) -> ok
> Types:
> * ServerPid = pid()
> * Frames = ws_frame() | [ws_frame()]
>
> Send a Websocket frame.
|