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