diff options
Diffstat (limited to 'src/include/microhttpd_ws.h')
-rw-r--r-- | src/include/microhttpd_ws.h | 534 |
1 files changed, 336 insertions, 198 deletions
diff --git a/src/include/microhttpd_ws.h b/src/include/microhttpd_ws.h index bfbd550a..f19c140d 100644 --- a/src/include/microhttpd_ws.h +++ b/src/include/microhttpd_ws.h | |||
@@ -51,29 +51,27 @@ struct MHD_WebSocketStream; | |||
51 | enum MHD_WEBSOCKET_FLAG | 51 | enum MHD_WEBSOCKET_FLAG |
52 | { | 52 | { |
53 | /** | 53 | /** |
54 | * The websocket is used by the server (default). | 54 | * The websocket stream is initialized in server mode (default). |
55 | * Thus all outgoing payload will not be "masked". | 55 | * Thus all outgoing payload will not be "masked". |
56 | * All incoming payload must be masked. | 56 | * All incoming payload must be masked. |
57 | * This cannot be used together with #MHD_WEBSOCKET_FLAG_CLIENT | 57 | * This flag cannot be used together with #MHD_WEBSOCKET_FLAG_CLIENT |
58 | */ | 58 | */ |
59 | MHD_WEBSOCKET_FLAG_SERVER = 0, | 59 | MHD_WEBSOCKET_FLAG_SERVER = 0, |
60 | /** | 60 | /** |
61 | * The websocket is used by the client | 61 | * The websocket stream is initialized in client mode. |
62 | * (not used if you provide the server). | 62 | * You will usually never use that mode in combination with libmicrohttpd, |
63 | * Thus all outgoing payload will be "masked" (XOR-ed with random values). | 63 | * because libmicrohttpd provides a server and not a client. |
64 | * In client mode all outgoing payload will be "masked" | ||
65 | * (XOR-ed with random values). | ||
64 | * All incoming payload must be unmasked. | 66 | * All incoming payload must be unmasked. |
65 | * Please note that this implementation doesn't use a strong random | 67 | * If you use this mode, you must always call #MHD_websocket_stream_init2() |
66 | * number generator for the mask as suggested in RFC6455 10.3, because | 68 | * instead of #MHD_websocket_stream_init(), because you need |
67 | * the main intention of this implementation is the use as server | 69 | * to pass a random number generator callback function for masking. |
68 | * with MHD, which doesn't need masking. | 70 | * This flag cannot be used together with #MHD_WEBSOCKET_FLAG_SERVER |
69 | * Instead a weak random number generator is used (`rand()`). | ||
70 | * You can set the seed for the random number generator | ||
71 | * by calling #MHD_websocket_srand(). | ||
72 | * This cannot be used together with #MHD_WEBSOCKET_FLAG_SERVER | ||
73 | */ | 71 | */ |
74 | MHD_WEBSOCKET_FLAG_CLIENT = 1, | 72 | MHD_WEBSOCKET_FLAG_CLIENT = 1, |
75 | /** | 73 | /** |
76 | * You don't want to get fragmented data while decoding. | 74 | * You don't want to get fragmented data while decoding (default). |
77 | * Fragmented frames will be internally put together until | 75 | * Fragmented frames will be internally put together until |
78 | * they are complete. | 76 | * they are complete. |
79 | * Whether or not data is fragmented is decided | 77 | * Whether or not data is fragmented is decided |
@@ -85,11 +83,11 @@ enum MHD_WEBSOCKET_FLAG | |||
85 | * You want fragmented data, if it appears while decoding. | 83 | * You want fragmented data, if it appears while decoding. |
86 | * You will receive the content of the fragmented frame, | 84 | * You will receive the content of the fragmented frame, |
87 | * but if you are decoding text, you will never get an unfinished | 85 | * but if you are decoding text, you will never get an unfinished |
88 | * UTF-8 sequences (if the sequence appears between two fragments). | 86 | * UTF-8 sequence (if the sequence appears between two fragments). |
89 | * Instead the text will end before the unfinished UTF-8 sequence. | 87 | * Instead the text will end before the unfinished UTF-8 sequence. |
90 | * With the next fragment, which finishes the UTF-8 sequence, | 88 | * With the next fragment, which finishes the UTF-8 sequence, |
91 | * you will get the complete UTF-8 sequence. | 89 | * you will get the complete UTF-8 sequence. |
92 | * This cannot be used together with #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | 90 | * This cannot be used together with #MHD_WEBSOCKET_FLAG_NO_FRAGMENTS |
93 | */ | 91 | */ |
94 | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS = 2, | 92 | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS = 2, |
95 | /** | 93 | /** |
@@ -97,7 +95,7 @@ enum MHD_WEBSOCKET_FLAG | |||
97 | * protocol errors, a matching close frame will automatically | 95 | * protocol errors, a matching close frame will automatically |
98 | * be generated. | 96 | * be generated. |
99 | * The close frame will be returned via the parameters | 97 | * The close frame will be returned via the parameters |
100 | * result and result_len of #MHD_websocket_decode() and | 98 | * `payload` and `payload_len` of #MHD_websocket_decode() and |
101 | * the return value is negative | 99 | * the return value is negative |
102 | * (a value of `enum MHD_WEBSOCKET_STATUS`). | 100 | * (a value of `enum MHD_WEBSOCKET_STATUS`). |
103 | * The generated close frame must be freed by the caller | 101 | * The generated close frame must be freed by the caller |
@@ -140,7 +138,7 @@ enum MHD_WEBSOCKET_FRAGMENTATION | |||
140 | * You want to use fragmentation. | 138 | * You want to use fragmentation. |
141 | * The encoded frame is the last frame of | 139 | * The encoded frame is the last frame of |
142 | * the series of data frames, but also not the first one. | 140 | * the series of data frames, but also not the first one. |
143 | * After this frame, you may send all type of frames again. | 141 | * After this frame, you may send all types of frames again. |
144 | */ | 142 | */ |
145 | MHD_WEBSOCKET_FRAGMENTATION_LAST = 3 | 143 | MHD_WEBSOCKET_FRAGMENTATION_LAST = 3 |
146 | }; | 144 | }; |
@@ -157,18 +155,19 @@ enum MHD_WEBSOCKET_STATUS | |||
157 | * The call succeeded. | 155 | * The call succeeded. |
158 | * For #MHD_websocket_decode() this means that no error occurred, | 156 | * For #MHD_websocket_decode() this means that no error occurred, |
159 | * but also no frame has been completed yet. | 157 | * but also no frame has been completed yet. |
158 | * For other functions this means simply a success. | ||
160 | */ | 159 | */ |
161 | MHD_WEBSOCKET_STATUS_OK = 0, | 160 | MHD_WEBSOCKET_STATUS_OK = 0, |
162 | /** | 161 | /** |
163 | * #MHD_websocket_decode() has decoded a text frame. | 162 | * #MHD_websocket_decode() has decoded a text frame. |
164 | * The parameters result and result_len are filled with the decoded text | 163 | * The parameters `payload` and `payload_len` are filled with |
165 | * (if any). | 164 | * the decoded text (if any). |
166 | */ | 165 | */ |
167 | MHD_WEBSOCKET_STATUS_TEXT_FRAME = 0x1, | 166 | MHD_WEBSOCKET_STATUS_TEXT_FRAME = 0x1, |
168 | /** | 167 | /** |
169 | * #MHD_websocket_decode() has decoded a binary frame. | 168 | * #MHD_websocket_decode() has decoded a binary frame. |
170 | * The parameters result and result_len are filled with the decoded | 169 | * The parameters `payload` and `payload_len` are filled with |
171 | * binary data (if any). | 170 | * the decoded binary data (if any). |
172 | */ | 171 | */ |
173 | MHD_WEBSOCKET_STATUS_BINARY_FRAME = 0x2, | 172 | MHD_WEBSOCKET_STATUS_BINARY_FRAME = 0x2, |
174 | /** | 173 | /** |
@@ -176,12 +175,13 @@ enum MHD_WEBSOCKET_STATUS | |||
176 | * This means you must close the socket using #MHD_upgrade_action() | 175 | * This means you must close the socket using #MHD_upgrade_action() |
177 | * with #MHD_UPGRADE_ACTION_CLOSE. | 176 | * with #MHD_UPGRADE_ACTION_CLOSE. |
178 | * You may respond with a close frame before closing. | 177 | * You may respond with a close frame before closing. |
179 | * The parameters result and result_len are filled with | 178 | * The parameters `payload` and `payload_len` are filled with |
180 | * the close reason (if any). | 179 | * the close reason (if any). |
181 | * The close reason starts with a two byte sequence of close code | 180 | * The close reason starts with a two byte sequence of close code |
182 | * in network byte order (see `enum MHD_WEBSOCKET_CLOSEREASON`). | 181 | * in network byte order (see `enum MHD_WEBSOCKET_CLOSEREASON`). |
183 | * After these two bytes a UTF-8 encoded close reason may follow. | 182 | * After these two bytes a UTF-8 encoded close reason may follow. |
184 | * Compare with result_len to decide whether there is any close reason. | 183 | * You can call #MHD_websocket_split_close_reason() to split that |
184 | * close reason. | ||
185 | */ | 185 | */ |
186 | MHD_WEBSOCKET_STATUS_CLOSE_FRAME = 0x8, | 186 | MHD_WEBSOCKET_STATUS_CLOSE_FRAME = 0x8, |
187 | /** | 187 | /** |
@@ -189,7 +189,7 @@ enum MHD_WEBSOCKET_STATUS | |||
189 | * You should respond to this with a pong frame. | 189 | * You should respond to this with a pong frame. |
190 | * The pong frame must contain the same binary data as | 190 | * The pong frame must contain the same binary data as |
191 | * the corresponding ping frame (if it had any). | 191 | * the corresponding ping frame (if it had any). |
192 | * The parameters result and result_len are filled with | 192 | * The parameters `payload` and `payload_len` are filled with |
193 | * the binary ping data (if any). | 193 | * the binary ping data (if any). |
194 | */ | 194 | */ |
195 | MHD_WEBSOCKET_STATUS_PING_FRAME = 0x9, | 195 | MHD_WEBSOCKET_STATUS_PING_FRAME = 0x9, |
@@ -199,57 +199,79 @@ enum MHD_WEBSOCKET_STATUS | |||
199 | * a ping frame before. | 199 | * a ping frame before. |
200 | * The binary data should be equal to your ping frame and can be | 200 | * The binary data should be equal to your ping frame and can be |
201 | * used to distinguish the response if you sent multiple ping frames. | 201 | * used to distinguish the response if you sent multiple ping frames. |
202 | * The parameters result and result_len are filled with | 202 | * The parameters `payload` and `payload_len` are filled with |
203 | * the binary pong data (if any). | 203 | * the binary pong data (if any). |
204 | */ | 204 | */ |
205 | MHD_WEBSOCKET_STATUS_PONG_FRAME = 0xA, | 205 | MHD_WEBSOCKET_STATUS_PONG_FRAME = 0xA, |
206 | /** | 206 | /** |
207 | * #MHD_websocket_decode() has decoded a text frame fragment. | 207 | * #MHD_websocket_decode() has decoded a text frame fragment. |
208 | * The parameters result and result_len are filled with the decoded text | 208 | * The parameters `payload` and `payload_len` are filled with |
209 | * (if any). | 209 | * the decoded text (if any). |
210 | * This is like #MHD_WEBSOCKET_STATUS_TEXT_FRAME, but it can only | 210 | * This is like #MHD_WEBSOCKET_STATUS_TEXT_FRAME, but it can only |
211 | * appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS during | 211 | * appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS during |
212 | * the call of #MHD_websocket_stream_init() or | 212 | * the call of #MHD_websocket_stream_init() or |
213 | * #MHD_websocket_stream_init2(). | 213 | * #MHD_websocket_stream_init2(). |
214 | */ | 214 | */ |
215 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT = 0x11, | 215 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT = 0x11, |
216 | /** | 216 | /** |
217 | * #MHD_websocket_decode() has decoded a binary frame fragment. | 217 | * #MHD_websocket_decode() has decoded a binary frame fragment. |
218 | * The parameters result and result_len are filled with the decoded | 218 | * The parameters `payload` and `payload_len` are filled with |
219 | * binary data (if any). | 219 | * the decoded binary data (if any). |
220 | * This is like #MHD_WEBSOCKET_STATUS_BINARY_FRAME, but it can only | 220 | * This is like #MHD_WEBSOCKET_STATUS_BINARY_FRAME, but it can only |
221 | * appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS during | 221 | * appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS during |
222 | * the call of #MHD_websocket_stream_init() or | 222 | * the call of #MHD_websocket_stream_init() or |
223 | * #MHD_websocket_stream_init2(). | 223 | * #MHD_websocket_stream_init2(). |
224 | */ | 224 | */ |
225 | MHD_WEBSOCKET_STATUS_BINARY_FRAGMENT = 0x12, | 225 | MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT = 0x12, |
226 | /** | 226 | /** |
227 | * #MHD_websocket_decode() has decoded the last text frame fragment. | 227 | * #MHD_websocket_decode() has decoded the next text frame fragment. |
228 | * The parameters result and result_len are filled with the decoded text | 228 | * The parameters `payload` and `payload_len` are filled with |
229 | * (if any). | 229 | * the decoded text (if any). |
230 | * This is like #MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, but it appears | 230 | * This is like #MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, but it appears |
231 | * only for the last fragment of a series of fragments. | 231 | * only after the first and before the last fragment of a series of fragments. |
232 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | 232 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS |
233 | * during the call of #MHD_websocket_stream_init() or | 233 | * during the call of #MHD_websocket_stream_init() or |
234 | * #MHD_websocket_stream_init2(). | 234 | * #MHD_websocket_stream_init2(). |
235 | */ | 235 | */ |
236 | MHD_WEBSOCKET_STATUS_TEXT_LAST_FRAGMENT = 0x21, | 236 | MHD_WEBSOCKET_STATUS_TEXT_NEXT_FRAGMENT = 0x21, |
237 | /** | 237 | /** |
238 | * #MHD_websocket_decode() has decoded the last binary frame fragment. | 238 | * #MHD_websocket_decode() has decoded the next binary frame fragment. |
239 | * The parameters result and result_len are filled with the decoded | 239 | * The parameters `payload` and `payload_len` are filled with |
240 | * binary data (if any). | 240 | * the decoded binary data (if any). |
241 | * This is like #MHD_WEBSOCKET_STATUS_BINARY_FRAGMENT, but it appears | 241 | * This is like #MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, but it appears |
242 | * only for the last fragment of a series of fragments. | 242 | * only after the first and before the last fragment of a series of fragments. |
243 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | 243 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS |
244 | * during the call of #MHD_websocket_stream_init() or | 244 | * during the call of #MHD_websocket_stream_init() or |
245 | * #MHD_websocket_stream_init2(). | 245 | * #MHD_websocket_stream_init2(). |
246 | */ | 246 | */ |
247 | MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT = 0x22, | 247 | MHD_WEBSOCKET_STATUS_BINARY_NEXT_FRAGMENT = 0x22, |
248 | /** | ||
249 | * #MHD_websocket_decode() has decoded the last text frame fragment. | ||
250 | * The parameters `payload` and `payload_len` are filled with | ||
251 | * the decoded text (if any). | ||
252 | * This is like #MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, but it appears | ||
253 | * only for the last fragment of a series of fragments. | ||
254 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | ||
255 | * during the call of #MHD_websocket_stream_init() or | ||
256 | * #MHD_websocket_stream_init2(). | ||
257 | */ | ||
258 | MHD_WEBSOCKET_STATUS_TEXT_LAST_FRAGMENT = 0x41, | ||
259 | /** | ||
260 | * #MHD_websocket_decode() has decoded the last binary frame fragment. | ||
261 | * The parameters `payload` and `payload_len` are filled with | ||
262 | * the decoded binary data (if any). | ||
263 | * This is like #MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, but it appears | ||
264 | * only for the last fragment of a series of fragments. | ||
265 | * It can only appear if you specified #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | ||
266 | * during the call of #MHD_websocket_stream_init() or | ||
267 | * #MHD_websocket_stream_init2(). | ||
268 | */ | ||
269 | MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT = 0x42, | ||
248 | /** | 270 | /** |
249 | * The call failed and the stream is invalid now for decoding. | 271 | * The call failed and the stream is invalid now for decoding. |
250 | * You must close the websocket now using #MHD_upgrade_action() | 272 | * You must close the websocket now using #MHD_upgrade_action() |
251 | * with #MHD_UPGRADE_ACTION_CLOSE. | 273 | * with #MHD_UPGRADE_ACTION_CLOSE. |
252 | * You can send a close frame before closing. | 274 | * You may send a close frame before closing. |
253 | * This is only used by #MHD_websocket_decode() and happens | 275 | * This is only used by #MHD_websocket_decode() and happens |
254 | * if the stream contains errors (i. e. invalid byte data). | 276 | * if the stream contains errors (i. e. invalid byte data). |
255 | */ | 277 | */ |
@@ -259,10 +281,12 @@ enum MHD_WEBSOCKET_STATUS | |||
259 | * been marked invalid. | 281 | * been marked invalid. |
260 | * You must close the websocket now using #MHD_upgrade_action() | 282 | * You must close the websocket now using #MHD_upgrade_action() |
261 | * with #MHD_UPGRADE_ACTION_CLOSE. | 283 | * with #MHD_UPGRADE_ACTION_CLOSE. |
262 | * You can send a close frame before closing. | 284 | * You may send a close frame before closing. |
263 | * This is only used by #MHD_websocket_decode() and happens | 285 | * This is only used by #MHD_websocket_decode() and happens |
264 | * if you call #MDM_websocket_decode() again after is | 286 | * if you call #MDM_websocket_decode() again after |
265 | * has been invalidated. | 287 | * has been invalidated. |
288 | * You can call #MHD_websocket_stream_is_valid() at any time | ||
289 | * to check whether a stream is invalid or not. | ||
266 | */ | 290 | */ |
267 | MHD_WEBSOCKET_STATUS_STREAM_BROKEN = -2, | 291 | MHD_WEBSOCKET_STATUS_STREAM_BROKEN = -2, |
268 | /** | 292 | /** |
@@ -271,8 +295,8 @@ enum MHD_WEBSOCKET_STATUS | |||
271 | * possible later if enough memory is available. | 295 | * possible later if enough memory is available. |
272 | * This could happen while decoding if you received a too big data frame. | 296 | * This could happen while decoding if you received a too big data frame. |
273 | * You could try to specify max_payload_size during the call of | 297 | * You could try to specify max_payload_size during the call of |
274 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2() then to | 298 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2() to |
275 | * avoid this and close the frame instead. | 299 | * avoid this and close the websocket instead. |
276 | */ | 300 | */ |
277 | MHD_WEBSOCKET_STATUS_MEMORY_ERROR = -3, | 301 | MHD_WEBSOCKET_STATUS_MEMORY_ERROR = -3, |
278 | /** | 302 | /** |
@@ -286,28 +310,39 @@ enum MHD_WEBSOCKET_STATUS | |||
286 | * If you got this return code from #MHD_websocket_decode() then | 310 | * If you got this return code from #MHD_websocket_decode() then |
287 | * the stream becomes invalid and the websocket must be closed | 311 | * the stream becomes invalid and the websocket must be closed |
288 | * using #MHD_upgrade_action() with #MHD_UPGRADE_ACTION_CLOSE. | 312 | * using #MHD_upgrade_action() with #MHD_UPGRADE_ACTION_CLOSE. |
289 | * You can send a close frame before closing. | 313 | * You may send a close frame before closing. |
290 | * The maximum payload size is specified during the call of | 314 | * The maximum payload size is specified during the call of |
291 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2(). | 315 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2(). |
292 | * This can also appear if you specified 0 as maximum payload size | 316 | * This can also appear if you specified 0 as maximum payload size |
293 | * when the message is greater than the maximum allocatable memory size | 317 | * when the message is greater than the maximum allocatable memory size |
294 | * (i. e. more than 4 GB on 32 bit systems). | 318 | * (i. e. more than 4 GiB on 32 bit systems). |
295 | * If you got this return code from #MHD_websocket_encode_close(), | 319 | * If you got this return code from #MHD_websocket_encode_close(), |
296 | * #MHD_websocket_encode_ping() or #MHD_websocket_encode_pong() then | 320 | * #MHD_websocket_encode_ping() or #MHD_websocket_encode_pong() then |
297 | * you passed to much payload data. The stream remains valid then. | 321 | * you passed to much payload data. The stream remains valid then. |
298 | */ | 322 | */ |
299 | MHD_WEBSOCKET_STATUS_MAXIMUM_SIZE_EXCEEDED = -5, | 323 | MHD_WEBSOCKET_STATUS_MAXIMUM_SIZE_EXCEEDED = -5, |
300 | /** | 324 | /** |
301 | * An UTF-8 text is invalid. | 325 | * An UTF-8 sequence is invalid. |
302 | * If you got this return code from #MHD_websocket_decode() then | 326 | * If you got this return code from #MHD_websocket_decode() then |
303 | * the stream becomes invalid and you must close the websocket | 327 | * the stream becomes invalid and you must close the websocket |
304 | * using #MHD_upgrade_action() with #MHD_UPGRADE_ACTION_CLOSE. | 328 | * using #MHD_upgrade_action() with #MHD_UPGRADE_ACTION_CLOSE. |
305 | * You can send a close frame before closing. | 329 | * You may send a close frame before closing. |
306 | * If you got this from #MHD_websocket_encode_text() or | 330 | * If you got this from #MHD_websocket_encode_text() or |
307 | * #MHD_websocket_encode_close() then you passed invalid UTF-8 text. | 331 | * #MHD_websocket_encode_close() then you passed invalid UTF-8 text. |
308 | * The stream remains valid then. | 332 | * The stream remains valid then. |
309 | */ | 333 | */ |
310 | MHD_WEBSOCKET_STATUS_UTF8_ENCODING_ERROR = -6 | 334 | MHD_WEBSOCKET_STATUS_UTF8_ENCODING_ERROR = -6, |
335 | /** | ||
336 | * A check routine for the HTTP headers came to the conclusion that | ||
337 | * the header value isn't valid for a websocket handshake request. | ||
338 | * This value can only be returned from the following functions: | ||
339 | * * #MHD_websocket_check_http_version() | ||
340 | * * #MHD_websocket_check_connection_header() | ||
341 | * * #MHD_websocket_check_upgrade_header() | ||
342 | * * #MHD_websocket_check_version_header() | ||
343 | * * #MHD_websocket_create_accept_header() | ||
344 | */ | ||
345 | MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER = -7 | ||
311 | }; | 346 | }; |
312 | 347 | ||
313 | /** | 348 | /** |
@@ -332,22 +367,22 @@ enum MHD_WEBSOCKET_CLOSEREASON | |||
332 | * This value is used as placeholder for #MHD_websocket_encode_close() | 367 | * This value is used as placeholder for #MHD_websocket_encode_close() |
333 | * to tell that you don't want to specify any reason. | 368 | * to tell that you don't want to specify any reason. |
334 | * If you use this value then no reason text may be used. | 369 | * If you use this value then no reason text may be used. |
335 | * This value cannot a result of decoding, because this value | 370 | * This value cannot be a result of decoding, because this value |
336 | * is not a valid close reason for the WebSocket protocol. | 371 | * is not a valid close reason for the websocket protocol. |
337 | */ | 372 | */ |
338 | MHD_WEBSOCKET_CLOSEREASON_NO_REASON = 0, | 373 | MHD_WEBSOCKET_CLOSEREASON_NO_REASON = 0, |
339 | /** | 374 | /** |
340 | * You close the websocket fulfilled its purpose and shall | 375 | * You close the websocket because it fulfilled its purpose and shall |
341 | * now be closed in a normal, planned way. | 376 | * now be closed in a normal, planned way. |
342 | */ | 377 | */ |
343 | MHD_WEBSOCKET_CLOSEREASON_REGULAR = 1000, | 378 | MHD_WEBSOCKET_CLOSEREASON_REGULAR = 1000, |
344 | /** | 379 | /** |
345 | * You close the websocket because are shutting down the server or | 380 | * You close the websocket because you are shutting down the server or |
346 | * something similar. | 381 | * something similar. |
347 | */ | 382 | */ |
348 | MHD_WEBSOCKET_CLOSEREASON_GOING_AWAY = 1001, | 383 | MHD_WEBSOCKET_CLOSEREASON_GOING_AWAY = 1001, |
349 | /** | 384 | /** |
350 | * You close the websocket because you a protocol error occurred | 385 | * You close the websocket because a protocol error occurred |
351 | * during decoding (i. e. invalid byte data). | 386 | * during decoding (i. e. invalid byte data). |
352 | */ | 387 | */ |
353 | MHD_WEBSOCKET_CLOSEREASON_PROTOCOL_ERROR = 1002, | 388 | MHD_WEBSOCKET_CLOSEREASON_PROTOCOL_ERROR = 1002, |
@@ -371,7 +406,8 @@ enum MHD_WEBSOCKET_CLOSEREASON | |||
371 | */ | 406 | */ |
372 | MHD_WEBSOCKET_CLOSEREASON_POLICY_VIOLATED = 1008, | 407 | MHD_WEBSOCKET_CLOSEREASON_POLICY_VIOLATED = 1008, |
373 | /** | 408 | /** |
374 | * You close the websocket because you received a frame which is too big to process. | 409 | * You close the websocket because you received a frame which is too big |
410 | * to process. | ||
375 | * You can specify the maximum allowed payload size during the call of | 411 | * You can specify the maximum allowed payload size during the call of |
376 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2(). | 412 | * #MHD_websocket_stream_init() or #MHD_websocket_stream_init2(). |
377 | */ | 413 | */ |
@@ -497,61 +533,170 @@ enum MHD_WEBSOCKET_VALIDITY | |||
497 | MHD_WEBSOCKET_VALIDITY_ONLY_VALID_FOR_CONTROL_FRAMES = 2 | 533 | MHD_WEBSOCKET_VALIDITY_ONLY_VALID_FOR_CONTROL_FRAMES = 2 |
498 | }; | 534 | }; |
499 | /** | 535 | /** |
500 | * This method is called by many websocket | 536 | * This callback function is used internally by many websocket functions |
501 | * functions for allocating data. | 537 | * for allocating data. |
502 | * By default 'malloc' is used. | 538 | * By default `malloc()` is used. |
503 | * This can be used for operating systems like Windows | 539 | * You can use your own allocation function with |
504 | * where malloc, realloc and free are compiler dependent. | 540 | * #MHD_websocket_stream_init2() if you wish to. |
541 | * This can be useful for operating systems like Windows | ||
542 | * where `malloc()`, `realloc()` and `free()` are compiler-dependent. | ||
543 | * You can call the associated `malloc()` callback of | ||
544 | * a websocket stream with #MHD_websocket_malloc(). | ||
505 | * | 545 | * |
506 | * @param len new size | 546 | * @param buf_len buffer size in bytes |
507 | * @return allocated memory | 547 | * @return allocated memory |
508 | * @ingroup websocket | 548 | * @ingroup websocket |
509 | */ | 549 | */ |
510 | typedef void* | 550 | typedef void* |
511 | (*MHD_WebSocketMallocCallback) (size_t len); | 551 | (*MHD_WebSocketMallocCallback) (size_t buf_len); |
512 | /** | 552 | /** |
513 | * This method is called by many websocket | 553 | * This callback function is used internally by many websocket |
514 | * functions for reallocating data. | 554 | * functions for reallocating data. |
515 | * By default 'realloc' is used. | 555 | * By default `realloc()` is used. |
516 | * This can be used for operating systems like Windows | 556 | * You can use your own reallocation function with |
517 | * where malloc, realloc and free are compiler dependent. | 557 | * #MHD_websocket_stream_init2() if you wish to. |
558 | * This can be useful for operating systems like Windows | ||
559 | * where `malloc()`, `realloc()` and `free()` are compiler-dependent. | ||
560 | * You can call the associated `realloc()` callback of | ||
561 | * a websocket stream with #MHD_websocket_realloc(). | ||
518 | * | 562 | * |
519 | * @param cls closure | 563 | * @param buf buffer |
520 | * @param len new size | 564 | * @param new_buf_len new buffer size in bytes |
521 | * @return reallocated memory | 565 | * @return reallocated memory |
522 | * @ingroup websocket | 566 | * @ingroup websocket |
523 | */ | 567 | */ |
524 | typedef void* | 568 | typedef void* |
525 | (*MHD_WebSocketReallocCallback) (void *cls, size_t len); | 569 | (*MHD_WebSocketReallocCallback) (void *buf, size_t new_buf_len); |
526 | /** | 570 | /** |
527 | * This method is called by many websocket | 571 | * This callback function is used internally by many websocket |
528 | * functions for freeing data. | 572 | * functions for freeing data. |
529 | * By default 'free' is used. | 573 | * By default `free()` is used. |
530 | * This can be used for operating systems like Windows | 574 | * You can use your own free function with |
531 | * where malloc, realloc and free are compiler dependent. | 575 | * #MHD_websocket_stream_init2() if you wish to. |
576 | * This can be useful for operating systems like Windows | ||
577 | * where `malloc()`, `realloc()` and `free()` are compiler-dependent. | ||
578 | * You can call the associated `free()` callback of | ||
579 | * a websocket stream with #MHD_websocket_free(). | ||
532 | * | 580 | * |
533 | * @param cls closure | 581 | * @param buf buffer |
534 | * @ingroup websocket | 582 | * @ingroup websocket |
535 | */ | 583 | */ |
536 | typedef void | 584 | typedef void |
537 | (*MHD_WebSocketFreeCallback) (void *cls); | 585 | (*MHD_WebSocketFreeCallback) (void *buf); |
586 | /** | ||
587 | * This callback function is used for generating random numbers | ||
588 | * for masking payload data in client mode. | ||
589 | * If you use websockets in server mode with libmicrohttpd then | ||
590 | * you don't need a random number generator, because | ||
591 | * the server doesn't mask its outgoing messageses. | ||
592 | * However if you wish to use a websocket stream in client mode, | ||
593 | * you must pass this callback function to #MHD_websocket_stream_init2(). | ||
594 | * | ||
595 | * @param cls closure specified in #MHD_websocket_stream_init2() | ||
596 | * @param buf buffer to fill with random values | ||
597 | * @param buf_len size of buffer in bytes | ||
598 | * @return The number of generated random bytes. | ||
599 | * Should usually equal to buf_len. | ||
600 | * @ingroup websocket | ||
601 | */ | ||
602 | typedef size_t | ||
603 | (*MHD_WebSocketRandomNumberGenerator) (void *cls, void* buf, size_t buf_len); | ||
604 | |||
605 | /** | ||
606 | * Checks the HTTP version of the incoming request. | ||
607 | * Websocket requests are only allowed for HTTP/1.1 or above. | ||
608 | * | ||
609 | * @param http_version The value of the 'version' parameter of your | ||
610 | * access_handler callback | ||
611 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | ||
612 | * 0 means the HTTP version is correct for a websocket request, | ||
613 | * a value less than zero means that the HTTP version isn't | ||
614 | * valid for a websocket request. | ||
615 | * @ingroup websocket | ||
616 | */ | ||
617 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS | ||
618 | MHD_websocket_check_http_version (const char* http_version); | ||
619 | |||
620 | /** | ||
621 | * Checks the value of the 'Connection' HTTP request header. | ||
622 | * Websocket requests require the token 'Upgrade' in | ||
623 | * the 'Connection' HTTP request header. | ||
624 | * | ||
625 | * @param connection_header The value of the 'Connection' request header. | ||
626 | * You can get this request header value by passing | ||
627 | * #MHD_HTTP_HEADER_CONNECTION to | ||
628 | * #MHD_lookup_connection_value(). | ||
629 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | ||
630 | * 0 means the 'Connection' request header is correct | ||
631 | * for a websocket request, | ||
632 | * a value less than zero means that the 'Connection' header isn't | ||
633 | * valid for a websocket request. | ||
634 | * @ingroup websocket | ||
635 | */ | ||
636 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS | ||
637 | MHD_websocket_check_connection_header (const char* connection_header); | ||
638 | |||
639 | /** | ||
640 | * Checks the value of the 'Upgrade' HTTP request header. | ||
641 | * Websocket requests require the value 'websocket' in | ||
642 | * the 'Upgrade' HTTP request header. | ||
643 | * | ||
644 | * @param upgrade_header The value of the 'Upgrade' request header. | ||
645 | * You can get this request header value by passing | ||
646 | * #MHD_HTTP_HEADER_UPGRADE to | ||
647 | * #MHD_lookup_connection_value(). | ||
648 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | ||
649 | * 0 means the 'Upgrade' request header is correct | ||
650 | * for a websocket request, | ||
651 | * a value less than zero means that the 'Upgrade' header isn't | ||
652 | * valid for a websocket request. | ||
653 | * @ingroup websocket | ||
654 | */ | ||
655 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS | ||
656 | MHD_websocket_check_upgrade_header (const char* upgrade_header); | ||
538 | 657 | ||
539 | /** | 658 | /** |
540 | * Creates the response value for the incoming 'Sec-WebSocket-Key' header. | 659 | * Checks the value of the 'Sec-WebSocket-Version' HTTP request header. |
541 | * The generated value must be sent to the client as 'Sec-WebSocket-Accept' response header. | 660 | * Websocket requests require the value '13' |
661 | * in the 'Sec-WebSocket-Version' HTTP request header. | ||
542 | * | 662 | * |
543 | * @param sec_websocket_key The value of the 'Sec-WebSocket-Key' request header | 663 | * @param version_header The value of the 'Sec-WebSocket-Version' |
664 | * request header. | ||
665 | * You can get this request header value by passing | ||
666 | * #MHD_HTTP_HEADER_SEC_WEBSOCKET_VERSION to | ||
667 | * #MHD_lookup_connection_value(). | ||
668 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | ||
669 | * 0 means the 'Sec-WebSocket-Version' request header is correct | ||
670 | * for a websocket request, | ||
671 | * a value less than zero means that the 'Sec-WebSocket-Version' | ||
672 | * header isn't valid for a websocket request. | ||
673 | * @ingroup websocket | ||
674 | */ | ||
675 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS | ||
676 | MHD_websocket_check_version_header (const char* version_header); | ||
677 | |||
678 | /** | ||
679 | * Creates the response value for the 'Sec-WebSocket-Key' HTTP request header. | ||
680 | * The generated value must be sent to the client | ||
681 | * as 'Sec-WebSocket-Accept' HTTP response header. | ||
682 | * | ||
683 | * @param sec_websocket_key The value of the 'Sec-WebSocket-Key' | ||
684 | * request header. | ||
685 | * You can get this request header value by passing | ||
686 | * #MHD_HTTP_HEADER_SEC_WEBSOCKET_KEY to | ||
687 | * #MHD_lookup_connection_value(). | ||
544 | * @param[out] sec_websocket_accept The response buffer, which will receive | 688 | * @param[out] sec_websocket_accept The response buffer, which will receive |
545 | * the generated 'Sec-WebSocket-Accept' header. | 689 | * the generated 'Sec-WebSocket-Accept' header. |
546 | * This buffer must be at least 29 bytes long and | 690 | * This buffer must be at least 29 bytes long and |
547 | * will contain a terminating NUL character. | 691 | * will contain the response value plus |
692 | * a terminating NUL on success. | ||
548 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 693 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
549 | * Typically 0 on success or less than 0 on errors. | 694 | * Typically 0 on success or less than 0 on errors. |
550 | * @ingroup websocket | 695 | * @ingroup websocket |
551 | */ | 696 | */ |
552 | _MHD_EXTERN int | 697 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
553 | MHD_websocket_create_accept (const char*sec_websocket_key, | 698 | MHD_websocket_create_accept_header (const char* sec_websocket_key, |
554 | char*sec_websocket_accept); | 699 | char* sec_websocket_accept); |
555 | 700 | ||
556 | /** | 701 | /** |
557 | * Creates a new websocket stream, used for decoding/encoding. | 702 | * Creates a new websocket stream, used for decoding/encoding. |
@@ -559,13 +704,13 @@ MHD_websocket_create_accept (const char*sec_websocket_key, | |||
559 | * @param[out] ws The websocket stream | 704 | * @param[out] ws The websocket stream |
560 | * @param flags Combination of `enum MHD_WEBSOCKET_FLAG` values | 705 | * @param flags Combination of `enum MHD_WEBSOCKET_FLAG` values |
561 | * to modify the behavior of the websocket stream. | 706 | * to modify the behavior of the websocket stream. |
562 | * @param max_message_size The maximum size for incoming payload | 707 | * @param max_payload_size The maximum size for incoming payload |
563 | * data in bytes. Use 0 to allow each size. | 708 | * data in bytes. Use 0 to allow each size. |
564 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 709 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
565 | * Typically 0 on success or less than 0 on errors. | 710 | * Typically 0 on success or less than 0 on errors. |
566 | * @ingroup websocket | 711 | * @ingroup websocket |
567 | */ | 712 | */ |
568 | _MHD_EXTERN int | 713 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
569 | MHD_websocket_stream_init (struct MHD_WebSocketStream**ws, | 714 | MHD_websocket_stream_init (struct MHD_WebSocketStream**ws, |
570 | int flags, | 715 | int flags, |
571 | size_t max_payload_size); | 716 | size_t max_payload_size); |
@@ -573,26 +718,42 @@ MHD_websocket_stream_init (struct MHD_WebSocketStream**ws, | |||
573 | /** | 718 | /** |
574 | * Creates a new websocket stream, used for decoding/encoding, | 719 | * Creates a new websocket stream, used for decoding/encoding, |
575 | * but with custom memory functions for malloc, realloc and free. | 720 | * but with custom memory functions for malloc, realloc and free. |
721 | * Also a random number generator can be specified for client mode. | ||
576 | * | 722 | * |
577 | * @param[out] ws The websocket stream | 723 | * @param[out] ws The websocket stream |
578 | * @param flags Combination of `enum MHD_WEBSOCKET_FLAG` values | 724 | * @param flags Combination of `enum MHD_WEBSOCKET_FLAG` values |
579 | * to modify the behavior of the websocket stream. | 725 | * to modify the behavior of the websocket stream. |
580 | * @param max_message_size The maximum size for incoming payload | 726 | * @param max_payload_size The maximum size for incoming payload |
581 | * data in bytes. Use 0 to allow each size. | 727 | * data in bytes. Use 0 to allow each size. |
582 | * @param callback_malloc The callback function for 'malloc'. | 728 | * @param callback_malloc The callback function for `malloc()`. |
583 | * @param callback_realloc The callback function for 'realloc'. | 729 | * @param callback_realloc The callback function for `realloc()`. |
584 | * @param callback_free The callback function for 'free'. | 730 | * @param callback_free The callback function for `free()`. |
731 | * @param cls_rng A closure for the random number generator callback. | ||
732 | * This is only required when | ||
733 | * MHD_WEBSOCKET_FLAG_CLIENT is passed in `flags`. | ||
734 | * The given value is passed to | ||
735 | * the random number generator. | ||
736 | * May be NULL if not needed. | ||
737 | * Should be NULL when you are | ||
738 | * not using MHD_WEBSOCKET_FLAG_CLIENT. | ||
739 | * @param callback_rng A callback function for a | ||
740 | * secure random number generator. | ||
741 | * This is only required when | ||
742 | * MHD_WEBSOCKET_FLAG_CLIENT is passed in `flags`. | ||
743 | * Should be NULL otherwise. | ||
585 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 744 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
586 | * Typically 0 on success or less than 0 on errors. | 745 | * Typically 0 on success or less than 0 on errors. |
587 | * @ingroup websocket | 746 | * @ingroup websocket |
588 | */ | 747 | */ |
589 | _MHD_EXTERN int | 748 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
590 | MHD_websocket_stream_init2 (struct MHD_WebSocketStream**ws, | 749 | MHD_websocket_stream_init2 (struct MHD_WebSocketStream**ws, |
591 | int flags, | 750 | int flags, |
592 | size_t max_payload_size, | 751 | size_t max_payload_size, |
593 | MHD_WebSocketMallocCallback callback_malloc, | 752 | MHD_WebSocketMallocCallback callback_malloc, |
594 | MHD_WebSocketReallocCallback callback_realloc, | 753 | MHD_WebSocketReallocCallback callback_realloc, |
595 | MHD_WebSocketFreeCallback callback_free); | 754 | MHD_WebSocketFreeCallback callback_free, |
755 | void* cls_rng, | ||
756 | MHD_WebSocketRandomNumberGenerator callback_rng); | ||
596 | 757 | ||
597 | /** | 758 | /** |
598 | * Frees a websocket stream | 759 | * Frees a websocket stream |
@@ -602,7 +763,7 @@ MHD_websocket_stream_init2 (struct MHD_WebSocketStream**ws, | |||
602 | * Typically 0 on success or less than 0 on errors. | 763 | * Typically 0 on success or less than 0 on errors. |
603 | * @ingroup websocket | 764 | * @ingroup websocket |
604 | */ | 765 | */ |
605 | _MHD_EXTERN int | 766 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
606 | MHD_websocket_stream_free (struct MHD_WebSocketStream*ws); | 767 | MHD_websocket_stream_free (struct MHD_WebSocketStream*ws); |
607 | 768 | ||
608 | /** | 769 | /** |
@@ -615,7 +776,7 @@ MHD_websocket_stream_free (struct MHD_WebSocketStream*ws); | |||
615 | * Typically 0 on success or less than 0 on errors. | 776 | * Typically 0 on success or less than 0 on errors. |
616 | * @ingroup websocket | 777 | * @ingroup websocket |
617 | */ | 778 | */ |
618 | _MHD_EXTERN int | 779 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
619 | MHD_websocket_stream_invalidate (struct MHD_WebSocketStream*ws); | 780 | MHD_websocket_stream_invalidate (struct MHD_WebSocketStream*ws); |
620 | 781 | ||
621 | /** | 782 | /** |
@@ -627,11 +788,11 @@ MHD_websocket_stream_invalidate (struct MHD_WebSocketStream*ws); | |||
627 | * @return A value of `enum MHD_WEBSOCKET_VALIDITY`. | 788 | * @return A value of `enum MHD_WEBSOCKET_VALIDITY`. |
628 | * @ingroup websocket | 789 | * @ingroup websocket |
629 | */ | 790 | */ |
630 | _MHD_EXTERN int | 791 | _MHD_EXTERN enum MHD_WEBSOCKET_VALIDITY |
631 | MHD_websocket_stream_is_valid (struct MHD_WebSocketStream*ws); | 792 | MHD_websocket_stream_is_valid (struct MHD_WebSocketStream*ws); |
632 | 793 | ||
633 | /** | 794 | /** |
634 | * Decodes a byte sequence via this websocket stream. | 795 | * Decodes a byte sequence for a websocket stream. |
635 | * Decoding is done until either a frame is complete or | 796 | * Decoding is done until either a frame is complete or |
636 | * the end of the byte sequence is reached. | 797 | * the end of the byte sequence is reached. |
637 | * | 798 | * |
@@ -644,11 +805,11 @@ MHD_websocket_stream_is_valid (struct MHD_WebSocketStream*ws); | |||
644 | * than @a streambuf_len when a frame is decoded | 805 | * than @a streambuf_len when a frame is decoded |
645 | * before the end of the buffer is reached. | 806 | * before the end of the buffer is reached. |
646 | * The remaining bytes of @a buf must be passed | 807 | * The remaining bytes of @a buf must be passed |
647 | * in the following decoding. | 808 | * to the next call of this function. |
648 | * @param[out] payload This variable receives a buffer with the decoded | 809 | * @param[out] payload Pointer to a variable, which receives a buffer |
649 | * payload data. | 810 | * with the decoded payload data. |
650 | * If no decoded data is available this is NULL. | 811 | * If no decoded data is available this is NULL. |
651 | * When this variable is not NULL then | 812 | * When the returned value is not NULL then |
652 | * the buffer contains always @a payload_len bytes plus | 813 | * the buffer contains always @a payload_len bytes plus |
653 | * one terminating NUL character. | 814 | * one terminating NUL character. |
654 | * The caller must free this buffer | 815 | * The caller must free this buffer |
@@ -657,11 +818,11 @@ MHD_websocket_stream_is_valid (struct MHD_WebSocketStream*ws); | |||
657 | * #MHD_WEBSOCKET_FLAG_GENERATE_CLOSE_FRAMES_ON_ERROR | 818 | * #MHD_WEBSOCKET_FLAG_GENERATE_CLOSE_FRAMES_ON_ERROR |
658 | * upon creation of this websocket stream and | 819 | * upon creation of this websocket stream and |
659 | * a decoding error occurred | 820 | * a decoding error occurred |
660 | * (return value less than 0), then this | 821 | * (function return value less than 0), then this |
661 | * buffer contains a generated close frame | 822 | * buffer contains a generated close frame |
662 | * which must be sent via the socket to the recipient. | 823 | * which must be sent via the socket to the recipient. |
663 | * If you passed the flag #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS | 824 | * If you passed the flag #MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS |
664 | * upon creation of this websocket stream then | 825 | * upon creation of the websocket stream then |
665 | * this payload may only be a part of the complete message. | 826 | * this payload may only be a part of the complete message. |
666 | * Only complete UTF-8 sequences are returned | 827 | * Only complete UTF-8 sequences are returned |
667 | * for fragmented text frames. | 828 | * for fragmented text frames. |
@@ -670,58 +831,58 @@ MHD_websocket_stream_is_valid (struct MHD_WebSocketStream*ws); | |||
670 | * @param[out] payload_len The length of the result payload buffer in bytes. | 831 | * @param[out] payload_len The length of the result payload buffer in bytes. |
671 | * | 832 | * |
672 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 833 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
673 | * This is greater than 0 if a frame has is complete, equal to 0 if more data | 834 | * This is greater than 0 if a frame has is complete, |
674 | * is needed an less than 0 on errors. | 835 | * equal to 0 if more data is needed an less than 0 on errors. |
675 | * @ingroup websocket | 836 | * @ingroup websocket |
676 | */ | 837 | */ |
677 | _MHD_EXTERN int | 838 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
678 | MHD_websocket_decode (struct MHD_WebSocketStream*ws, | 839 | MHD_websocket_decode (struct MHD_WebSocketStream* ws, |
679 | const char*streambuf, | 840 | const char* streambuf, |
680 | size_t streambuf_len, | 841 | size_t streambuf_len, |
681 | size_t*streambuf_read_len, | 842 | size_t* streambuf_read_len, |
682 | char**payload, | 843 | char** payload, |
683 | size_t*payload_len); | 844 | size_t* payload_len); |
684 | 845 | ||
685 | /** | 846 | /** |
686 | * Splits the payload of of a decoded close frame. | 847 | * Splits the payload of a decoded close frame. |
687 | * | 848 | * |
688 | * @param payload The payload of the close frame. | 849 | * @param payload The payload of the close frame. |
689 | * This parameter may be NULL if @a payload_len is 0. | 850 | * This parameter may only be NULL if @a payload_len is 0. |
690 | * @param payload_len The length of @a payload. | 851 | * @param payload_len The length of @a payload. |
691 | * @param[out] reason_code The numeric close reason. | 852 | * @param[out] reason_code The numeric close reason. |
692 | * If there was no close reason, this is | 853 | * If there was no close reason, this is |
693 | * #MHD_WEBSOCKET_CLOSEREASON_NO_REASON. | 854 | * #MHD_WEBSOCKET_CLOSEREASON_NO_REASON. |
694 | * Compare with `enum MHD_WEBSOCKET_CLOSEREASON`. | 855 | * Compare with `enum MHD_WEBSOCKET_CLOSEREASON`. |
695 | * This parameter is optional and can be NULL. | 856 | * This parameter is optional and may be NULL. |
696 | * @param[out] reason_utf8 The literal close reason. | 857 | * @param[out] reason_utf8 The literal close reason. |
697 | * If there was no literal close reason, this is NULL. | 858 | * If there was no literal close reason, this is NULL. |
698 | * This parameter is optional and can be NULL. | 859 | * This parameter is optional and may be NULL. |
699 | * Please note that no memory is allocated | 860 | * Please note that no memory is allocated |
700 | * in this function. | 861 | * in this function. |
701 | * If not NULL the returned value of this parameter | 862 | * If not NULL the returned value of this parameter |
702 | * points to a position in the specified @a payload. | 863 | * points to a position in the specified @a payload. |
703 | * @param[out] reason_utf8_len The length of the literal close reason. | 864 | * @param[out] reason_utf8_len The length of the literal close reason. |
704 | * If there was no literal close reason, this is 0. | 865 | * If there was no literal close reason, this is 0. |
705 | * This parameter is optional and can be NULL. | 866 | * This parameter is optional and may be NULL. |
706 | * | 867 | * |
707 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 868 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
708 | * This is #MHD_WEBSOCKET_STATUS_OK (= 0) on success | 869 | * This is #MHD_WEBSOCKET_STATUS_OK (= 0) on success |
709 | * or a value less than 0 on errors. | 870 | * or a value less than 0 on errors. |
710 | * @ingroup websocket | 871 | * @ingroup websocket |
711 | */ | 872 | */ |
712 | _MHD_EXTERN int | 873 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
713 | MHD_websocket_split_close_reason (const char*payload, | 874 | MHD_websocket_split_close_reason (const char* payload, |
714 | size_t payload_len, | 875 | size_t payload_len, |
715 | unsigned short*reason_code, | 876 | unsigned short* reason_code, |
716 | const char**reason_utf8, | 877 | const char** reason_utf8, |
717 | size_t*reason_utf8_len); | 878 | size_t* reason_utf8_len); |
718 | 879 | ||
719 | /** | 880 | /** |
720 | * Encodes an UTF-8 encoded text into websocket text frame. | 881 | * Encodes an UTF-8 encoded text into websocket text frame. |
721 | * | 882 | * |
722 | * @param ws The websocket stream. | 883 | * @param ws The websocket stream. |
723 | * @param payload_utf8 The UTF-8 encoded text to send. | 884 | * @param payload_utf8 The UTF-8 encoded text to send. |
724 | * This can be NULL if payload_utf8_len is 0. | 885 | * This may be NULL if payload_utf8_len is 0. |
725 | * @param payload_utf8_len The length of the UTF-8 encoded text in bytes. | 886 | * @param payload_utf8_len The length of the UTF-8 encoded text in bytes. |
726 | * @param fragmentation A value of `enum MHD_WEBSOCKET_FRAGMENTATION` | 887 | * @param fragmentation A value of `enum MHD_WEBSOCKET_FRAGMENTATION` |
727 | * to specify the fragmentation behavior. | 888 | * to specify the fragmentation behavior. |
@@ -735,11 +896,11 @@ MHD_websocket_split_close_reason (const char*payload, | |||
735 | * The caller must free this buffer using #MHD_websocket_free(). | 896 | * The caller must free this buffer using #MHD_websocket_free(). |
736 | * @param[out] frame_len The length of the encoded frame in bytes. | 897 | * @param[out] frame_len The length of the encoded frame in bytes. |
737 | * @param[out] utf8_step This parameter is required for fragmentation and | 898 | * @param[out] utf8_step This parameter is required for fragmentation and |
738 | * can be NULL if no fragmentation is used. | 899 | * should be NULL if no fragmentation is used. |
739 | * It contains information about the last encoded | 900 | * It contains information about the last encoded |
740 | * UTF-8 sequence and is required to continue a previous | 901 | * UTF-8 sequence and is required to continue a previous |
741 | * UTF-8 sequence when fragmentation is used. | 902 | * UTF-8 sequence when fragmentation is used. |
742 | * The `enum MHD_WEBSOCKET_UTF8STEP` is for this. | 903 | * `enum MHD_WEBSOCKET_UTF8STEP` is for this value. |
743 | * If you start a new fragment using | 904 | * If you start a new fragment using |
744 | * MHD_WEBSOCKET_FRAGMENTATION_NONE or | 905 | * MHD_WEBSOCKET_FRAGMENTATION_NONE or |
745 | * MHD_WEBSOCKET_FRAGMENTATION_FIRST the value | 906 | * MHD_WEBSOCKET_FRAGMENTATION_FIRST the value |
@@ -751,17 +912,17 @@ MHD_websocket_split_close_reason (const char*payload, | |||
751 | * or a value less than 0 on errors. | 912 | * or a value less than 0 on errors. |
752 | * @ingroup websocket | 913 | * @ingroup websocket |
753 | */ | 914 | */ |
754 | _MHD_EXTERN int | 915 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
755 | MHD_websocket_encode_text (struct MHD_WebSocketStream*ws, | 916 | MHD_websocket_encode_text (struct MHD_WebSocketStream* ws, |
756 | const char*payload_utf8, | 917 | const char* payload_utf8, |
757 | size_t payload_utf8_len, | 918 | size_t payload_utf8_len, |
758 | int fragmentation, | 919 | int fragmentation, |
759 | char**frame, | 920 | char** frame, |
760 | size_t*frame_len, | 921 | size_t* frame_len, |
761 | int*utf8_step); | 922 | int* utf8_step); |
762 | 923 | ||
763 | /** | 924 | /** |
764 | * Encodes a binary data into websocket binary frame. | 925 | * Encodes binary data into websocket binary frame. |
765 | * | 926 | * |
766 | * @param ws The websocket stream. | 927 | * @param ws The websocket stream. |
767 | * @param payload The binary data to send. | 928 | * @param payload The binary data to send. |
@@ -786,13 +947,13 @@ MHD_websocket_encode_text (struct MHD_WebSocketStream*ws, | |||
786 | * or a value less than 0 on errors. | 947 | * or a value less than 0 on errors. |
787 | * @ingroup websocket | 948 | * @ingroup websocket |
788 | */ | 949 | */ |
789 | _MHD_EXTERN int | 950 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
790 | MHD_websocket_encode_binary (struct MHD_WebSocketStream*ws, | 951 | MHD_websocket_encode_binary (struct MHD_WebSocketStream* ws, |
791 | const char*payload, | 952 | const char* payload, |
792 | size_t payload_len, | 953 | size_t payload_len, |
793 | int fragmentation, | 954 | int fragmentation, |
794 | char**frame, | 955 | char** frame, |
795 | size_t*frame_len); | 956 | size_t* frame_len); |
796 | 957 | ||
797 | /** | 958 | /** |
798 | * Encodes a websocket ping frame | 959 | * Encodes a websocket ping frame |
@@ -815,18 +976,18 @@ MHD_websocket_encode_binary (struct MHD_WebSocketStream*ws, | |||
815 | * or a value less than 0 on errors. | 976 | * or a value less than 0 on errors. |
816 | * @ingroup websocket | 977 | * @ingroup websocket |
817 | */ | 978 | */ |
818 | _MHD_EXTERN int | 979 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
819 | MHD_websocket_encode_ping (struct MHD_WebSocketStream*ws, | 980 | MHD_websocket_encode_ping (struct MHD_WebSocketStream* ws, |
820 | const char*payload, | 981 | const char* payload, |
821 | size_t payload_len, | 982 | size_t payload_len, |
822 | char**frame, | 983 | char** frame, |
823 | size_t*frame_len); | 984 | size_t* frame_len); |
824 | 985 | ||
825 | /** | 986 | /** |
826 | * Encodes a websocket pong frame | 987 | * Encodes a websocket pong frame |
827 | * | 988 | * |
828 | * @param ws The websocket stream. | 989 | * @param ws The websocket stream. |
829 | * @param payload The binary pong payload data, which is typically | 990 | * @param payload The binary pong payload data, which should be |
830 | * the decoded payload from the received ping frame. | 991 | * the decoded payload from the received ping frame. |
831 | * This may be NULL if @a payload_len is 0. | 992 | * This may be NULL if @a payload_len is 0. |
832 | * @param payload_len The length of the payload data in bytes. | 993 | * @param payload_len The length of the payload data in bytes. |
@@ -848,12 +1009,12 @@ MHD_websocket_encode_ping (struct MHD_WebSocketStream*ws, | |||
848 | * or a value less than 0 on errors. | 1009 | * or a value less than 0 on errors. |
849 | * @ingroup websocket | 1010 | * @ingroup websocket |
850 | */ | 1011 | */ |
851 | _MHD_EXTERN int | 1012 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
852 | MHD_websocket_encode_pong (struct MHD_WebSocketStream*ws, | 1013 | MHD_websocket_encode_pong (struct MHD_WebSocketStream* ws, |
853 | const char*payload, | 1014 | const char* payload, |
854 | size_t payload_len, | 1015 | size_t payload_len, |
855 | char**frame, | 1016 | char** frame, |
856 | size_t*frame_len); | 1017 | size_t* frame_len); |
857 | 1018 | ||
858 | /** | 1019 | /** |
859 | * Encodes a websocket close frame | 1020 | * Encodes a websocket close frame |
@@ -890,58 +1051,35 @@ MHD_websocket_encode_pong (struct MHD_WebSocketStream*ws, | |||
890 | * or a value less than 0 on errors. | 1051 | * or a value less than 0 on errors. |
891 | * @ingroup websocket | 1052 | * @ingroup websocket |
892 | */ | 1053 | */ |
893 | _MHD_EXTERN int | 1054 | _MHD_EXTERN enum MHD_WEBSOCKET_STATUS |
894 | MHD_websocket_encode_close (struct MHD_WebSocketStream*ws, | 1055 | MHD_websocket_encode_close (struct MHD_WebSocketStream* ws, |
895 | unsigned short reason_code, | 1056 | unsigned short reason_code, |
896 | const char*reason_utf8, | 1057 | const char* reason_utf8, |
897 | size_t reason_utf8_len, | 1058 | size_t reason_utf8_len, |
898 | char**frame, | 1059 | char** frame, |
899 | size_t*frame_len); | 1060 | size_t* frame_len); |
900 | |||
901 | /** | ||
902 | * Sets the seed for the random number generated used for | ||
903 | * the generation of masked frames (this is only used for client websockets). | ||
904 | * This seed is used for all websocket streams. | ||
905 | * Internally `srand()` is called. | ||
906 | * Please note that on some situations | ||
907 | * (where `rand()` and `srand()` are shared between your program | ||
908 | * and this library) this could cause unwanted results in your program if | ||
909 | * your program relies on a specific seed. | ||
910 | * | ||
911 | * @param seed The seed used for the initialization of | ||
912 | * the pseudo random number generator. | ||
913 | * Typically `time(NULL)` is used here to | ||
914 | * generate a seed. | ||
915 | * | ||
916 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | ||
917 | * This is #MHD_WEBSOCKET_STATUS_OK (= 0) on success | ||
918 | * or a value less than 0 on errors. | ||
919 | * @ingroup websocket | ||
920 | */ | ||
921 | _MHD_EXTERN int | ||
922 | MHD_websocket_srand (unsigned long seed); | ||
923 | 1061 | ||
924 | /** | 1062 | /** |
925 | * Allocates memory with the associated 'malloc' function | 1063 | * Allocates memory with the associated 'malloc' function |
926 | * of the websocket stream | 1064 | * of the websocket stream |
927 | * | 1065 | * |
928 | * @param ws The websocket stream. | 1066 | * @param ws The websocket stream. |
929 | * @param len The length of the memory to allocate in bytes | 1067 | * @param buf_len The length of the memory to allocate in bytes |
930 | * | 1068 | * |
931 | * @return The allocated memory on success or NULL on failure. | 1069 | * @return The allocated memory on success or NULL on failure. |
932 | * @ingroup websocket | 1070 | * @ingroup websocket |
933 | */ | 1071 | */ |
934 | _MHD_EXTERN void* | 1072 | _MHD_EXTERN void* |
935 | MHD_websocket_malloc (struct MHD_WebSocketStream*ws, | 1073 | MHD_websocket_malloc (struct MHD_WebSocketStream* ws, |
936 | size_t len); | 1074 | size_t buf_len); |
937 | 1075 | ||
938 | /** | 1076 | /** |
939 | * Reallocates memory with the associated 'realloc' function | 1077 | * Reallocates memory with the associated 'realloc' function |
940 | * of the websocket stream | 1078 | * of the websocket stream |
941 | * | 1079 | * |
942 | * @param ws The websocket stream. | 1080 | * @param ws The websocket stream. |
943 | * @param cls The previously allocated memory or NULL | 1081 | * @param buf The previously allocated memory or NULL |
944 | * @param len The new length of the memory in bytes | 1082 | * @param new_buf_len The new length of the memory in bytes |
945 | * | 1083 | * |
946 | * @return The allocated memory on success or NULL on failure. | 1084 | * @return The allocated memory on success or NULL on failure. |
947 | * If NULL is returned the previously allocated buffer | 1085 | * If NULL is returned the previously allocated buffer |
@@ -949,16 +1087,16 @@ MHD_websocket_malloc (struct MHD_WebSocketStream*ws, | |||
949 | * @ingroup websocket | 1087 | * @ingroup websocket |
950 | */ | 1088 | */ |
951 | _MHD_EXTERN void* | 1089 | _MHD_EXTERN void* |
952 | MHD_websocket_realloc (struct MHD_WebSocketStream*ws, | 1090 | MHD_websocket_realloc (struct MHD_WebSocketStream* ws, |
953 | void*cls, | 1091 | void* buf, |
954 | size_t len); | 1092 | size_t new_buf_len); |
955 | 1093 | ||
956 | /** | 1094 | /** |
957 | * Frees memory with the associated 'free' function | 1095 | * Frees memory with the associated 'free' function |
958 | * of the websocket stream | 1096 | * of the websocket stream |
959 | * | 1097 | * |
960 | * @param ws The websocket stream. | 1098 | * @param ws The websocket stream. |
961 | * @param cls The previously allocated memory or NULL | 1099 | * @param buf The previously allocated memory or NULL |
962 | * | 1100 | * |
963 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. | 1101 | * @return A value of `enum MHD_WEBSOCKET_STATUS`. |
964 | * This is #MHD_WEBSOCKET_STATUS_OK (= 0) on success | 1102 | * This is #MHD_WEBSOCKET_STATUS_OK (= 0) on success |
@@ -966,8 +1104,8 @@ MHD_websocket_realloc (struct MHD_WebSocketStream*ws, | |||
966 | * @ingroup websocket | 1104 | * @ingroup websocket |
967 | */ | 1105 | */ |
968 | _MHD_EXTERN int | 1106 | _MHD_EXTERN int |
969 | MHD_websocket_free (struct MHD_WebSocketStream*ws, | 1107 | MHD_websocket_free (struct MHD_WebSocketStream* ws, |
970 | void*cls); | 1108 | void* buf); |
971 | 1109 | ||
972 | #if 0 /* keep Emacsens' auto-indent happy */ | 1110 | #if 0 /* keep Emacsens' auto-indent happy */ |
973 | { | 1111 | { |