Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | |
27 | | #ifdef CURL_DISABLE_HTTP |
28 | | /* no WebSockets without HTTP present */ |
29 | | #undef CURL_DISABLE_WEBSOCKETS |
30 | | #define CURL_DISABLE_WEBSOCKETS 1 |
31 | | #endif |
32 | | |
33 | | #ifndef CURL_DISABLE_WEBSOCKETS |
34 | | |
35 | | #include "url.h" |
36 | | #include "bufq.h" |
37 | | #include "curlx/dynbuf.h" |
38 | | #include "rand.h" |
39 | | #include "curlx/base64.h" |
40 | | #include "connect.h" |
41 | | #include "sendf.h" |
42 | | #include "curl_trc.h" |
43 | | #include "multiif.h" |
44 | | #include "ws.h" |
45 | | #include "easyif.h" |
46 | | #include "transfer.h" |
47 | | #include "select.h" |
48 | | #include "curlx/strparse.h" |
49 | | #include "curlx/strcopy.h" |
50 | | |
51 | | /*** |
52 | | RFC 6455 Section 5.2 |
53 | | |
54 | | 0 1 2 3 4 5 6 7 |
55 | | +-+-+-+-+-------+ |
56 | | |F|R|R|R| opcode| |
57 | | |I|S|S|S| (4) | |
58 | | |N|V|V|V| | |
59 | | | |1|2|3| | |
60 | | */ |
61 | 0 | #define WSBIT_FIN (0x80) |
62 | 0 | #define WSBIT_RSV1 (0x40) |
63 | 0 | #define WSBIT_RSV2 (0x20) |
64 | 0 | #define WSBIT_RSV3 (0x10) |
65 | 0 | #define WSBIT_RSV_MASK (WSBIT_RSV1 | WSBIT_RSV2 | WSBIT_RSV3) |
66 | 0 | #define WSBIT_OPCODE_CONT (0x0) |
67 | 0 | #define WSBIT_OPCODE_TEXT (0x1) |
68 | 0 | #define WSBIT_OPCODE_BIN (0x2) |
69 | 0 | #define WSBIT_OPCODE_CLOSE (0x8) |
70 | 0 | #define WSBIT_OPCODE_PING (0x9) |
71 | 0 | #define WSBIT_OPCODE_PONG (0xa) |
72 | 0 | #define WSBIT_OPCODE_MASK (0xf) |
73 | | |
74 | 0 | #define WSBIT_MASK 0x80 |
75 | | |
76 | | /* buffer dimensioning */ |
77 | 2 | #define WS_CHUNK_SIZE 65535 |
78 | 2 | #define WS_CHUNK_COUNT 2 |
79 | | |
80 | | /* a client-side WS frame decoder, parsing frame headers and |
81 | | * payload, keeping track of current position and stats */ |
82 | | enum ws_dec_state { |
83 | | WS_DEC_INIT, |
84 | | WS_DEC_HEAD, |
85 | | WS_DEC_PAYLOAD |
86 | | }; |
87 | | |
88 | | struct ws_decoder { |
89 | | int frame_age; /* zero */ |
90 | | int frame_flags; /* See the CURLWS_* defines */ |
91 | | curl_off_t payload_offset; /* the offset parsing is at */ |
92 | | curl_off_t payload_len; |
93 | | uint8_t head[10]; |
94 | | int head_len, head_total; |
95 | | enum ws_dec_state state; |
96 | | int cont_flags; |
97 | | }; |
98 | | |
99 | | /* a client-side WS frame encoder, generating frame headers and |
100 | | * converting payloads, tracking remaining data in current frame */ |
101 | | struct ws_encoder { |
102 | | curl_off_t payload_len; /* payload length of current frame */ |
103 | | curl_off_t payload_remain; /* remaining payload of current */ |
104 | | unsigned int xori; /* xor index */ |
105 | | uint8_t mask[4]; /* 32-bit mask for this connection */ |
106 | | uint8_t firstbyte; /* first byte of frame we encode */ |
107 | | BIT(contfragment); /* set TRUE if the previous fragment sent was not final */ |
108 | | }; |
109 | | |
110 | | /* Control frames are allowed up to 125 characters, rfc6455, ch. 5.5 */ |
111 | 0 | #define WS_MAX_CNTRL_LEN 125 |
112 | | |
113 | | struct ws_cntrl_frame { |
114 | | unsigned int type; |
115 | | size_t payload_len; |
116 | | uint8_t payload[WS_MAX_CNTRL_LEN]; |
117 | | }; |
118 | | |
119 | | /* A websocket connection with en- and decoder that treat frames |
120 | | * and keep track of boundaries. */ |
121 | | struct websocket { |
122 | | struct Curl_easy *data; /* used for write callback handling */ |
123 | | struct ws_decoder dec; /* decode of we frames */ |
124 | | struct ws_encoder enc; /* decode of we frames */ |
125 | | struct bufq recvbuf; /* raw data from the server */ |
126 | | struct bufq sendbuf; /* raw data to be sent to the server */ |
127 | | struct curl_ws_frame recvframe; /* the current WS FRAME received */ |
128 | | struct ws_cntrl_frame pending; /* a control frame pending to be sent */ |
129 | | size_t sendbuf_payload; /* number of payload bytes in sendbuf */ |
130 | | }; |
131 | | |
132 | | #ifdef CURLVERBOSE |
133 | | static const char *ws_frame_name_of_op(uint8_t firstbyte) |
134 | 0 | { |
135 | 0 | switch(firstbyte & WSBIT_OPCODE_MASK) { |
136 | 0 | case WSBIT_OPCODE_CONT: |
137 | 0 | return "CONT"; |
138 | 0 | case WSBIT_OPCODE_TEXT: |
139 | 0 | return "TEXT"; |
140 | 0 | case WSBIT_OPCODE_BIN: |
141 | 0 | return "BIN"; |
142 | 0 | case WSBIT_OPCODE_CLOSE: |
143 | 0 | return "CLOSE"; |
144 | 0 | case WSBIT_OPCODE_PING: |
145 | 0 | return "PING"; |
146 | 0 | case WSBIT_OPCODE_PONG: |
147 | 0 | return "PONG"; |
148 | 0 | default: |
149 | 0 | return "???"; |
150 | 0 | } |
151 | 0 | } |
152 | | #endif |
153 | | |
154 | | static int ws_frame_firstbyte2flags(struct Curl_easy *data, |
155 | | uint8_t firstbyte, int cont_flags) |
156 | 0 | { |
157 | 0 | switch(firstbyte) { |
158 | | /* 0x00 - intermediate TEXT/BINARY fragment */ |
159 | 0 | case WSBIT_OPCODE_CONT: |
160 | 0 | if(!(cont_flags & CURLWS_CONT)) { |
161 | 0 | failf(data, "[WS] no ongoing fragmented message to resume"); |
162 | 0 | return 0; |
163 | 0 | } |
164 | 0 | return cont_flags | CURLWS_CONT; |
165 | | /* 0x80 - final TEXT/BIN fragment */ |
166 | 0 | case (WSBIT_OPCODE_CONT | WSBIT_FIN): |
167 | 0 | if(!(cont_flags & CURLWS_CONT)) { |
168 | 0 | failf(data, "[WS] no ongoing fragmented message to resume"); |
169 | 0 | return 0; |
170 | 0 | } |
171 | 0 | return cont_flags & ~CURLWS_CONT; |
172 | | /* 0x01 - first TEXT fragment */ |
173 | 0 | case WSBIT_OPCODE_TEXT: |
174 | 0 | if(cont_flags & CURLWS_CONT) { |
175 | 0 | failf(data, "[WS] fragmented message interrupted by new TEXT msg"); |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 | return CURLWS_TEXT | CURLWS_CONT; |
179 | | /* 0x81 - unfragmented TEXT msg */ |
180 | 0 | case (WSBIT_OPCODE_TEXT | WSBIT_FIN): |
181 | 0 | if(cont_flags & CURLWS_CONT) { |
182 | 0 | failf(data, "[WS] fragmented message interrupted by new TEXT msg"); |
183 | 0 | return 0; |
184 | 0 | } |
185 | 0 | return CURLWS_TEXT; |
186 | | /* 0x02 - first BINARY fragment */ |
187 | 0 | case WSBIT_OPCODE_BIN: |
188 | 0 | if(cont_flags & CURLWS_CONT) { |
189 | 0 | failf(data, "[WS] fragmented message interrupted by new BINARY msg"); |
190 | 0 | return 0; |
191 | 0 | } |
192 | 0 | return CURLWS_BINARY | CURLWS_CONT; |
193 | | /* 0x82 - unfragmented BINARY msg */ |
194 | 0 | case (WSBIT_OPCODE_BIN | WSBIT_FIN): |
195 | 0 | if(cont_flags & CURLWS_CONT) { |
196 | 0 | failf(data, "[WS] fragmented message interrupted by new BINARY msg"); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 0 | return CURLWS_BINARY; |
200 | | /* 0x08 - first CLOSE fragment */ |
201 | 0 | case WSBIT_OPCODE_CLOSE: |
202 | 0 | failf(data, "[WS] invalid fragmented CLOSE frame"); |
203 | 0 | return 0; |
204 | | /* 0x88 - unfragmented CLOSE */ |
205 | 0 | case (WSBIT_OPCODE_CLOSE | WSBIT_FIN): |
206 | 0 | return CURLWS_CLOSE; |
207 | | /* 0x09 - first PING fragment */ |
208 | 0 | case WSBIT_OPCODE_PING: |
209 | 0 | failf(data, "[WS] invalid fragmented PING frame"); |
210 | 0 | return 0; |
211 | | /* 0x89 - unfragmented PING */ |
212 | 0 | case (WSBIT_OPCODE_PING | WSBIT_FIN): |
213 | 0 | return CURLWS_PING; |
214 | | /* 0x0a - first PONG fragment */ |
215 | 0 | case WSBIT_OPCODE_PONG: |
216 | 0 | failf(data, "[WS] invalid fragmented PONG frame"); |
217 | 0 | return 0; |
218 | | /* 0x8a - unfragmented PONG */ |
219 | 0 | case (WSBIT_OPCODE_PONG | WSBIT_FIN): |
220 | 0 | return CURLWS_PONG; |
221 | | /* invalid first byte */ |
222 | 0 | default: |
223 | 0 | if(firstbyte & WSBIT_RSV_MASK) |
224 | | /* any of the reserved bits 0x40/0x20/0x10 are set */ |
225 | 0 | failf(data, "[WS] invalid reserved bits: %02x", firstbyte); |
226 | 0 | else |
227 | | /* any of the reserved opcodes 0x3-0x7 or 0xb-0xf is used */ |
228 | 0 | failf(data, "[WS] invalid opcode: %02x", firstbyte); |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | | static CURLcode ws_frame_flags2firstbyte(struct Curl_easy *data, |
234 | | unsigned int flags, |
235 | | bool contfragment, |
236 | | uint8_t *pfirstbyte) |
237 | 0 | { |
238 | 0 | *pfirstbyte = 0; |
239 | 0 | switch(flags & ~CURLWS_OFFSET) { |
240 | 0 | case 0: |
241 | 0 | if(contfragment) { |
242 | 0 | CURL_TRC_WS(data, "no flags given; interpreting as continuation " |
243 | 0 | "fragment for compatibility"); |
244 | 0 | *pfirstbyte = (WSBIT_OPCODE_CONT | WSBIT_FIN); |
245 | 0 | return CURLE_OK; |
246 | 0 | } |
247 | 0 | failf(data, "[WS] no flags given"); |
248 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
249 | 0 | case CURLWS_CONT: |
250 | 0 | if(contfragment) { |
251 | 0 | infof(data, "[WS] setting CURLWS_CONT flag without message type is " |
252 | 0 | "supported for compatibility but highly discouraged"); |
253 | 0 | *pfirstbyte = WSBIT_OPCODE_CONT; |
254 | 0 | return CURLE_OK; |
255 | 0 | } |
256 | 0 | failf(data, "[WS] No ongoing fragmented message to continue"); |
257 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
258 | 0 | case CURLWS_TEXT: |
259 | 0 | *pfirstbyte = contfragment ? (WSBIT_OPCODE_CONT | WSBIT_FIN) |
260 | 0 | : (WSBIT_OPCODE_TEXT | WSBIT_FIN); |
261 | 0 | return CURLE_OK; |
262 | 0 | case (CURLWS_TEXT | CURLWS_CONT): |
263 | 0 | *pfirstbyte = contfragment ? WSBIT_OPCODE_CONT : WSBIT_OPCODE_TEXT; |
264 | 0 | return CURLE_OK; |
265 | 0 | case CURLWS_BINARY: |
266 | 0 | *pfirstbyte = contfragment ? (WSBIT_OPCODE_CONT | WSBIT_FIN) |
267 | 0 | : (WSBIT_OPCODE_BIN | WSBIT_FIN); |
268 | 0 | return CURLE_OK; |
269 | 0 | case (CURLWS_BINARY | CURLWS_CONT): |
270 | 0 | *pfirstbyte = contfragment ? WSBIT_OPCODE_CONT : WSBIT_OPCODE_BIN; |
271 | 0 | return CURLE_OK; |
272 | 0 | case CURLWS_CLOSE: |
273 | 0 | *pfirstbyte = WSBIT_OPCODE_CLOSE | WSBIT_FIN; |
274 | 0 | return CURLE_OK; |
275 | 0 | case (CURLWS_CLOSE | CURLWS_CONT): |
276 | 0 | failf(data, "[WS] CLOSE frame must not be fragmented"); |
277 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
278 | 0 | case CURLWS_PING: |
279 | 0 | *pfirstbyte = WSBIT_OPCODE_PING | WSBIT_FIN; |
280 | 0 | return CURLE_OK; |
281 | 0 | case (CURLWS_PING | CURLWS_CONT): |
282 | 0 | failf(data, "[WS] PING frame must not be fragmented"); |
283 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
284 | 0 | case CURLWS_PONG: |
285 | 0 | *pfirstbyte = WSBIT_OPCODE_PONG | WSBIT_FIN; |
286 | 0 | return CURLE_OK; |
287 | 0 | case (CURLWS_PONG | CURLWS_CONT): |
288 | 0 | failf(data, "[WS] PONG frame must not be fragmented"); |
289 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
290 | 0 | default: |
291 | 0 | failf(data, "[WS] unknown flags: %x", flags); |
292 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | static void ws_dec_info(struct ws_decoder *dec, struct Curl_easy *data, |
297 | | const char *msg) |
298 | 0 | { |
299 | 0 | NOVERBOSE((void)msg); |
300 | 0 | switch(dec->head_len) { |
301 | 0 | case 0: |
302 | 0 | break; |
303 | 0 | case 1: |
304 | 0 | CURL_TRC_WS(data, "decoded %s [%s%s]", msg, |
305 | 0 | ws_frame_name_of_op(dec->head[0]), |
306 | 0 | (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL"); |
307 | 0 | break; |
308 | 0 | default: |
309 | 0 | if(dec->head_len < dec->head_total) { |
310 | 0 | CURL_TRC_WS(data, "decoded %s [%s%s](%d/%d)", msg, |
311 | 0 | ws_frame_name_of_op(dec->head[0]), |
312 | 0 | (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL", |
313 | 0 | dec->head_len, dec->head_total); |
314 | 0 | } |
315 | 0 | else { |
316 | 0 | CURL_TRC_WS(data, "decoded %s [%s%s payload=%" |
317 | 0 | FMT_OFF_T "/%" FMT_OFF_T "]", |
318 | 0 | msg, ws_frame_name_of_op(dec->head[0]), |
319 | 0 | (dec->head[0] & WSBIT_FIN) ? "" : " NON-FINAL", |
320 | 0 | dec->payload_offset, dec->payload_len); |
321 | 0 | } |
322 | 0 | break; |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | static CURLcode ws_send_raw_blocking(struct Curl_easy *data, |
327 | | struct websocket *ws, |
328 | | const char *buffer, size_t buflen); |
329 | | |
330 | | typedef CURLcode ws_write_payload(const uint8_t *buf, size_t buflen, |
331 | | int frame_age, int frame_flags, |
332 | | curl_off_t payload_offset, |
333 | | curl_off_t payload_len, |
334 | | void *userp, |
335 | | size_t *pnwritten); |
336 | | |
337 | | static void ws_dec_next_frame(struct ws_decoder *dec) |
338 | 0 | { |
339 | 0 | dec->frame_age = 0; |
340 | 0 | dec->frame_flags = 0; |
341 | 0 | dec->payload_offset = 0; |
342 | 0 | dec->payload_len = 0; |
343 | 0 | dec->head_len = dec->head_total = 0; |
344 | 0 | dec->state = WS_DEC_INIT; |
345 | | /* dec->cont_flags must be carried over to next frame */ |
346 | 0 | } |
347 | | |
348 | | static void ws_dec_reset(struct ws_decoder *dec) |
349 | 1 | { |
350 | 1 | dec->frame_age = 0; |
351 | 1 | dec->frame_flags = 0; |
352 | 1 | dec->payload_offset = 0; |
353 | 1 | dec->payload_len = 0; |
354 | 1 | dec->head_len = dec->head_total = 0; |
355 | 1 | dec->state = WS_DEC_INIT; |
356 | 1 | dec->cont_flags = 0; |
357 | 1 | } |
358 | | |
359 | | static void ws_dec_init(struct ws_decoder *dec) |
360 | 1 | { |
361 | 1 | ws_dec_reset(dec); |
362 | 1 | } |
363 | | |
364 | | static CURLcode ws_dec_read_head(struct ws_decoder *dec, |
365 | | struct Curl_easy *data, |
366 | | struct bufq *inraw) |
367 | 0 | { |
368 | 0 | const uint8_t *inbuf; |
369 | 0 | size_t inlen; |
370 | |
|
371 | 0 | while(Curl_bufq_peek(inraw, &inbuf, &inlen)) { |
372 | 0 | if(dec->head_len == 0) { |
373 | 0 | dec->head[0] = *inbuf; |
374 | 0 | Curl_bufq_skip(inraw, 1); |
375 | |
|
376 | 0 | dec->frame_flags = ws_frame_firstbyte2flags(data, dec->head[0], |
377 | 0 | dec->cont_flags); |
378 | 0 | if(!dec->frame_flags) { |
379 | 0 | ws_dec_reset(dec); |
380 | 0 | return CURLE_RECV_ERROR; |
381 | 0 | } |
382 | | |
383 | | /* fragmentation only applies to data frames (text/binary); |
384 | | * control frames (close/ping/pong) do not affect the CONT status */ |
385 | 0 | if(dec->frame_flags & (CURLWS_TEXT | CURLWS_BINARY)) { |
386 | 0 | dec->cont_flags = dec->frame_flags; |
387 | 0 | } |
388 | |
|
389 | 0 | dec->head_len = 1; |
390 | | /* ws_dec_info(dec, data, "seeing opcode"); */ |
391 | 0 | continue; |
392 | 0 | } |
393 | 0 | else if(dec->head_len == 1) { |
394 | 0 | dec->head[1] = *inbuf; |
395 | 0 | Curl_bufq_skip(inraw, 1); |
396 | 0 | dec->head_len = 2; |
397 | |
|
398 | 0 | if(dec->head[1] & WSBIT_MASK) { |
399 | | /* A client MUST close a connection if it detects a masked frame. */ |
400 | 0 | failf(data, "[WS] masked input frame"); |
401 | 0 | ws_dec_reset(dec); |
402 | 0 | return CURLE_RECV_ERROR; |
403 | 0 | } |
404 | 0 | if(dec->frame_flags & CURLWS_PING && dec->head[1] > WS_MAX_CNTRL_LEN) { |
405 | | /* The maximum valid size of PING frames is 125 bytes. |
406 | | Accepting overlong pings would mean sending equivalent pongs! */ |
407 | 0 | failf(data, "[WS] received PING frame is too big"); |
408 | 0 | ws_dec_reset(dec); |
409 | 0 | return CURLE_RECV_ERROR; |
410 | 0 | } |
411 | 0 | if(dec->frame_flags & CURLWS_PONG && dec->head[1] > WS_MAX_CNTRL_LEN) { |
412 | | /* The maximum valid size of PONG frames is 125 bytes. */ |
413 | 0 | failf(data, "[WS] received PONG frame is too big"); |
414 | 0 | ws_dec_reset(dec); |
415 | 0 | return CURLE_RECV_ERROR; |
416 | 0 | } |
417 | 0 | if(dec->frame_flags & CURLWS_CLOSE && dec->head[1] > WS_MAX_CNTRL_LEN) { |
418 | 0 | failf(data, "[WS] received CLOSE frame is too big"); |
419 | 0 | ws_dec_reset(dec); |
420 | 0 | return CURLE_RECV_ERROR; |
421 | 0 | } |
422 | | |
423 | | /* How long is the frame head? */ |
424 | 0 | if(dec->head[1] == 126) { |
425 | 0 | dec->head_total = 4; |
426 | 0 | continue; |
427 | 0 | } |
428 | 0 | else if(dec->head[1] == 127) { |
429 | 0 | dec->head_total = 10; |
430 | 0 | continue; |
431 | 0 | } |
432 | 0 | else { |
433 | 0 | dec->head_total = 2; |
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | 0 | if(dec->head_len < dec->head_total) { |
438 | 0 | dec->head[dec->head_len] = *inbuf; |
439 | 0 | Curl_bufq_skip(inraw, 1); |
440 | 0 | ++dec->head_len; |
441 | 0 | if(dec->head_len < dec->head_total) { |
442 | | /* ws_dec_info(dec, data, "decoding head"); */ |
443 | 0 | continue; |
444 | 0 | } |
445 | 0 | } |
446 | | /* got the complete frame head */ |
447 | 0 | DEBUGASSERT(dec->head_len == dec->head_total); |
448 | 0 | switch(dec->head_total) { |
449 | 0 | case 2: |
450 | 0 | dec->payload_len = dec->head[1]; |
451 | 0 | break; |
452 | 0 | case 4: |
453 | 0 | dec->payload_len = (dec->head[2] << 8) | dec->head[3]; |
454 | 0 | break; |
455 | 0 | case 10: |
456 | 0 | if(dec->head[2] > 127) { |
457 | 0 | failf(data, "[WS] frame length longer than 63 bits not supported"); |
458 | 0 | return CURLE_RECV_ERROR; |
459 | 0 | } |
460 | 0 | dec->payload_len = |
461 | 0 | (curl_off_t)dec->head[2] << 56 | |
462 | 0 | (curl_off_t)dec->head[3] << 48 | |
463 | 0 | (curl_off_t)dec->head[4] << 40 | |
464 | 0 | (curl_off_t)dec->head[5] << 32 | |
465 | 0 | (curl_off_t)dec->head[6] << 24 | |
466 | 0 | (curl_off_t)dec->head[7] << 16 | |
467 | 0 | (curl_off_t)dec->head[8] << 8 | |
468 | 0 | dec->head[9]; |
469 | 0 | break; |
470 | 0 | default: |
471 | | /* this should never happen */ |
472 | 0 | DEBUGASSERT(0); |
473 | 0 | failf(data, "[WS] unexpected frame header length"); |
474 | 0 | return CURLE_RECV_ERROR; |
475 | 0 | } |
476 | | |
477 | 0 | dec->frame_age = 0; |
478 | 0 | dec->payload_offset = 0; |
479 | 0 | ws_dec_info(dec, data, "decoded"); |
480 | 0 | return CURLE_OK; |
481 | 0 | } |
482 | 0 | return CURLE_AGAIN; |
483 | 0 | } |
484 | | |
485 | | static CURLcode ws_dec_pass_payload(struct ws_decoder *dec, |
486 | | struct Curl_easy *data, |
487 | | struct bufq *inraw, |
488 | | ws_write_payload *write_cb, |
489 | | void *write_ctx) |
490 | 0 | { |
491 | 0 | const uint8_t *inbuf; |
492 | 0 | size_t inlen; |
493 | 0 | size_t nwritten; |
494 | 0 | CURLcode result; |
495 | 0 | size_t remain = curlx_sotouz_range(dec->payload_len - dec->payload_offset, |
496 | 0 | 0, SIZE_MAX); |
497 | |
|
498 | 0 | while(remain && Curl_bufq_peek(inraw, &inbuf, &inlen)) { |
499 | 0 | if(inlen > remain) |
500 | 0 | inlen = remain; |
501 | 0 | result = write_cb(inbuf, inlen, dec->frame_age, dec->frame_flags, |
502 | 0 | dec->payload_offset, dec->payload_len, |
503 | 0 | write_ctx, &nwritten); |
504 | 0 | if(result) |
505 | 0 | return result; |
506 | 0 | Curl_bufq_skip(inraw, nwritten); |
507 | 0 | dec->payload_offset += nwritten; |
508 | 0 | remain = curlx_sotouz_range(dec->payload_len - dec->payload_offset, |
509 | 0 | 0, SIZE_MAX); |
510 | 0 | CURL_TRC_WS(data, "passed %zu bytes payload, %zu remain", |
511 | 0 | nwritten, remain); |
512 | 0 | } |
513 | | |
514 | 0 | return remain ? CURLE_AGAIN : CURLE_OK; |
515 | 0 | } |
516 | | |
517 | | static CURLcode ws_dec_pass(struct ws_decoder *dec, |
518 | | struct Curl_easy *data, |
519 | | struct bufq *inraw, |
520 | | ws_write_payload *write_cb, |
521 | | void *write_ctx) |
522 | 0 | { |
523 | 0 | CURLcode result; |
524 | |
|
525 | 0 | if(Curl_bufq_is_empty(inraw)) |
526 | 0 | return CURLE_AGAIN; |
527 | | |
528 | 0 | switch(dec->state) { |
529 | 0 | case WS_DEC_INIT: |
530 | 0 | ws_dec_next_frame(dec); |
531 | 0 | dec->state = WS_DEC_HEAD; |
532 | 0 | FALLTHROUGH(); |
533 | 0 | case WS_DEC_HEAD: |
534 | 0 | result = ws_dec_read_head(dec, data, inraw); |
535 | 0 | if(result) { |
536 | 0 | if(result != CURLE_AGAIN) { |
537 | 0 | failf(data, "[WS] decode frame error %d", (int)result); |
538 | 0 | break; /* real error */ |
539 | 0 | } |
540 | | /* incomplete ws frame head */ |
541 | 0 | DEBUGASSERT(Curl_bufq_is_empty(inraw)); |
542 | 0 | break; |
543 | 0 | } |
544 | | /* head parsing done */ |
545 | 0 | dec->state = WS_DEC_PAYLOAD; |
546 | 0 | if(dec->payload_len == 0) { |
547 | 0 | size_t nwritten; |
548 | 0 | const uint8_t tmp = '\0'; |
549 | | /* special case of a 0 length frame, need to write once */ |
550 | 0 | result = write_cb(&tmp, 0, dec->frame_age, dec->frame_flags, |
551 | 0 | 0, 0, write_ctx, &nwritten); |
552 | 0 | if(result) |
553 | 0 | return result; |
554 | 0 | dec->state = WS_DEC_INIT; |
555 | 0 | break; |
556 | 0 | } |
557 | 0 | FALLTHROUGH(); |
558 | 0 | case WS_DEC_PAYLOAD: |
559 | 0 | result = ws_dec_pass_payload(dec, data, inraw, write_cb, write_ctx); |
560 | 0 | ws_dec_info(dec, data, "passing"); |
561 | 0 | if(result) |
562 | 0 | return result; |
563 | | /* payload parsing done */ |
564 | 0 | dec->state = WS_DEC_INIT; |
565 | 0 | break; |
566 | 0 | default: |
567 | | /* we covered all enums above, but some code analyzers are whimps */ |
568 | 0 | result = CURLE_FAILED_INIT; |
569 | 0 | } |
570 | 0 | return result; |
571 | 0 | } |
572 | | |
573 | | static void update_meta(struct websocket *ws, |
574 | | int frame_age, int frame_flags, |
575 | | curl_off_t payload_offset, |
576 | | curl_off_t payload_len, |
577 | | size_t cur_len) |
578 | 0 | { |
579 | 0 | curl_off_t bytesleft = (payload_len - payload_offset - cur_len); |
580 | |
|
581 | 0 | ws->recvframe.age = frame_age; |
582 | 0 | ws->recvframe.flags = frame_flags; |
583 | 0 | ws->recvframe.offset = payload_offset; |
584 | 0 | ws->recvframe.len = cur_len; |
585 | 0 | ws->recvframe.bytesleft = bytesleft; |
586 | 0 | } |
587 | | |
588 | | /* WebSocket decoding client writer */ |
589 | | struct ws_cw_ctx { |
590 | | struct Curl_cwriter super; |
591 | | struct bufq buf; |
592 | | }; |
593 | | |
594 | | static CURLcode ws_cw_init(struct Curl_easy *data, |
595 | | struct Curl_cwriter *writer) |
596 | 1 | { |
597 | 1 | struct ws_cw_ctx *ctx = writer->ctx; |
598 | 1 | (void)data; |
599 | 1 | Curl_bufq_init2(&ctx->buf, WS_CHUNK_SIZE, 1, BUFQ_OPT_SOFT_LIMIT); |
600 | 1 | return CURLE_OK; |
601 | 1 | } |
602 | | |
603 | | static void ws_cw_close(struct Curl_easy *data, struct Curl_cwriter *writer) |
604 | 1 | { |
605 | 1 | struct ws_cw_ctx *ctx = writer->ctx; |
606 | 1 | (void)data; |
607 | 1 | Curl_bufq_free(&ctx->buf); |
608 | 1 | } |
609 | | |
610 | | struct ws_cw_dec_ctx { |
611 | | struct Curl_easy *data; |
612 | | struct websocket *ws; |
613 | | struct Curl_cwriter *next_writer; |
614 | | int cw_type; |
615 | | }; |
616 | | |
617 | | static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws, |
618 | | bool blocking); |
619 | | static CURLcode ws_enc_send(struct Curl_easy *data, |
620 | | struct websocket *ws, |
621 | | const uint8_t *buffer, |
622 | | size_t buflen, |
623 | | curl_off_t fragsize, |
624 | | unsigned int flags, |
625 | | size_t *sent); |
626 | | static CURLcode ws_enc_add_pending(struct Curl_easy *data, |
627 | | struct websocket *ws); |
628 | | |
629 | | static CURLcode ws_enc_add_cntrl(struct Curl_easy *data, |
630 | | struct websocket *ws, |
631 | | const uint8_t *payload, |
632 | | size_t plen, |
633 | | unsigned int frame_type) |
634 | 0 | { |
635 | 0 | DEBUGASSERT(plen <= WS_MAX_CNTRL_LEN); |
636 | 0 | if(plen > WS_MAX_CNTRL_LEN) |
637 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
638 | | |
639 | | /* Overwrite any pending frame with the new one, we keep |
640 | | * only one. */ |
641 | 0 | ws->pending.type = frame_type; |
642 | 0 | ws->pending.payload_len = plen; |
643 | 0 | memcpy(ws->pending.payload, payload, plen); |
644 | |
|
645 | 0 | if(!ws->enc.payload_remain) { /* not in the middle of another frame */ |
646 | 0 | CURLcode result = ws_enc_add_pending(data, ws); |
647 | 0 | if(!result) |
648 | 0 | (void)ws_flush(data, ws, Curl_is_in_callback(data)); |
649 | 0 | return result; |
650 | 0 | } |
651 | 0 | return CURLE_OK; |
652 | 0 | } |
653 | | |
654 | | static curl_off_t ws_payload_remain(curl_off_t payload_total, |
655 | | curl_off_t payload_offset, |
656 | | size_t payload_buffered) |
657 | 0 | { |
658 | 0 | curl_off_t buffered, remain = payload_total - payload_offset; |
659 | 0 | if((payload_total < 0) || (payload_offset < 0) || (remain < 0)) |
660 | 0 | return -1; |
661 | 0 | buffered = curlx_uztoso(payload_buffered); |
662 | 0 | if(remain < buffered) |
663 | 0 | return -1; |
664 | 0 | return remain - buffered; |
665 | 0 | } |
666 | | |
667 | | static CURLcode ws_cw_dec_next(const uint8_t *buf, size_t buflen, |
668 | | int frame_age, int frame_flags, |
669 | | curl_off_t payload_offset, |
670 | | curl_off_t payload_len, |
671 | | void *user_data, |
672 | | size_t *pnwritten) |
673 | 0 | { |
674 | 0 | struct ws_cw_dec_ctx *ctx = user_data; |
675 | 0 | struct Curl_easy *data = ctx->data; |
676 | 0 | struct websocket *ws = ctx->ws; |
677 | 0 | bool auto_pong = !data->set.ws_no_auto_pong; |
678 | 0 | curl_off_t remain; |
679 | 0 | CURLcode result; |
680 | |
|
681 | 0 | (void)frame_age; |
682 | 0 | *pnwritten = 0; |
683 | 0 | remain = ws_payload_remain(payload_len, payload_offset, buflen); |
684 | 0 | if(remain < 0) { |
685 | 0 | DEBUGASSERT(0); /* parameter mismatch */ |
686 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
687 | 0 | } |
688 | | |
689 | 0 | if(auto_pong && (frame_flags & CURLWS_PING) && !remain) { |
690 | | /* auto-respond to PINGs, only works for single-frame payloads atm */ |
691 | 0 | CURL_TRC_WS(data, "auto PONG to [PING payload=%" FMT_OFF_T |
692 | 0 | "/%" FMT_OFF_T "]", payload_offset, payload_len); |
693 | | /* send back the exact same content as a PONG */ |
694 | 0 | result = ws_enc_add_cntrl(data, ws, buf, buflen, CURLWS_PONG); |
695 | 0 | if(result) |
696 | 0 | return result; |
697 | 0 | } |
698 | 0 | else if(buflen || !remain) { |
699 | | /* forward the decoded frame to the next client writer. */ |
700 | 0 | update_meta(ws, frame_age, frame_flags, payload_offset, |
701 | 0 | payload_len, buflen); |
702 | |
|
703 | 0 | result = Curl_cwriter_write(data, ctx->next_writer, |
704 | 0 | (ctx->cw_type | CLIENTWRITE_0LEN), |
705 | 0 | (const char *)buf, buflen); |
706 | 0 | if(result) |
707 | 0 | return result; |
708 | 0 | } |
709 | 0 | *pnwritten = buflen; |
710 | 0 | return CURLE_OK; |
711 | 0 | } |
712 | | |
713 | | static CURLcode ws_cw_write(struct Curl_easy *data, |
714 | | struct Curl_cwriter *writer, int type, |
715 | | const char *buf, size_t nbytes) |
716 | 0 | { |
717 | 0 | struct ws_cw_ctx *ctx = writer->ctx; |
718 | 0 | struct websocket *ws; |
719 | 0 | CURLcode result; |
720 | |
|
721 | 0 | CURL_TRC_WRITE(data, "ws_cw_write(len=%zu, type=%d)", nbytes, type); |
722 | 0 | if(!(type & CLIENTWRITE_BODY) || data->set.ws_raw_mode) |
723 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
724 | | |
725 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
726 | 0 | if(!ws) { |
727 | 0 | failf(data, "[WS] not a websocket transfer"); |
728 | 0 | return CURLE_FAILED_INIT; |
729 | 0 | } |
730 | | |
731 | 0 | if(nbytes) { |
732 | 0 | size_t nwritten; |
733 | 0 | result = Curl_bufq_write(&ctx->buf, (const uint8_t *)buf, |
734 | 0 | nbytes, &nwritten); |
735 | 0 | if(result) { |
736 | 0 | infof(data, "[WS] error adding data to buffer %d", result); |
737 | 0 | return result; |
738 | 0 | } |
739 | 0 | } |
740 | | |
741 | 0 | while(!Curl_bufq_is_empty(&ctx->buf)) { |
742 | 0 | struct ws_cw_dec_ctx pass_ctx; |
743 | 0 | pass_ctx.data = data; |
744 | 0 | pass_ctx.ws = ws; |
745 | 0 | pass_ctx.next_writer = writer->next; |
746 | 0 | pass_ctx.cw_type = type; |
747 | 0 | result = ws_dec_pass(&ws->dec, data, &ctx->buf, |
748 | 0 | ws_cw_dec_next, &pass_ctx); |
749 | 0 | if(result == CURLE_AGAIN) { |
750 | | /* insufficient amount of data, keep it for later. |
751 | | * we pretend to have written all since we have a copy */ |
752 | 0 | return CURLE_OK; |
753 | 0 | } |
754 | 0 | else if(result) { |
755 | 0 | failf(data, "[WS] decode payload error %d", (int)result); |
756 | 0 | return result; |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | 0 | if((type & CLIENTWRITE_EOS) && !Curl_bufq_is_empty(&ctx->buf)) { |
761 | 0 | failf(data, "[WS] decode ending with %zd frame bytes remaining", |
762 | 0 | Curl_bufq_len(&ctx->buf)); |
763 | 0 | return CURLE_RECV_ERROR; |
764 | 0 | } |
765 | | |
766 | 0 | return CURLE_OK; |
767 | 0 | } |
768 | | |
769 | | /* WebSocket payload decoding client writer. */ |
770 | | static const struct Curl_cwtype ws_cw_decode = { |
771 | | "ws-decode", |
772 | | NULL, |
773 | | ws_cw_init, |
774 | | ws_cw_write, |
775 | | ws_cw_close, |
776 | | sizeof(struct ws_cw_ctx) |
777 | | }; |
778 | | |
779 | | static void ws_enc_info(struct ws_encoder *enc, struct Curl_easy *data, |
780 | | const char *msg) |
781 | 0 | { |
782 | 0 | NOVERBOSE((void)enc); |
783 | 0 | NOVERBOSE((void)msg); |
784 | 0 | CURL_TRC_WS(data, "WS-ENC: %s [%s%s payload=%" |
785 | 0 | FMT_OFF_T "/%" FMT_OFF_T "]", |
786 | 0 | msg, ws_frame_name_of_op(enc->firstbyte), |
787 | 0 | (enc->firstbyte & WSBIT_FIN) ? "" : " NON-FIN", |
788 | 0 | enc->payload_len - enc->payload_remain, enc->payload_len); |
789 | 0 | } |
790 | | |
791 | | static void ws_enc_reset(struct ws_encoder *enc) |
792 | 1 | { |
793 | 1 | enc->payload_remain = 0; |
794 | 1 | enc->xori = 0; |
795 | 1 | enc->contfragment = FALSE; |
796 | 1 | } |
797 | | |
798 | | static void ws_enc_init(struct ws_encoder *enc) |
799 | 1 | { |
800 | 1 | ws_enc_reset(enc); |
801 | 1 | } |
802 | | |
803 | | /*** |
804 | | RFC 6455 Section 5.2 |
805 | | |
806 | | 0 1 2 3 |
807 | | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
808 | | +-+-+-+-+-------+-+-------------+-------------------------------+ |
809 | | |F|R|R|R| opcode|M| Payload len | Extended payload length | |
810 | | |I|S|S|S| (4) |A| (7) | (16/64) | |
811 | | |N|V|V|V| |S| | (if payload len==126/127) | |
812 | | | |1|2|3| |K| | | |
813 | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
814 | | | Extended payload length continued, if payload len == 127 | |
815 | | + - - - - - - - - - - - - - - - +-------------------------------+ |
816 | | | |Masking-key, if MASK set to 1 | |
817 | | +-------------------------------+-------------------------------+ |
818 | | | Masking-key (continued) | Payload Data | |
819 | | +-------------------------------- - - - - - - - - - - - - - - - + |
820 | | : Payload Data continued ... : |
821 | | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |
822 | | | Payload Data continued ... | |
823 | | +---------------------------------------------------------------+ |
824 | | */ |
825 | | |
826 | | static CURLcode ws_enc_add_frame(struct Curl_easy *data, |
827 | | struct ws_encoder *enc, |
828 | | unsigned int flags, |
829 | | curl_off_t payload_len, |
830 | | struct bufq *out) |
831 | 0 | { |
832 | 0 | uint8_t firstb = 0; |
833 | 0 | uint8_t head[14]; |
834 | 0 | CURLcode result; |
835 | 0 | size_t hlen, nwritten; |
836 | |
|
837 | 0 | if(payload_len < 0) { |
838 | 0 | failf(data, "[WS] starting new frame with negative payload length %" |
839 | 0 | FMT_OFF_T, payload_len); |
840 | 0 | return CURLE_SEND_ERROR; |
841 | 0 | } |
842 | | |
843 | 0 | if(enc->payload_remain > 0) { |
844 | | /* trying to write a new frame before the previous one is finished */ |
845 | 0 | failf(data, "[WS] starting new frame with %" FMT_OFF_T " bytes " |
846 | 0 | "from last one remaining to be sent", enc->payload_remain); |
847 | 0 | return CURLE_SEND_ERROR; |
848 | 0 | } |
849 | | |
850 | 0 | result = ws_frame_flags2firstbyte(data, flags, (bool)enc->contfragment, |
851 | 0 | &firstb); |
852 | 0 | if(result) |
853 | 0 | return result; |
854 | | |
855 | | /* fragmentation only applies to data frames (text/binary); |
856 | | * control frames (close/ping/pong) do not affect the CONT status */ |
857 | 0 | if(flags & (CURLWS_TEXT | CURLWS_BINARY)) { |
858 | 0 | enc->contfragment = (curl_bit)((flags & CURLWS_CONT) ? TRUE : FALSE); |
859 | 0 | } |
860 | |
|
861 | 0 | if(flags & CURLWS_PING && payload_len > WS_MAX_CNTRL_LEN) { |
862 | 0 | failf(data, "[WS] given PING frame is too big"); |
863 | 0 | return CURLE_TOO_LARGE; |
864 | 0 | } |
865 | 0 | if(flags & CURLWS_PONG && payload_len > WS_MAX_CNTRL_LEN) { |
866 | 0 | failf(data, "[WS] given PONG frame is too big"); |
867 | 0 | return CURLE_TOO_LARGE; |
868 | 0 | } |
869 | 0 | if(flags & CURLWS_CLOSE && payload_len > WS_MAX_CNTRL_LEN) { |
870 | 0 | failf(data, "[WS] given CLOSE frame is too big"); |
871 | 0 | return CURLE_TOO_LARGE; |
872 | 0 | } |
873 | | |
874 | 0 | head[0] = enc->firstbyte = firstb; |
875 | 0 | if(payload_len > 65535) { |
876 | 0 | head[1] = 127 | WSBIT_MASK; |
877 | 0 | head[2] = (uint8_t)((payload_len >> 56) & 0xff); |
878 | 0 | head[3] = (uint8_t)((payload_len >> 48) & 0xff); |
879 | 0 | head[4] = (uint8_t)((payload_len >> 40) & 0xff); |
880 | 0 | head[5] = (uint8_t)((payload_len >> 32) & 0xff); |
881 | 0 | head[6] = (uint8_t)((payload_len >> 24) & 0xff); |
882 | 0 | head[7] = (uint8_t)((payload_len >> 16) & 0xff); |
883 | 0 | head[8] = (uint8_t)((payload_len >> 8) & 0xff); |
884 | 0 | head[9] = (uint8_t)(payload_len & 0xff); |
885 | 0 | hlen = 10; |
886 | 0 | } |
887 | 0 | else if(payload_len >= 126) { |
888 | 0 | head[1] = 126 | WSBIT_MASK; |
889 | 0 | head[2] = (uint8_t)((payload_len >> 8) & 0xff); |
890 | 0 | head[3] = (uint8_t)(payload_len & 0xff); |
891 | 0 | hlen = 4; |
892 | 0 | } |
893 | 0 | else { |
894 | 0 | head[1] = (uint8_t)payload_len | WSBIT_MASK; |
895 | 0 | hlen = 2; |
896 | 0 | } |
897 | |
|
898 | 0 | enc->payload_remain = enc->payload_len = payload_len; |
899 | 0 | ws_enc_info(enc, data, "sending"); |
900 | | |
901 | | /* 4 bytes random */ |
902 | |
|
903 | 0 | result = Curl_rand(data, (uint8_t *)&enc->mask, sizeof(enc->mask)); |
904 | 0 | if(result) |
905 | 0 | return result; |
906 | | |
907 | 0 | #ifdef DEBUGBUILD |
908 | 0 | if(getenv("CURL_WS_FORCE_ZERO_MASK")) |
909 | | /* force the bit mask to 0x00000000, effectively disabling masking */ |
910 | 0 | memset(&enc->mask, 0, sizeof(enc->mask)); |
911 | 0 | #endif |
912 | | |
913 | | /* add 4 bytes mask */ |
914 | 0 | memcpy(&head[hlen], &enc->mask, 4); |
915 | 0 | hlen += 4; |
916 | | /* reset for payload to come */ |
917 | 0 | enc->xori = 0; |
918 | |
|
919 | 0 | result = Curl_bufq_write(out, head, hlen, &nwritten); |
920 | 0 | if(result) |
921 | 0 | return result; |
922 | 0 | if(nwritten != hlen) { |
923 | | /* We use a bufq with SOFT_LIMIT, writing should always succeed */ |
924 | 0 | DEBUGASSERT(0); |
925 | 0 | return CURLE_SEND_ERROR; |
926 | 0 | } |
927 | 0 | return CURLE_OK; |
928 | 0 | } |
929 | | |
930 | | static CURLcode ws_enc_write_head(struct Curl_easy *data, |
931 | | struct websocket *ws, |
932 | | struct ws_encoder *enc, |
933 | | unsigned int flags, |
934 | | curl_off_t payload_len, |
935 | | struct bufq *out) |
936 | 0 | { |
937 | | /* starting a new frame, we want a clean sendbuf. |
938 | | * Any pending control frame we can add now as part of the flush. */ |
939 | 0 | if(ws->pending.type) { |
940 | 0 | CURLcode result = ws_enc_add_pending(data, ws); |
941 | 0 | if(result) |
942 | 0 | return result; |
943 | 0 | } |
944 | 0 | return ws_enc_add_frame(data, enc, flags, payload_len, out); |
945 | 0 | } |
946 | | |
947 | | static CURLcode ws_enc_write_payload(struct ws_encoder *enc, |
948 | | struct Curl_easy *data, |
949 | | const uint8_t *buf, size_t buflen, |
950 | | struct bufq *out, size_t *pnwritten) |
951 | 0 | { |
952 | 0 | CURLcode result; |
953 | 0 | size_t i, len, n, remain; |
954 | |
|
955 | 0 | *pnwritten = 0; |
956 | 0 | if(Curl_bufq_is_full(out)) |
957 | 0 | return CURLE_AGAIN; |
958 | | |
959 | | /* not the most performant way to do this */ |
960 | 0 | len = buflen; |
961 | 0 | remain = curlx_sotouz_range(enc->payload_remain, 0, SIZE_MAX); |
962 | 0 | if(remain < len) |
963 | 0 | len = remain; |
964 | |
|
965 | 0 | for(i = 0; i < len; ++i) { |
966 | 0 | uint8_t c = buf[i] ^ enc->mask[enc->xori]; |
967 | 0 | result = Curl_bufq_write(out, &c, 1, &n); |
968 | 0 | if(result) { |
969 | 0 | if((result != CURLE_AGAIN) || !i) |
970 | 0 | return result; |
971 | 0 | break; |
972 | 0 | } |
973 | 0 | enc->xori++; |
974 | 0 | enc->xori &= 3; |
975 | 0 | } |
976 | 0 | *pnwritten = i; |
977 | 0 | enc->payload_remain -= (curl_off_t)i; |
978 | 0 | ws_enc_info(enc, data, "buffered"); |
979 | 0 | return CURLE_OK; |
980 | 0 | } |
981 | | |
982 | | static CURLcode ws_enc_add_pending(struct Curl_easy *data, |
983 | | struct websocket *ws) |
984 | 0 | { |
985 | 0 | CURLcode result; |
986 | 0 | size_t n; |
987 | |
|
988 | 0 | if(!ws->pending.type) /* no pending frame here */ |
989 | 0 | return CURLE_OK; |
990 | 0 | if(ws->enc.payload_remain) /* in the middle of another frame */ |
991 | 0 | return CURLE_AGAIN; |
992 | | |
993 | 0 | result = ws_enc_add_frame(data, &ws->enc, ws->pending.type, |
994 | 0 | (curl_off_t)ws->pending.payload_len, |
995 | 0 | &ws->sendbuf); |
996 | 0 | if(result) { |
997 | 0 | CURL_TRC_WS(data, "ws_enc_cntrl(), error adding head: %d", |
998 | 0 | result); |
999 | 0 | goto out; |
1000 | 0 | } |
1001 | 0 | result = ws_enc_write_payload(&ws->enc, data, ws->pending.payload, |
1002 | 0 | ws->pending.payload_len, |
1003 | 0 | &ws->sendbuf, &n); |
1004 | 0 | if(result) { |
1005 | 0 | CURL_TRC_WS(data, "ws_enc_cntrl(), error adding payload: %d", |
1006 | 0 | result); |
1007 | 0 | goto out; |
1008 | 0 | } |
1009 | 0 | if(n != ws->pending.payload_len) { |
1010 | 0 | DEBUGASSERT(0); /* buffer should always be able to take all */ |
1011 | 0 | CURL_TRC_WS(data, "ws_enc_cntrl(), error added only %zu/%zu payload,", |
1012 | 0 | n, ws->pending.payload_len); |
1013 | 0 | result = CURLE_SEND_ERROR; |
1014 | 0 | goto out; |
1015 | 0 | } |
1016 | | /* the frame should be complete now */ |
1017 | 0 | DEBUGASSERT(!ws->enc.payload_remain); |
1018 | 0 | memset(&ws->pending, 0, sizeof(ws->pending)); |
1019 | |
|
1020 | 0 | out: |
1021 | 0 | return result; |
1022 | 0 | } |
1023 | | |
1024 | | static CURLcode ws_enc_send(struct Curl_easy *data, |
1025 | | struct websocket *ws, |
1026 | | const uint8_t *buffer, |
1027 | | size_t buflen, |
1028 | | curl_off_t fragsize, |
1029 | | unsigned int flags, |
1030 | | size_t *pnsent) |
1031 | 0 | { |
1032 | 0 | size_t n; |
1033 | 0 | CURLcode result = CURLE_OK; |
1034 | |
|
1035 | 0 | DEBUGASSERT(!data->set.ws_raw_mode); |
1036 | 0 | *pnsent = 0; |
1037 | |
|
1038 | 0 | if(ws->enc.payload_remain || !Curl_bufq_is_empty(&ws->sendbuf)) { |
1039 | | /* a frame is ongoing with payload buffered or more payload |
1040 | | * that needs to be encoded into the buffer */ |
1041 | 0 | if(buflen < ws->sendbuf_payload) { |
1042 | | /* We have been called with LESS buffer data than before. This |
1043 | | * is not how it is supposed too work. */ |
1044 | 0 | failf(data, "[WS] curl_ws_send() called with smaller 'buflen' than " |
1045 | 0 | "bytes already buffered in previous call, %zu vs %zu", |
1046 | 0 | buflen, ws->sendbuf_payload); |
1047 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1048 | 0 | } |
1049 | 0 | if((curl_off_t)buflen > |
1050 | 0 | (ws->enc.payload_remain + (curl_off_t)ws->sendbuf_payload)) { |
1051 | | /* too large buflen beyond payload length of frame */ |
1052 | 0 | failf(data, "[WS] unaligned frame size (sending %zu instead of %" |
1053 | 0 | FMT_OFF_T ")", |
1054 | 0 | buflen, ws->enc.payload_remain + ws->sendbuf_payload); |
1055 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | else { |
1059 | 0 | result = ws_flush(data, ws, Curl_is_in_callback(data)); |
1060 | 0 | if(result) |
1061 | 0 | return result; |
1062 | | |
1063 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, flags, |
1064 | 0 | (flags & CURLWS_OFFSET) ? |
1065 | 0 | fragsize : (curl_off_t)buflen, |
1066 | 0 | &ws->sendbuf); |
1067 | 0 | if(result) { |
1068 | 0 | CURL_TRC_WS(data, "curl_ws_send(), error writing frame head %d", result); |
1069 | 0 | return result; |
1070 | 0 | } |
1071 | 0 | } |
1072 | | |
1073 | | /* While there is either sendbuf to flush OR more payload to encode... */ |
1074 | 0 | while(!Curl_bufq_is_empty(&ws->sendbuf) || (buflen > ws->sendbuf_payload)) { |
1075 | | /* Try to add more payload to sendbuf */ |
1076 | 0 | if(buflen > ws->sendbuf_payload) { |
1077 | 0 | size_t prev_len = Curl_bufq_len(&ws->sendbuf); |
1078 | 0 | result = ws_enc_write_payload(&ws->enc, data, |
1079 | 0 | buffer + ws->sendbuf_payload, |
1080 | 0 | buflen - ws->sendbuf_payload, |
1081 | 0 | &ws->sendbuf, &n); |
1082 | 0 | if(result && (result != CURLE_AGAIN)) |
1083 | 0 | return result; |
1084 | 0 | ws->sendbuf_payload += Curl_bufq_len(&ws->sendbuf) - prev_len; |
1085 | 0 | if(!ws->sendbuf_payload) { |
1086 | 0 | return CURLE_AGAIN; |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | | /* flush, blocking when in callback */ |
1091 | 0 | result = ws_flush(data, ws, Curl_is_in_callback(data)); |
1092 | 0 | if(!result && ws->sendbuf_payload > 0) { |
1093 | 0 | *pnsent += ws->sendbuf_payload; |
1094 | 0 | buffer += ws->sendbuf_payload; |
1095 | 0 | buflen -= ws->sendbuf_payload; |
1096 | 0 | ws->sendbuf_payload = 0; |
1097 | 0 | } |
1098 | 0 | else if(result == CURLE_AGAIN) { |
1099 | 0 | if(ws->sendbuf_payload > Curl_bufq_len(&ws->sendbuf)) { |
1100 | | /* blocked, part of payload bytes remain, report length |
1101 | | * that we managed to send. */ |
1102 | 0 | size_t flushed = (ws->sendbuf_payload - Curl_bufq_len(&ws->sendbuf)); |
1103 | 0 | *pnsent += flushed; |
1104 | 0 | ws->sendbuf_payload -= flushed; |
1105 | 0 | return CURLE_OK; |
1106 | 0 | } |
1107 | 0 | else { |
1108 | | /* blocked before sending headers or 1st payload byte. We cannot report |
1109 | | * OK on 0-length send (caller counts only payload) and EAGAIN */ |
1110 | 0 | CURL_TRC_WS(data, "EAGAIN flushing sendbuf, payload_encoded: %zu/%zu", |
1111 | 0 | ws->sendbuf_payload, buflen); |
1112 | 0 | DEBUGASSERT(*pnsent == 0); |
1113 | 0 | return CURLE_AGAIN; |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | else |
1117 | 0 | return result; /* real error sending the data */ |
1118 | 0 | } |
1119 | 0 | return CURLE_OK; |
1120 | 0 | } |
1121 | | |
1122 | | struct cr_ws_ctx { |
1123 | | struct Curl_creader super; |
1124 | | BIT(read_eos); /* we read an EOS from the next reader */ |
1125 | | BIT(eos); /* we have returned an EOS */ |
1126 | | }; |
1127 | | |
1128 | | static CURLcode cr_ws_init(struct Curl_easy *data, struct Curl_creader *reader) |
1129 | 0 | { |
1130 | 0 | (void)data; |
1131 | 0 | (void)reader; |
1132 | 0 | return CURLE_OK; |
1133 | 0 | } |
1134 | | |
1135 | | static void cr_ws_close(struct Curl_easy *data, struct Curl_creader *reader) |
1136 | 0 | { |
1137 | 0 | (void)data; |
1138 | 0 | (void)reader; |
1139 | 0 | } |
1140 | | |
1141 | | static CURLcode cr_ws_read(struct Curl_easy *data, |
1142 | | struct Curl_creader *reader, |
1143 | | char *buf, size_t blen, |
1144 | | size_t *pnread, bool *peos) |
1145 | 0 | { |
1146 | 0 | struct cr_ws_ctx *ctx = reader->ctx; |
1147 | 0 | CURLcode result = CURLE_OK; |
1148 | 0 | size_t nread, n; |
1149 | 0 | struct websocket *ws; |
1150 | 0 | bool eos; |
1151 | |
|
1152 | 0 | *pnread = 0; |
1153 | 0 | if(ctx->eos) { |
1154 | 0 | *peos = TRUE; |
1155 | 0 | return CURLE_OK; |
1156 | 0 | } |
1157 | | |
1158 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1159 | 0 | if(!ws) { |
1160 | 0 | failf(data, "[WS] not a websocket transfer"); |
1161 | 0 | return CURLE_FAILED_INIT; |
1162 | 0 | } |
1163 | | |
1164 | 0 | if(Curl_bufq_is_empty(&ws->sendbuf)) { |
1165 | 0 | if(ctx->read_eos) { |
1166 | 0 | ctx->eos = TRUE; |
1167 | 0 | *peos = TRUE; |
1168 | 0 | return CURLE_OK; |
1169 | 0 | } |
1170 | | |
1171 | 0 | if(ws->enc.payload_remain) { |
1172 | 0 | CURL_TRC_WS(data, "current frame, %" FMT_OFF_T " remaining", |
1173 | 0 | ws->enc.payload_remain); |
1174 | 0 | blen = curlx_sotouz_range(ws->enc.payload_remain, 0, blen); |
1175 | 0 | } |
1176 | |
|
1177 | 0 | result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos); |
1178 | 0 | if(result) |
1179 | 0 | return result; |
1180 | 0 | ctx->read_eos = eos; |
1181 | |
|
1182 | 0 | if(!Curl_bufq_is_empty(&ws->sendbuf)) { |
1183 | | /* client_read started a new frame, we disregard any eos reported */ |
1184 | 0 | ctx->read_eos = FALSE; |
1185 | 0 | Curl_creader_clear_eos(data, reader->next); |
1186 | 0 | } |
1187 | 0 | else if(!nread) { |
1188 | | /* nothing to convert, return this right away */ |
1189 | 0 | if(ctx->read_eos) |
1190 | 0 | ctx->eos = TRUE; |
1191 | 0 | *pnread = nread; |
1192 | 0 | *peos = (bool)ctx->eos; |
1193 | 0 | goto out; |
1194 | 0 | } |
1195 | | |
1196 | 0 | if(!ws->enc.payload_remain && Curl_bufq_is_empty(&ws->sendbuf)) { |
1197 | | /* encode the data as a new BINARY frame */ |
1198 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, CURLWS_BINARY, nread, |
1199 | 0 | &ws->sendbuf); |
1200 | 0 | if(result) |
1201 | 0 | goto out; |
1202 | 0 | } |
1203 | | |
1204 | 0 | result = ws_enc_write_payload(&ws->enc, data, (uint8_t *)buf, |
1205 | 0 | nread, &ws->sendbuf, &n); |
1206 | 0 | if(result) |
1207 | 0 | goto out; |
1208 | 0 | CURL_TRC_READ(data, "cr_ws_read, added %zu payload, len=%zu", nread, n); |
1209 | 0 | } |
1210 | | |
1211 | 0 | DEBUGASSERT(!Curl_bufq_is_empty(&ws->sendbuf)); |
1212 | 0 | *peos = FALSE; |
1213 | 0 | result = Curl_bufq_cread(&ws->sendbuf, buf, blen, pnread); |
1214 | 0 | if(!result && ctx->read_eos && Curl_bufq_is_empty(&ws->sendbuf)) { |
1215 | | /* no more data, read all, done. */ |
1216 | 0 | ctx->eos = TRUE; |
1217 | 0 | *peos = TRUE; |
1218 | 0 | } |
1219 | |
|
1220 | 0 | out: |
1221 | 0 | CURL_TRC_READ(data, "cr_ws_read(len=%zu) -> %d, nread=%zu, eos=%d", |
1222 | 0 | blen, result, *pnread, *peos); |
1223 | 0 | return result; |
1224 | 0 | } |
1225 | | |
1226 | | static const struct Curl_crtype ws_cr_encode = { |
1227 | | "ws-encode", |
1228 | | cr_ws_init, |
1229 | | cr_ws_read, |
1230 | | cr_ws_close, |
1231 | | Curl_creader_def_needs_rewind, |
1232 | | Curl_creader_def_total_length, |
1233 | | Curl_creader_def_resume_from, |
1234 | | Curl_creader_def_cntrl, |
1235 | | Curl_creader_def_is_paused, |
1236 | | Curl_creader_def_done, |
1237 | | sizeof(struct cr_ws_ctx) |
1238 | | }; |
1239 | | |
1240 | | struct wsfield { |
1241 | | const char *name; |
1242 | | const char *val; |
1243 | | }; |
1244 | | |
1245 | | CURLcode Curl_ws_request(struct Curl_easy *data, struct dynbuf *req) |
1246 | 224 | { |
1247 | 224 | unsigned int i; |
1248 | 224 | CURLcode result = CURLE_OK; |
1249 | 224 | uint8_t rand[16]; |
1250 | 224 | char *randstr; |
1251 | 224 | size_t randlen; |
1252 | 224 | char keyval[40]; |
1253 | 224 | struct SingleRequest *k = &data->req; |
1254 | 224 | struct wsfield heads[] = { |
1255 | 224 | { |
1256 | | /* The request MUST contain an |Upgrade| header field whose value |
1257 | | MUST include the "websocket" keyword. */ |
1258 | 224 | "Upgrade", "websocket" |
1259 | 224 | }, |
1260 | 224 | { |
1261 | | /* The request MUST include a header field with the name |
1262 | | |Sec-WebSocket-Version|. The value of this header field MUST be |
1263 | | 13. */ |
1264 | 224 | "Sec-WebSocket-Version", "13", |
1265 | 224 | }, |
1266 | 224 | { |
1267 | | /* The request MUST include a header field with the name |
1268 | | |Sec-WebSocket-Key|. The value of this header field MUST be a nonce |
1269 | | consisting of a randomly selected 16-byte value that has been |
1270 | | base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be |
1271 | | selected randomly for each connection. */ |
1272 | 224 | "Sec-WebSocket-Key", NULL, |
1273 | 224 | } |
1274 | 224 | }; |
1275 | 224 | heads[2].val = &keyval[0]; |
1276 | | |
1277 | | /* 16 bytes random */ |
1278 | 224 | result = Curl_rand(data, rand, sizeof(rand)); |
1279 | 224 | if(result) |
1280 | 0 | return result; |
1281 | 224 | result = curlx_base64_encode(rand, sizeof(rand), &randstr, &randlen); |
1282 | 224 | if(result) |
1283 | 0 | return result; |
1284 | 224 | DEBUGASSERT(randlen < sizeof(keyval)); |
1285 | 224 | if(randlen >= sizeof(keyval)) { |
1286 | 0 | curlx_free(randstr); |
1287 | 0 | return CURLE_FAILED_INIT; |
1288 | 0 | } |
1289 | 224 | curlx_strcopy(keyval, sizeof(keyval), randstr, randlen); |
1290 | 224 | curlx_free(randstr); |
1291 | 896 | for(i = 0; !result && (i < CURL_ARRAYSIZE(heads)); i++) { |
1292 | 672 | if(!Curl_checkheaders(data, heads[i].name, strlen(heads[i].name))) { |
1293 | 666 | result = curlx_dyn_addf(req, "%s: %s\r\n", heads[i].name, heads[i].val); |
1294 | 666 | } |
1295 | 672 | } |
1296 | 224 | data->state.http_hd_upgrade = TRUE; |
1297 | 224 | k->upgr101 = UPGR101_WS; |
1298 | 224 | data->conn->bits.upgrade_in_progress = TRUE; |
1299 | 224 | return result; |
1300 | 224 | } |
1301 | | |
1302 | | static void ws_conn_dtor(void *key, size_t klen, void *entry) |
1303 | 1 | { |
1304 | 1 | struct websocket *ws = entry; |
1305 | 1 | (void)key; |
1306 | 1 | (void)klen; |
1307 | 1 | Curl_bufq_free(&ws->recvbuf); |
1308 | 1 | Curl_bufq_free(&ws->sendbuf); |
1309 | 1 | curlx_free(ws); |
1310 | 1 | } |
1311 | | |
1312 | | /* |
1313 | | * 'nread' is number of bytes of websocket data already in the buffer at |
1314 | | * 'mem'. |
1315 | | */ |
1316 | | CURLcode Curl_ws_accept(struct Curl_easy *data, |
1317 | | const char *mem, size_t nread) |
1318 | 1 | { |
1319 | 1 | struct SingleRequest *k = &data->req; |
1320 | 1 | struct websocket *ws; |
1321 | 1 | struct Curl_cwriter *ws_dec_writer = NULL; |
1322 | 1 | struct Curl_creader *ws_enc_reader = NULL; |
1323 | 1 | CURLcode result; |
1324 | | |
1325 | 1 | DEBUGASSERT(data->conn); |
1326 | 1 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1327 | 1 | if(!ws) { |
1328 | 1 | size_t chunk_size = WS_CHUNK_SIZE; |
1329 | 1 | ws = curlx_calloc(1, sizeof(*ws)); |
1330 | 1 | if(!ws) |
1331 | 0 | return CURLE_OUT_OF_MEMORY; |
1332 | 1 | #ifdef DEBUGBUILD |
1333 | 1 | { |
1334 | 1 | const char *p = getenv("CURL_WS_CHUNK_SIZE"); |
1335 | 1 | if(p) { |
1336 | 0 | curl_off_t l; |
1337 | 0 | if(!curlx_str_number(&p, &l, 1 * 1024 * 1024)) |
1338 | 0 | chunk_size = (size_t)l; |
1339 | 0 | } |
1340 | 1 | } |
1341 | 1 | #endif |
1342 | 1 | CURL_TRC_WS(data, "WS, using chunk size %zu", chunk_size); |
1343 | 1 | Curl_bufq_init2(&ws->recvbuf, chunk_size, WS_CHUNK_COUNT, |
1344 | 1 | BUFQ_OPT_SOFT_LIMIT); |
1345 | 1 | Curl_bufq_init2(&ws->sendbuf, chunk_size, WS_CHUNK_COUNT, |
1346 | 1 | BUFQ_OPT_SOFT_LIMIT); |
1347 | 1 | ws_dec_init(&ws->dec); |
1348 | 1 | ws_enc_init(&ws->enc); |
1349 | 1 | result = Curl_conn_meta_set(data->conn, CURL_META_PROTO_WS_CONN, |
1350 | 1 | ws, ws_conn_dtor); |
1351 | 1 | if(result) |
1352 | 0 | return result; |
1353 | 1 | } |
1354 | 0 | else { |
1355 | 0 | Curl_bufq_reset(&ws->recvbuf); |
1356 | 0 | ws_dec_reset(&ws->dec); |
1357 | 0 | ws_enc_reset(&ws->enc); |
1358 | 0 | } |
1359 | | /* Verify the Sec-WebSocket-Accept response. |
1360 | | |
1361 | | The sent value is the base64 encoded version of a SHA-1 hash done on the |
1362 | | |Sec-WebSocket-Key| header field concatenated with |
1363 | | the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11". |
1364 | | */ |
1365 | | |
1366 | | /* If the response includes a |Sec-WebSocket-Extensions| header field and |
1367 | | this header field indicates the use of an extension that was not present |
1368 | | in the client's handshake (the server has indicated an extension not |
1369 | | requested by the client), the client MUST Fail the WebSocket Connection. |
1370 | | */ |
1371 | | |
1372 | | /* If the response includes a |Sec-WebSocket-Protocol| header field |
1373 | | and this header field indicates the use of a subprotocol that was |
1374 | | not present in the client's handshake (the server has indicated a |
1375 | | subprotocol not requested by the client), the client MUST Fail |
1376 | | the WebSocket Connection. */ |
1377 | | |
1378 | 1 | infof(data, "[WS] Received 101, switch to WebSocket"); |
1379 | | |
1380 | | /* Install our client writer that decodes WS frames payload */ |
1381 | 1 | result = Curl_cwriter_create(&ws_dec_writer, data, &ws_cw_decode, |
1382 | 1 | CURL_CW_CONTENT_DECODE); |
1383 | 1 | if(result) |
1384 | 0 | goto out; |
1385 | 1 | result = Curl_cwriter_add(data, ws_dec_writer); |
1386 | 1 | if(result) |
1387 | 0 | goto out; |
1388 | 1 | ws_dec_writer = NULL; /* owned by transfer now */ |
1389 | | |
1390 | 1 | k->header = FALSE; /* we will not get more response headers */ |
1391 | | |
1392 | 1 | if(data->set.connect_only) { |
1393 | 1 | size_t nwritten; |
1394 | | /* In CONNECT_ONLY setup, the payloads from `mem` need to be received |
1395 | | * when using `curl_ws_recv` later on after this transfer is already |
1396 | | * marked as DONE. */ |
1397 | 1 | result = Curl_bufq_write(&ws->recvbuf, (const uint8_t *)mem, |
1398 | 1 | nread, &nwritten); |
1399 | 1 | if(result) |
1400 | 0 | goto out; |
1401 | 1 | DEBUGASSERT(nread == nwritten); |
1402 | 1 | k->keepon &= ~KEEP_RECV; /* read no more content */ |
1403 | 1 | } |
1404 | 0 | else { /* !connect_only */ |
1405 | 0 | if(data->set.method == HTTPREQ_PUT) { |
1406 | 0 | CURL_TRC_WS(data, "UPLOAD set, add ws-encode reader"); |
1407 | 0 | result = Curl_creader_set_fread(data, -1); |
1408 | 0 | if(result) |
1409 | 0 | goto out; |
1410 | | |
1411 | 0 | if(!data->set.ws_raw_mode) { |
1412 | | /* Add our client readerr encoding WS BINARY frames */ |
1413 | 0 | result = Curl_creader_create(&ws_enc_reader, data, &ws_cr_encode, |
1414 | 0 | CURL_CR_CONTENT_ENCODE); |
1415 | 0 | if(result) |
1416 | 0 | goto out; |
1417 | 0 | result = Curl_creader_add(data, ws_enc_reader); |
1418 | 0 | if(result) |
1419 | 0 | goto out; |
1420 | 0 | ws_enc_reader = NULL; /* owned by transfer now */ |
1421 | 0 | } |
1422 | | |
1423 | | /* start over with sending */ |
1424 | 0 | data->req.eos_read = FALSE; |
1425 | 0 | data->req.upload_done = FALSE; |
1426 | 0 | k->keepon |= KEEP_SEND; |
1427 | 0 | } |
1428 | | |
1429 | | /* And pass any additional data to the writers */ |
1430 | 0 | if(nread) { |
1431 | 0 | result = Curl_client_write(data, CLIENTWRITE_BODY, mem, nread); |
1432 | 0 | if(result) |
1433 | 0 | goto out; |
1434 | 0 | } |
1435 | 0 | } |
1436 | | |
1437 | 1 | k->upgr101 = UPGR101_RECEIVED; |
1438 | 1 | k->header = FALSE; /* we will not get more responses */ |
1439 | | |
1440 | 1 | out: |
1441 | 1 | if(ws_dec_writer) |
1442 | 0 | Curl_cwriter_free(data, ws_dec_writer); |
1443 | 1 | if(ws_enc_reader) |
1444 | 0 | Curl_creader_free(data, ws_enc_reader); |
1445 | 1 | if(result) |
1446 | 0 | CURL_TRC_WS(data, "Curl_ws_accept() failed -> %d", result); |
1447 | 1 | else |
1448 | 1 | CURL_TRC_WS(data, "websocket established, %s mode", |
1449 | 1 | data->set.connect_only ? "connect-only" : "callback"); |
1450 | 1 | return result; |
1451 | 1 | } |
1452 | | |
1453 | | struct ws_collect { |
1454 | | struct Curl_easy *data; |
1455 | | struct websocket *ws; |
1456 | | uint8_t *buffer; |
1457 | | size_t buflen; |
1458 | | size_t bufidx; |
1459 | | int frame_age; |
1460 | | int frame_flags; |
1461 | | curl_off_t payload_offset; |
1462 | | curl_off_t payload_len; |
1463 | | bool written; |
1464 | | }; |
1465 | | |
1466 | | static CURLcode ws_client_collect(const uint8_t *buf, size_t buflen, |
1467 | | int frame_age, int frame_flags, |
1468 | | curl_off_t payload_offset, |
1469 | | curl_off_t payload_len, |
1470 | | void *userp, |
1471 | | size_t *pnwritten) |
1472 | 0 | { |
1473 | 0 | struct ws_collect *ctx = userp; |
1474 | 0 | struct Curl_easy *data = ctx->data; |
1475 | 0 | bool auto_pong = !data->set.ws_no_auto_pong; |
1476 | 0 | curl_off_t remain; |
1477 | 0 | CURLcode result = CURLE_OK; |
1478 | |
|
1479 | 0 | *pnwritten = 0; |
1480 | 0 | remain = ws_payload_remain(payload_len, payload_offset, buflen); |
1481 | 0 | if(remain < 0) { |
1482 | 0 | DEBUGASSERT(0); /* parameter mismatch */ |
1483 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1484 | 0 | } |
1485 | | |
1486 | 0 | if(!ctx->bufidx) { |
1487 | | /* first write */ |
1488 | 0 | ctx->frame_age = frame_age; |
1489 | 0 | ctx->frame_flags = frame_flags; |
1490 | 0 | ctx->payload_offset = payload_offset; |
1491 | 0 | ctx->payload_len = payload_len; |
1492 | 0 | } |
1493 | |
|
1494 | 0 | if(auto_pong && (frame_flags & CURLWS_PING) && !remain) { |
1495 | | /* auto-respond to PINGs, only works for single-frame payloads atm */ |
1496 | 0 | CURL_TRC_WS(data, "auto PONG to [PING payload=%" FMT_OFF_T |
1497 | 0 | "/%" FMT_OFF_T "]", payload_offset, payload_len); |
1498 | | /* send back the exact same content as a PONG */ |
1499 | 0 | result = ws_enc_add_cntrl(ctx->data, ctx->ws, buf, buflen, CURLWS_PONG); |
1500 | 0 | if(result) |
1501 | 0 | return result; |
1502 | 0 | *pnwritten = buflen; |
1503 | 0 | } |
1504 | 0 | else { |
1505 | 0 | size_t write_len; |
1506 | |
|
1507 | 0 | ctx->written = TRUE; |
1508 | 0 | DEBUGASSERT(ctx->buflen >= ctx->bufidx); |
1509 | 0 | write_len = CURLMIN(buflen, ctx->buflen - ctx->bufidx); |
1510 | 0 | if(!write_len) { |
1511 | 0 | if(!buflen) /* 0 length write, we accept that */ |
1512 | 0 | return CURLE_OK; |
1513 | 0 | return CURLE_AGAIN; /* no more space */ |
1514 | 0 | } |
1515 | 0 | memcpy(ctx->buffer + ctx->bufidx, buf, write_len); |
1516 | 0 | ctx->bufidx += write_len; |
1517 | 0 | *pnwritten = write_len; |
1518 | 0 | } |
1519 | 0 | return result; |
1520 | 0 | } |
1521 | | |
1522 | | static CURLcode nw_in_recv(void *reader_ctx, |
1523 | | uint8_t *buf, size_t buflen, |
1524 | | size_t *pnread) |
1525 | 0 | { |
1526 | 0 | struct Curl_easy *data = reader_ctx; |
1527 | 0 | return curl_easy_recv(data, buf, buflen, pnread); |
1528 | 0 | } |
1529 | | |
1530 | | CURLcode curl_ws_recv(CURL *d, void *buffer, |
1531 | | size_t buflen, size_t *nread, |
1532 | | const struct curl_ws_frame **metap) |
1533 | 0 | { |
1534 | 0 | struct Curl_easy *data = d; |
1535 | 0 | struct connectdata *conn; |
1536 | 0 | struct websocket *ws; |
1537 | 0 | struct ws_collect ctx; |
1538 | |
|
1539 | 0 | *nread = 0; |
1540 | 0 | *metap = NULL; |
1541 | 0 | if(!GOOD_EASY_HANDLE(data) || (buflen && !buffer)) |
1542 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1543 | | |
1544 | 0 | conn = data->conn; |
1545 | 0 | if(!conn) { |
1546 | | /* Unhappy hack with lifetimes of transfers and connection */ |
1547 | 0 | if(!data->set.connect_only) { |
1548 | 0 | failf(data, "[WS] CONNECT_ONLY is required"); |
1549 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
1550 | 0 | } |
1551 | | |
1552 | 0 | Curl_getconnectinfo(data, &conn); |
1553 | 0 | if(!conn) { |
1554 | 0 | failf(data, "[WS] connection not found"); |
1555 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1556 | 0 | } |
1557 | 0 | } |
1558 | 0 | ws = Curl_conn_meta_get(conn, CURL_META_PROTO_WS_CONN); |
1559 | 0 | if(!ws) { |
1560 | 0 | failf(data, "[WS] connection is not setup for websocket"); |
1561 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1562 | 0 | } |
1563 | | |
1564 | 0 | memset(&ctx, 0, sizeof(ctx)); |
1565 | 0 | ctx.data = data; |
1566 | 0 | ctx.ws = ws; |
1567 | 0 | ctx.buffer = buffer; |
1568 | 0 | ctx.buflen = buflen; |
1569 | |
|
1570 | 0 | while(1) { |
1571 | 0 | CURLcode result; |
1572 | | |
1573 | | /* receive more when our buffer is empty */ |
1574 | 0 | if(Curl_bufq_is_empty(&ws->recvbuf)) { |
1575 | 0 | size_t n; |
1576 | 0 | result = Curl_bufq_slurp(&ws->recvbuf, nw_in_recv, data, &n); |
1577 | 0 | if(result) |
1578 | 0 | return result; |
1579 | 0 | else if(n == 0) { |
1580 | | /* connection closed */ |
1581 | 0 | infof(data, "[WS] connection expectedly closed?"); |
1582 | 0 | return CURLE_GOT_NOTHING; |
1583 | 0 | } |
1584 | 0 | CURL_TRC_WS(data, "curl_ws_recv, added %zu bytes from network", |
1585 | 0 | Curl_bufq_len(&ws->recvbuf)); |
1586 | 0 | } |
1587 | | |
1588 | 0 | result = ws_dec_pass(&ws->dec, data, &ws->recvbuf, |
1589 | 0 | ws_client_collect, &ctx); |
1590 | 0 | if(result == CURLE_AGAIN) { |
1591 | 0 | if(!ctx.written) { |
1592 | 0 | ws_dec_info(&ws->dec, data, "need more input"); |
1593 | 0 | continue; /* nothing written, try more input */ |
1594 | 0 | } |
1595 | 0 | break; |
1596 | 0 | } |
1597 | 0 | else if(result) { |
1598 | 0 | return result; |
1599 | 0 | } |
1600 | 0 | else if(ctx.written) { |
1601 | | /* The decoded frame is passed back to our caller. |
1602 | | * There are frames like PING were we auto-respond to and |
1603 | | * that we do not return. For these `ctx.written` is not set. */ |
1604 | 0 | break; |
1605 | 0 | } |
1606 | 0 | } |
1607 | | |
1608 | | /* update frame information to be passed back */ |
1609 | 0 | update_meta(ws, ctx.frame_age, ctx.frame_flags, ctx.payload_offset, |
1610 | 0 | ctx.payload_len, ctx.bufidx); |
1611 | 0 | *metap = &ws->recvframe; |
1612 | 0 | *nread = ws->recvframe.len; |
1613 | 0 | CURL_TRC_WS(data, "curl_ws_recv(len=%zu) -> %zu bytes (frame at %" |
1614 | 0 | FMT_OFF_T ", %" FMT_OFF_T " left)", |
1615 | 0 | buflen, *nread, ws->recvframe.offset, |
1616 | 0 | ws->recvframe.bytesleft); |
1617 | | /* all's well, try to send any pending control. we do not know |
1618 | | * when the application will call `curl_ws_send()` again. */ |
1619 | 0 | if(!data->set.ws_raw_mode && ws->pending.type) { |
1620 | 0 | CURLcode r2 = ws_enc_add_pending(data, ws); |
1621 | 0 | if(!r2) |
1622 | 0 | (void)ws_flush(data, ws, Curl_is_in_callback(data)); |
1623 | 0 | } |
1624 | 0 | return CURLE_OK; |
1625 | 0 | } |
1626 | | |
1627 | | static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws, |
1628 | | bool blocking) |
1629 | 0 | { |
1630 | 0 | if(!Curl_bufq_is_empty(&ws->sendbuf)) { |
1631 | 0 | CURLcode result; |
1632 | 0 | const uint8_t *out; |
1633 | 0 | size_t outlen, n; |
1634 | 0 | #ifdef DEBUGBUILD |
1635 | | /* Simulate a blocking send after this chunk has been sent */ |
1636 | 0 | bool eagain_next = FALSE; |
1637 | 0 | size_t chunk_egain = 0; |
1638 | 0 | const char *p = getenv("CURL_WS_CHUNK_EAGAIN"); |
1639 | 0 | if(p) { |
1640 | 0 | curl_off_t l; |
1641 | 0 | if(!curlx_str_number(&p, &l, 1 * 1024 * 1024)) |
1642 | 0 | chunk_egain = (size_t)l; |
1643 | 0 | } |
1644 | 0 | #endif |
1645 | |
|
1646 | 0 | while(Curl_bufq_peek(&ws->sendbuf, &out, &outlen)) { |
1647 | 0 | #ifdef DEBUGBUILD |
1648 | 0 | if(eagain_next) |
1649 | 0 | return CURLE_AGAIN; |
1650 | 0 | if(chunk_egain && (outlen > chunk_egain)) { |
1651 | 0 | outlen = chunk_egain; |
1652 | 0 | eagain_next = TRUE; |
1653 | 0 | } |
1654 | 0 | #endif |
1655 | 0 | if(blocking) { |
1656 | 0 | result = ws_send_raw_blocking(data, ws, (const char *)out, outlen); |
1657 | 0 | n = result ? 0 : outlen; |
1658 | 0 | } |
1659 | 0 | else if(data->set.connect_only || Curl_is_in_callback(data)) |
1660 | 0 | result = Curl_senddata(data, out, outlen, &n); |
1661 | 0 | else { |
1662 | 0 | result = Curl_xfer_send(data, out, outlen, FALSE, &n); |
1663 | 0 | if(!result && !n && outlen) |
1664 | 0 | result = CURLE_AGAIN; |
1665 | 0 | } |
1666 | |
|
1667 | 0 | if(result == CURLE_AGAIN) { |
1668 | 0 | CURL_TRC_WS(data, "flush EAGAIN, %zu bytes remain in buffer", |
1669 | 0 | Curl_bufq_len(&ws->sendbuf)); |
1670 | 0 | return result; |
1671 | 0 | } |
1672 | 0 | else if(result) { |
1673 | 0 | failf(data, "[WS] flush, write error %d", result); |
1674 | 0 | return result; |
1675 | 0 | } |
1676 | 0 | else { |
1677 | 0 | CURL_TRC_WS(data, "flushed %zu bytes", n); |
1678 | 0 | Curl_bufq_skip(&ws->sendbuf, n); |
1679 | 0 | } |
1680 | 0 | } |
1681 | 0 | } |
1682 | 0 | return CURLE_OK; |
1683 | 0 | } |
1684 | | |
1685 | | static CURLcode ws_send_raw_blocking(struct Curl_easy *data, |
1686 | | struct websocket *ws, |
1687 | | const char *buffer, size_t buflen) |
1688 | 0 | { |
1689 | 0 | CURLcode result = CURLE_OK; |
1690 | 0 | size_t nwritten; |
1691 | |
|
1692 | 0 | (void)ws; |
1693 | 0 | while(buflen) { |
1694 | 0 | result = Curl_xfer_send(data, buffer, buflen, FALSE, &nwritten); |
1695 | 0 | if(result) |
1696 | 0 | return result; |
1697 | 0 | DEBUGASSERT(nwritten <= buflen); |
1698 | 0 | buffer += nwritten; |
1699 | 0 | buflen -= nwritten; |
1700 | 0 | if(buflen) { |
1701 | 0 | curl_socket_t sock = data->conn->sock[FIRSTSOCKET]; |
1702 | 0 | timediff_t left_ms; |
1703 | 0 | int ev; |
1704 | |
|
1705 | 0 | CURL_TRC_WS(data, "ws_send_raw_blocking() partial, %zu left to send", |
1706 | 0 | buflen); |
1707 | 0 | left_ms = Curl_timeleft_ms(data); |
1708 | 0 | if(left_ms < 0) { |
1709 | 0 | failf(data, "[WS] Timeout waiting for socket becoming writable"); |
1710 | 0 | return CURLE_SEND_ERROR; |
1711 | 0 | } |
1712 | | |
1713 | | /* POLLOUT socket */ |
1714 | 0 | if(sock == CURL_SOCKET_BAD) |
1715 | 0 | return CURLE_SEND_ERROR; |
1716 | 0 | ev = SOCKET_WRITABLE(sock, left_ms ? left_ms : 500); |
1717 | 0 | if(ev < 0) { |
1718 | 0 | failf(data, "[WS] Error while waiting for socket becoming writable"); |
1719 | 0 | return CURLE_SEND_ERROR; |
1720 | 0 | } |
1721 | 0 | } |
1722 | 0 | } |
1723 | 0 | return result; |
1724 | 0 | } |
1725 | | |
1726 | | static CURLcode ws_send_raw(struct Curl_easy *data, const void *buffer, |
1727 | | size_t buflen, size_t *pnwritten) |
1728 | 0 | { |
1729 | 0 | struct websocket *ws; |
1730 | 0 | CURLcode result; |
1731 | |
|
1732 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1733 | 0 | if(!ws) { |
1734 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1735 | 0 | return CURLE_SEND_ERROR; |
1736 | 0 | } |
1737 | 0 | if(!buflen) |
1738 | 0 | return CURLE_OK; |
1739 | | |
1740 | 0 | if(Curl_is_in_callback(data)) { |
1741 | | /* When invoked from inside callbacks, we do a blocking send as the |
1742 | | * callback will probably not implement partial writes that may then |
1743 | | * mess up the ws framing subsequently. |
1744 | | * We need any pending data to be flushed before sending. */ |
1745 | 0 | result = ws_flush(data, ws, TRUE); |
1746 | 0 | if(result) |
1747 | 0 | return result; |
1748 | 0 | result = ws_send_raw_blocking(data, ws, buffer, buflen); |
1749 | 0 | } |
1750 | 0 | else { |
1751 | | /* We need any pending data to be sent or EAGAIN this call. */ |
1752 | 0 | result = ws_flush(data, ws, FALSE); |
1753 | 0 | if(result) |
1754 | 0 | return result; |
1755 | 0 | result = Curl_senddata(data, buffer, buflen, pnwritten); |
1756 | 0 | } |
1757 | | |
1758 | 0 | CURL_TRC_WS(data, "ws_send_raw(len=%zu) -> %d, %zu", |
1759 | 0 | buflen, result, *pnwritten); |
1760 | 0 | return result; |
1761 | 0 | } |
1762 | | |
1763 | | CURLcode curl_ws_send(CURL *d, const void *buffer_arg, |
1764 | | size_t buflen, size_t *sent, |
1765 | | curl_off_t fragsize, |
1766 | | unsigned int flags) |
1767 | 0 | { |
1768 | 0 | struct websocket *ws; |
1769 | 0 | const uint8_t *buffer = buffer_arg; |
1770 | 0 | CURLcode result = CURLE_OK; |
1771 | 0 | struct Curl_easy *data = d; |
1772 | 0 | size_t ndummy; |
1773 | 0 | size_t *pnsent = sent ? sent : &ndummy; |
1774 | |
|
1775 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1776 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1777 | 0 | CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T |
1778 | 0 | ", flags=%x), raw=%d", |
1779 | 0 | buflen, fragsize, flags, data->set.ws_raw_mode); |
1780 | |
|
1781 | 0 | *pnsent = 0; |
1782 | |
|
1783 | 0 | if(!buffer && buflen) { |
1784 | 0 | failf(data, "[WS] buffer is NULL when buflen is not"); |
1785 | 0 | result = CURLE_BAD_FUNCTION_ARGUMENT; |
1786 | 0 | goto out; |
1787 | 0 | } |
1788 | | |
1789 | 0 | if(!data->conn && data->set.connect_only) { |
1790 | 0 | result = Curl_connect_only_attach(data); |
1791 | 0 | if(result) |
1792 | 0 | goto out; |
1793 | 0 | } |
1794 | 0 | if(!data->conn) { |
1795 | 0 | failf(data, "[WS] No associated connection"); |
1796 | 0 | result = CURLE_SEND_ERROR; |
1797 | 0 | goto out; |
1798 | 0 | } |
1799 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1800 | 0 | if(!ws) { |
1801 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1802 | 0 | result = CURLE_SEND_ERROR; |
1803 | 0 | goto out; |
1804 | 0 | } |
1805 | | |
1806 | 0 | if(data->set.ws_raw_mode) { |
1807 | | /* In raw mode, we write directly to the connection */ |
1808 | | /* try flushing any content still waiting to be sent. */ |
1809 | 0 | result = ws_flush(data, ws, FALSE); |
1810 | 0 | if(result) |
1811 | 0 | goto out; |
1812 | | |
1813 | 0 | if(!buffer) { |
1814 | 0 | failf(data, "[WS] buffer is NULL in raw mode"); |
1815 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1816 | 0 | } |
1817 | 0 | if(!sent) { |
1818 | 0 | failf(data, "[WS] sent is NULL in raw mode"); |
1819 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1820 | 0 | } |
1821 | 0 | if(fragsize || flags) { |
1822 | 0 | failf(data, "[WS] fragsize and flags must be zero in raw mode"); |
1823 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1824 | 0 | } |
1825 | 0 | result = ws_send_raw(data, buffer, buflen, pnsent); |
1826 | 0 | goto out; |
1827 | 0 | } |
1828 | | |
1829 | | /* Not RAW mode, we do the frame encoding */ |
1830 | 0 | result = ws_enc_send(data, ws, buffer, buflen, fragsize, flags, pnsent); |
1831 | |
|
1832 | 0 | out: |
1833 | 0 | CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T |
1834 | 0 | ", flags=%x, raw=%d) -> %d, %zu", |
1835 | 0 | buflen, fragsize, flags, data->set.ws_raw_mode, result, |
1836 | 0 | *pnsent); |
1837 | 0 | return result; |
1838 | 0 | } |
1839 | | |
1840 | | static CURLcode ws_setup_conn(struct Curl_easy *data, |
1841 | | struct connectdata *conn) |
1842 | 2.79k | { |
1843 | | /* WebSocket is 1.1 only (for now) */ |
1844 | 2.79k | data->state.http_neg.accept_09 = FALSE; |
1845 | 2.79k | data->state.http_neg.only_10 = FALSE; |
1846 | 2.79k | data->state.http_neg.wanted = CURL_HTTP_V1x; |
1847 | 2.79k | data->state.http_neg.allowed = CURL_HTTP_V1x; |
1848 | 2.79k | return Curl_http_setup_conn(data, conn); |
1849 | 2.79k | } |
1850 | | |
1851 | | const struct curl_ws_frame *curl_ws_meta(CURL *d) |
1852 | 0 | { |
1853 | | /* we only return something for websocket, called from within the callback |
1854 | | when not using raw mode */ |
1855 | 0 | struct Curl_easy *data = d; |
1856 | 0 | if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && |
1857 | 0 | data->conn && !data->set.ws_raw_mode) { |
1858 | 0 | struct websocket *ws; |
1859 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1860 | 0 | if(ws) |
1861 | 0 | return &ws->recvframe; |
1862 | 0 | } |
1863 | 0 | return NULL; |
1864 | 0 | } |
1865 | | |
1866 | | CURL_EXTERN CURLcode curl_ws_start_frame(CURL *d, |
1867 | | unsigned int flags, |
1868 | | curl_off_t frame_len) |
1869 | 0 | { |
1870 | 0 | struct websocket *ws; |
1871 | 0 | CURLcode result = CURLE_OK; |
1872 | 0 | struct Curl_easy *data = d; |
1873 | |
|
1874 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1875 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1876 | 0 | if(data->set.ws_raw_mode) { |
1877 | 0 | failf(data, "cannot curl_ws_start_frame() with CURLWS_RAW_MODE enabled"); |
1878 | 0 | return CURLE_FAILED_INIT; |
1879 | 0 | } |
1880 | | |
1881 | 0 | CURL_TRC_WS(data, "curl_ws_start_frame(flags=%x, frame_len=%" FMT_OFF_T, |
1882 | 0 | flags, frame_len); |
1883 | |
|
1884 | 0 | if(!data->conn) { |
1885 | 0 | failf(data, "[WS] No associated connection"); |
1886 | 0 | result = CURLE_SEND_ERROR; |
1887 | 0 | goto out; |
1888 | 0 | } |
1889 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1890 | 0 | if(!ws) { |
1891 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1892 | 0 | result = CURLE_SEND_ERROR; |
1893 | 0 | goto out; |
1894 | 0 | } |
1895 | | |
1896 | 0 | if(data->set.ws_raw_mode) { |
1897 | 0 | failf(data, "[WS] cannot start frame in raw mode"); |
1898 | 0 | result = CURLE_SEND_ERROR; |
1899 | 0 | goto out; |
1900 | 0 | } |
1901 | | |
1902 | 0 | if(ws->enc.payload_remain) { |
1903 | 0 | failf(data, "[WS] previous frame not finished"); |
1904 | 0 | result = CURLE_SEND_ERROR; |
1905 | 0 | goto out; |
1906 | 0 | } |
1907 | | |
1908 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, flags, frame_len, |
1909 | 0 | &ws->sendbuf); |
1910 | 0 | if(result) |
1911 | 0 | CURL_TRC_WS(data, "curl_start_frame(), error adding frame head %d", |
1912 | 0 | result); |
1913 | |
|
1914 | 0 | out: |
1915 | 0 | return result; |
1916 | 0 | } |
1917 | | |
1918 | | static const struct Curl_protocol Curl_protocol_ws = { |
1919 | | ws_setup_conn, /* setup_connection */ |
1920 | | Curl_http, /* do_it */ |
1921 | | Curl_http_done, /* done */ |
1922 | | ZERO_NULL, /* do_more */ |
1923 | | ZERO_NULL, /* connect_it */ |
1924 | | ZERO_NULL, /* connecting */ |
1925 | | ZERO_NULL, /* doing */ |
1926 | | ZERO_NULL, /* proto_pollset */ |
1927 | | Curl_http_doing_pollset, /* doing_pollset */ |
1928 | | ZERO_NULL, /* domore_pollset */ |
1929 | | Curl_http_perform_pollset, /* perform_pollset */ |
1930 | | ZERO_NULL, /* disconnect */ |
1931 | | Curl_http_write_resp, /* write_resp */ |
1932 | | Curl_http_write_resp_hd, /* write_resp_hd */ |
1933 | | ZERO_NULL, /* connection_check */ |
1934 | | ZERO_NULL, /* attach connection */ |
1935 | | Curl_http_follow, /* follow */ |
1936 | | }; |
1937 | | |
1938 | | #else |
1939 | | |
1940 | | CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen, |
1941 | | size_t *nread, |
1942 | | const struct curl_ws_frame **metap) |
1943 | | { |
1944 | | (void)curl; |
1945 | | (void)buffer; |
1946 | | (void)buflen; |
1947 | | (void)nread; |
1948 | | (void)metap; |
1949 | | return CURLE_NOT_BUILT_IN; |
1950 | | } |
1951 | | |
1952 | | CURLcode curl_ws_send(CURL *curl, const void *buffer, |
1953 | | size_t buflen, size_t *sent, |
1954 | | curl_off_t fragsize, |
1955 | | unsigned int flags) |
1956 | | { |
1957 | | (void)curl; |
1958 | | (void)buffer; |
1959 | | (void)buflen; |
1960 | | (void)sent; |
1961 | | (void)fragsize; |
1962 | | (void)flags; |
1963 | | return CURLE_NOT_BUILT_IN; |
1964 | | } |
1965 | | |
1966 | | const struct curl_ws_frame *curl_ws_meta(CURL *data) |
1967 | | { |
1968 | | (void)data; |
1969 | | return NULL; |
1970 | | } |
1971 | | |
1972 | | CURL_EXTERN CURLcode curl_ws_start_frame(CURL *curl, |
1973 | | unsigned int flags, |
1974 | | curl_off_t frame_len) |
1975 | | { |
1976 | | (void)curl; |
1977 | | (void)flags; |
1978 | | (void)frame_len; |
1979 | | return CURLE_NOT_BUILT_IN; |
1980 | | } |
1981 | | |
1982 | | #endif /* !CURL_DISABLE_WEBSOCKETS */ |
1983 | | |
1984 | | const struct Curl_scheme Curl_scheme_ws = { |
1985 | | "WS", /* scheme */ |
1986 | | #ifdef CURL_DISABLE_WEBSOCKETS |
1987 | | ZERO_NULL, |
1988 | | #else |
1989 | | &Curl_protocol_ws, |
1990 | | #endif |
1991 | | CURLPROTO_WS, /* protocol */ |
1992 | | CURLPROTO_HTTP, /* family */ |
1993 | | PROTOPT_CREDSPERREQUEST | /* flags */ |
1994 | | PROTOPT_USERPWDCTRL, |
1995 | | PORT_HTTP /* defport */ |
1996 | | } |
1997 | | ; |
1998 | | |
1999 | | const struct Curl_scheme Curl_scheme_wss = { |
2000 | | "WSS", /* scheme */ |
2001 | | #if defined(CURL_DISABLE_WEBSOCKETS) || !defined(USE_SSL) |
2002 | | ZERO_NULL, |
2003 | | #else |
2004 | | &Curl_protocol_ws, |
2005 | | #endif |
2006 | | CURLPROTO_WSS, /* protocol */ |
2007 | | CURLPROTO_HTTP, /* family */ |
2008 | | PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */ |
2009 | | PROTOPT_USERPWDCTRL, |
2010 | | PORT_HTTPS /* defport */ |
2011 | | }; |