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 CURLcode ws_cw_dec_next(const unsigned char *buf, size_t buflen, |
651 | | int frame_age, int frame_flags, |
652 | | curl_off_t payload_offset, |
653 | | curl_off_t payload_len, |
654 | | void *user_data, |
655 | | size_t *pnwritten) |
656 | 0 | { |
657 | 0 | struct ws_cw_dec_ctx *ctx = user_data; |
658 | 0 | struct Curl_easy *data = ctx->data; |
659 | 0 | struct websocket *ws = ctx->ws; |
660 | 0 | bool auto_pong = !data->set.ws_no_auto_pong; |
661 | 0 | curl_off_t remain = (payload_len - (payload_offset + buflen)); |
662 | 0 | CURLcode result; |
663 | |
|
664 | 0 | (void)frame_age; |
665 | 0 | *pnwritten = 0; |
666 | |
|
667 | 0 | if(auto_pong && (frame_flags & CURLWS_PING) && !remain) { |
668 | | /* auto-respond to PINGs, only works for single-frame payloads atm */ |
669 | 0 | CURL_TRC_WS(data, "auto PONG to [PING payload=%" FMT_OFF_T |
670 | 0 | "/%" FMT_OFF_T "]", payload_offset, payload_len); |
671 | | /* send back the exact same content as a PONG */ |
672 | 0 | result = ws_enc_add_cntrl(data, ws, buf, buflen, CURLWS_PONG); |
673 | 0 | if(result) |
674 | 0 | return result; |
675 | 0 | } |
676 | 0 | else if(buflen || !remain) { |
677 | | /* forward the decoded frame to the next client writer. */ |
678 | 0 | update_meta(ws, frame_age, frame_flags, payload_offset, |
679 | 0 | payload_len, buflen); |
680 | |
|
681 | 0 | result = Curl_cwriter_write(data, ctx->next_writer, |
682 | 0 | (ctx->cw_type | CLIENTWRITE_0LEN), |
683 | 0 | (const char *)buf, buflen); |
684 | 0 | if(result) |
685 | 0 | return result; |
686 | 0 | } |
687 | 0 | *pnwritten = buflen; |
688 | 0 | return CURLE_OK; |
689 | 0 | } |
690 | | |
691 | | static CURLcode ws_cw_write(struct Curl_easy *data, |
692 | | struct Curl_cwriter *writer, int type, |
693 | | const char *buf, size_t nbytes) |
694 | 0 | { |
695 | 0 | struct ws_cw_ctx *ctx = writer->ctx; |
696 | 0 | struct websocket *ws; |
697 | 0 | CURLcode result; |
698 | |
|
699 | 0 | CURL_TRC_WRITE(data, "ws_cw_write(len=%zu, type=%d)", nbytes, type); |
700 | 0 | if(!(type & CLIENTWRITE_BODY) || data->set.ws_raw_mode) |
701 | 0 | return Curl_cwriter_write(data, writer->next, type, buf, nbytes); |
702 | | |
703 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
704 | 0 | if(!ws) { |
705 | 0 | failf(data, "[WS] not a websocket transfer"); |
706 | 0 | return CURLE_FAILED_INIT; |
707 | 0 | } |
708 | | |
709 | 0 | if(nbytes) { |
710 | 0 | size_t nwritten; |
711 | 0 | result = Curl_bufq_write(&ctx->buf, (const unsigned char *)buf, |
712 | 0 | nbytes, &nwritten); |
713 | 0 | if(result) { |
714 | 0 | infof(data, "[WS] error adding data to buffer %d", result); |
715 | 0 | return result; |
716 | 0 | } |
717 | 0 | } |
718 | | |
719 | 0 | while(!Curl_bufq_is_empty(&ctx->buf)) { |
720 | 0 | struct ws_cw_dec_ctx pass_ctx; |
721 | 0 | pass_ctx.data = data; |
722 | 0 | pass_ctx.ws = ws; |
723 | 0 | pass_ctx.next_writer = writer->next; |
724 | 0 | pass_ctx.cw_type = type; |
725 | 0 | result = ws_dec_pass(&ws->dec, data, &ctx->buf, |
726 | 0 | ws_cw_dec_next, &pass_ctx); |
727 | 0 | if(result == CURLE_AGAIN) { |
728 | | /* insufficient amount of data, keep it for later. |
729 | | * we pretend to have written all since we have a copy */ |
730 | 0 | return CURLE_OK; |
731 | 0 | } |
732 | 0 | else if(result) { |
733 | 0 | failf(data, "[WS] decode payload error %d", (int)result); |
734 | 0 | return result; |
735 | 0 | } |
736 | 0 | } |
737 | | |
738 | 0 | if((type & CLIENTWRITE_EOS) && !Curl_bufq_is_empty(&ctx->buf)) { |
739 | 0 | failf(data, "[WS] decode ending with %zd frame bytes remaining", |
740 | 0 | Curl_bufq_len(&ctx->buf)); |
741 | 0 | return CURLE_RECV_ERROR; |
742 | 0 | } |
743 | | |
744 | 0 | return CURLE_OK; |
745 | 0 | } |
746 | | |
747 | | /* WebSocket payload decoding client writer. */ |
748 | | static const struct Curl_cwtype ws_cw_decode = { |
749 | | "ws-decode", |
750 | | NULL, |
751 | | ws_cw_init, |
752 | | ws_cw_write, |
753 | | ws_cw_close, |
754 | | sizeof(struct ws_cw_ctx) |
755 | | }; |
756 | | |
757 | | |
758 | | static void ws_enc_info(struct ws_encoder *enc, struct Curl_easy *data, |
759 | | const char *msg) |
760 | 0 | { |
761 | 0 | CURL_TRC_WS(data, "WS-ENC: %s [%s%s payload=%" |
762 | 0 | FMT_OFF_T "/%" FMT_OFF_T "]", |
763 | 0 | msg, ws_frame_name_of_op(enc->firstbyte), |
764 | 0 | (enc->firstbyte & WSBIT_FIN) ? "" : " NON-FIN", |
765 | 0 | enc->payload_len - enc->payload_remain, enc->payload_len); |
766 | 0 | } |
767 | | |
768 | | static void ws_enc_reset(struct ws_encoder *enc) |
769 | 0 | { |
770 | 0 | enc->payload_remain = 0; |
771 | 0 | enc->xori = 0; |
772 | 0 | enc->contfragment = FALSE; |
773 | 0 | } |
774 | | |
775 | | static void ws_enc_init(struct ws_encoder *enc) |
776 | 0 | { |
777 | 0 | ws_enc_reset(enc); |
778 | 0 | } |
779 | | |
780 | | /*** |
781 | | RFC 6455 Section 5.2 |
782 | | |
783 | | 0 1 2 3 |
784 | | 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 |
785 | | +-+-+-+-+-------+-+-------------+-------------------------------+ |
786 | | |F|R|R|R| opcode|M| Payload len | Extended payload length | |
787 | | |I|S|S|S| (4) |A| (7) | (16/64) | |
788 | | |N|V|V|V| |S| | (if payload len==126/127) | |
789 | | | |1|2|3| |K| | | |
790 | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
791 | | | Extended payload length continued, if payload len == 127 | |
792 | | + - - - - - - - - - - - - - - - +-------------------------------+ |
793 | | | |Masking-key, if MASK set to 1 | |
794 | | +-------------------------------+-------------------------------+ |
795 | | | Masking-key (continued) | Payload Data | |
796 | | +-------------------------------- - - - - - - - - - - - - - - - + |
797 | | : Payload Data continued ... : |
798 | | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |
799 | | | Payload Data continued ... | |
800 | | +---------------------------------------------------------------+ |
801 | | */ |
802 | | |
803 | | static CURLcode ws_enc_add_frame(struct Curl_easy *data, |
804 | | struct ws_encoder *enc, |
805 | | unsigned int flags, |
806 | | curl_off_t payload_len, |
807 | | struct bufq *out) |
808 | 0 | { |
809 | 0 | unsigned char firstb = 0; |
810 | 0 | unsigned char head[14]; |
811 | 0 | CURLcode result; |
812 | 0 | size_t hlen, nwritten; |
813 | |
|
814 | 0 | if(payload_len < 0) { |
815 | 0 | failf(data, "[WS] starting new frame with negative payload length %" |
816 | 0 | FMT_OFF_T, payload_len); |
817 | 0 | return CURLE_SEND_ERROR; |
818 | 0 | } |
819 | | |
820 | 0 | if(enc->payload_remain > 0) { |
821 | | /* trying to write a new frame before the previous one is finished */ |
822 | 0 | failf(data, "[WS] starting new frame with %zd bytes from last one " |
823 | 0 | "remaining to be sent", (ssize_t)enc->payload_remain); |
824 | 0 | return CURLE_SEND_ERROR; |
825 | 0 | } |
826 | | |
827 | 0 | result = ws_frame_flags2firstbyte(data, flags, enc->contfragment, &firstb); |
828 | 0 | if(result) |
829 | 0 | return result; |
830 | | |
831 | | /* fragmentation only applies to data frames (text/binary); |
832 | | * control frames (close/ping/pong) do not affect the CONT status */ |
833 | 0 | if(flags & (CURLWS_TEXT | CURLWS_BINARY)) { |
834 | 0 | enc->contfragment = (flags & CURLWS_CONT) ? (bit)TRUE : (bit)FALSE; |
835 | 0 | } |
836 | |
|
837 | 0 | if(flags & CURLWS_PING && payload_len > WS_MAX_CNTRL_LEN) { |
838 | 0 | failf(data, "[WS] given PING frame is too big"); |
839 | 0 | return CURLE_TOO_LARGE; |
840 | 0 | } |
841 | 0 | if(flags & CURLWS_PONG && payload_len > WS_MAX_CNTRL_LEN) { |
842 | 0 | failf(data, "[WS] given PONG frame is too big"); |
843 | 0 | return CURLE_TOO_LARGE; |
844 | 0 | } |
845 | 0 | if(flags & CURLWS_CLOSE && payload_len > WS_MAX_CNTRL_LEN) { |
846 | 0 | failf(data, "[WS] given CLOSE frame is too big"); |
847 | 0 | return CURLE_TOO_LARGE; |
848 | 0 | } |
849 | | |
850 | 0 | head[0] = enc->firstbyte = firstb; |
851 | 0 | if(payload_len > 65535) { |
852 | 0 | head[1] = 127 | WSBIT_MASK; |
853 | 0 | head[2] = (unsigned char)((payload_len >> 56) & 0xff); |
854 | 0 | head[3] = (unsigned char)((payload_len >> 48) & 0xff); |
855 | 0 | head[4] = (unsigned char)((payload_len >> 40) & 0xff); |
856 | 0 | head[5] = (unsigned char)((payload_len >> 32) & 0xff); |
857 | 0 | head[6] = (unsigned char)((payload_len >> 24) & 0xff); |
858 | 0 | head[7] = (unsigned char)((payload_len >> 16) & 0xff); |
859 | 0 | head[8] = (unsigned char)((payload_len >> 8) & 0xff); |
860 | 0 | head[9] = (unsigned char)(payload_len & 0xff); |
861 | 0 | hlen = 10; |
862 | 0 | } |
863 | 0 | else if(payload_len >= 126) { |
864 | 0 | head[1] = 126 | WSBIT_MASK; |
865 | 0 | head[2] = (unsigned char)((payload_len >> 8) & 0xff); |
866 | 0 | head[3] = (unsigned char)(payload_len & 0xff); |
867 | 0 | hlen = 4; |
868 | 0 | } |
869 | 0 | else { |
870 | 0 | head[1] = (unsigned char)payload_len | WSBIT_MASK; |
871 | 0 | hlen = 2; |
872 | 0 | } |
873 | |
|
874 | 0 | enc->payload_remain = enc->payload_len = payload_len; |
875 | 0 | ws_enc_info(enc, data, "sending"); |
876 | | |
877 | | /* 4 bytes random */ |
878 | |
|
879 | 0 | result = Curl_rand(data, (unsigned char *)&enc->mask, sizeof(enc->mask)); |
880 | 0 | if(result) |
881 | 0 | return result; |
882 | | |
883 | | #ifdef DEBUGBUILD |
884 | | if(getenv("CURL_WS_FORCE_ZERO_MASK")) |
885 | | /* force the bit mask to 0x00000000, effectively disabling masking */ |
886 | | memset(&enc->mask, 0, sizeof(enc->mask)); |
887 | | #endif |
888 | | |
889 | | /* add 4 bytes mask */ |
890 | 0 | memcpy(&head[hlen], &enc->mask, 4); |
891 | 0 | hlen += 4; |
892 | | /* reset for payload to come */ |
893 | 0 | enc->xori = 0; |
894 | |
|
895 | 0 | result = Curl_bufq_write(out, head, hlen, &nwritten); |
896 | 0 | if(result) |
897 | 0 | return result; |
898 | 0 | if(nwritten != hlen) { |
899 | | /* We use a bufq with SOFT_LIMIT, writing should always succeed */ |
900 | 0 | DEBUGASSERT(0); |
901 | 0 | return CURLE_SEND_ERROR; |
902 | 0 | } |
903 | 0 | return CURLE_OK; |
904 | 0 | } |
905 | | |
906 | | static CURLcode ws_enc_write_head(struct Curl_easy *data, |
907 | | struct websocket *ws, |
908 | | struct ws_encoder *enc, |
909 | | unsigned int flags, |
910 | | curl_off_t payload_len, |
911 | | struct bufq *out) |
912 | 0 | { |
913 | | /* starting a new frame, we want a clean sendbuf. |
914 | | * Any pending control frame we can add now as part of the flush. */ |
915 | 0 | if(ws->pending.type) { |
916 | 0 | CURLcode result = ws_enc_add_pending(data, ws); |
917 | 0 | if(result) |
918 | 0 | return result; |
919 | 0 | } |
920 | 0 | return ws_enc_add_frame(data, enc, flags, payload_len, out); |
921 | 0 | } |
922 | | |
923 | | static CURLcode ws_enc_write_payload(struct ws_encoder *enc, |
924 | | struct Curl_easy *data, |
925 | | const unsigned char *buf, size_t buflen, |
926 | | struct bufq *out, size_t *pnwritten) |
927 | 0 | { |
928 | 0 | CURLcode result; |
929 | 0 | size_t i, len, n; |
930 | |
|
931 | 0 | *pnwritten = 0; |
932 | 0 | if(Curl_bufq_is_full(out)) |
933 | 0 | return CURLE_AGAIN; |
934 | | |
935 | | /* not the most performant way to do this */ |
936 | 0 | len = buflen; |
937 | 0 | if((curl_off_t)len > enc->payload_remain) |
938 | 0 | len = (size_t)enc->payload_remain; |
939 | |
|
940 | 0 | for(i = 0; i < len; ++i) { |
941 | 0 | unsigned char c = buf[i] ^ enc->mask[enc->xori]; |
942 | 0 | result = Curl_bufq_write(out, &c, 1, &n); |
943 | 0 | if(result) { |
944 | 0 | if((result != CURLE_AGAIN) || !i) |
945 | 0 | return result; |
946 | 0 | break; |
947 | 0 | } |
948 | 0 | enc->xori++; |
949 | 0 | enc->xori &= 3; |
950 | 0 | } |
951 | 0 | *pnwritten = i; |
952 | 0 | enc->payload_remain -= (curl_off_t)i; |
953 | 0 | ws_enc_info(enc, data, "buffered"); |
954 | 0 | return CURLE_OK; |
955 | 0 | } |
956 | | |
957 | | static CURLcode ws_enc_add_pending(struct Curl_easy *data, |
958 | | struct websocket *ws) |
959 | 0 | { |
960 | 0 | CURLcode result; |
961 | 0 | size_t n; |
962 | |
|
963 | 0 | if(!ws->pending.type) /* no pending frame here */ |
964 | 0 | return CURLE_OK; |
965 | 0 | if(ws->enc.payload_remain) /* in the middle of another frame */ |
966 | 0 | return CURLE_AGAIN; |
967 | | |
968 | 0 | result = ws_enc_add_frame(data, &ws->enc, ws->pending.type, |
969 | 0 | (curl_off_t)ws->pending.payload_len, |
970 | 0 | &ws->sendbuf); |
971 | 0 | if(result) { |
972 | 0 | CURL_TRC_WS(data, "ws_enc_cntrl(), error addiong head: %d", |
973 | 0 | result); |
974 | 0 | goto out; |
975 | 0 | } |
976 | 0 | result = ws_enc_write_payload(&ws->enc, data, ws->pending.payload, |
977 | 0 | ws->pending.payload_len, |
978 | 0 | &ws->sendbuf, &n); |
979 | 0 | if(result) { |
980 | 0 | CURL_TRC_WS(data, "ws_enc_cntrl(), error adding payload: %d", |
981 | 0 | result); |
982 | 0 | goto out; |
983 | 0 | } |
984 | | /* our buffer should always be able to take in a control frame */ |
985 | 0 | DEBUGASSERT(n == ws->pending.payload_len); |
986 | 0 | DEBUGASSERT(!ws->enc.payload_remain); |
987 | |
|
988 | 0 | out: |
989 | 0 | memset(&ws->pending, 0, sizeof(ws->pending)); |
990 | 0 | return result; |
991 | 0 | } |
992 | | |
993 | | static CURLcode ws_enc_send(struct Curl_easy *data, |
994 | | struct websocket *ws, |
995 | | const unsigned char *buffer, |
996 | | size_t buflen, |
997 | | curl_off_t fragsize, |
998 | | unsigned int flags, |
999 | | size_t *pnsent) |
1000 | 0 | { |
1001 | 0 | size_t n; |
1002 | 0 | CURLcode result = CURLE_OK; |
1003 | |
|
1004 | 0 | DEBUGASSERT(!data->set.ws_raw_mode); |
1005 | 0 | *pnsent = 0; |
1006 | |
|
1007 | 0 | if(ws->enc.payload_remain || !Curl_bufq_is_empty(&ws->sendbuf)) { |
1008 | | /* a frame is ongoing with payload buffered or more payload |
1009 | | * that needs to be encoded into the buffer */ |
1010 | 0 | if(buflen < ws->sendbuf_payload) { |
1011 | | /* We have been called with LESS buffer data than before. This |
1012 | | * is not how it's supposed too work. */ |
1013 | 0 | failf(data, "[WS] curl_ws_send() called with smaller 'buflen' than " |
1014 | 0 | "bytes already buffered in previous call, %zu vs %zu", |
1015 | 0 | buflen, ws->sendbuf_payload); |
1016 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1017 | 0 | } |
1018 | 0 | if((curl_off_t)buflen > |
1019 | 0 | (ws->enc.payload_remain + (curl_off_t)ws->sendbuf_payload)) { |
1020 | | /* too large buflen beyond payload length of frame */ |
1021 | 0 | failf(data, "[WS] unaligned frame size (sending %zu instead of %" |
1022 | 0 | FMT_OFF_T ")", |
1023 | 0 | buflen, ws->enc.payload_remain + ws->sendbuf_payload); |
1024 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | else { |
1028 | 0 | result = ws_flush(data, ws, Curl_is_in_callback(data)); |
1029 | 0 | if(result) |
1030 | 0 | return result; |
1031 | | |
1032 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, flags, |
1033 | 0 | (flags & CURLWS_OFFSET) ? |
1034 | 0 | fragsize : (curl_off_t)buflen, |
1035 | 0 | &ws->sendbuf); |
1036 | 0 | if(result) { |
1037 | 0 | CURL_TRC_WS(data, "curl_ws_send(), error writing frame head %d", result); |
1038 | 0 | return result; |
1039 | 0 | } |
1040 | 0 | } |
1041 | | |
1042 | | /* While there is either sendbuf to flush OR more payload to encode... */ |
1043 | 0 | while(!Curl_bufq_is_empty(&ws->sendbuf) || (buflen > ws->sendbuf_payload)) { |
1044 | | /* Try to add more payload to sendbuf */ |
1045 | 0 | if(buflen > ws->sendbuf_payload) { |
1046 | 0 | size_t prev_len = Curl_bufq_len(&ws->sendbuf); |
1047 | 0 | result = ws_enc_write_payload(&ws->enc, data, |
1048 | 0 | buffer + ws->sendbuf_payload, |
1049 | 0 | buflen - ws->sendbuf_payload, |
1050 | 0 | &ws->sendbuf, &n); |
1051 | 0 | if(result && (result != CURLE_AGAIN)) |
1052 | 0 | return result; |
1053 | 0 | ws->sendbuf_payload += Curl_bufq_len(&ws->sendbuf) - prev_len; |
1054 | 0 | if(!ws->sendbuf_payload) { |
1055 | 0 | return CURLE_AGAIN; |
1056 | 0 | } |
1057 | 0 | } |
1058 | | |
1059 | | /* flush, blocking when in callback */ |
1060 | 0 | result = ws_flush(data, ws, Curl_is_in_callback(data)); |
1061 | 0 | if(!result && ws->sendbuf_payload > 0) { |
1062 | 0 | *pnsent += ws->sendbuf_payload; |
1063 | 0 | buffer += ws->sendbuf_payload; |
1064 | 0 | buflen -= ws->sendbuf_payload; |
1065 | 0 | ws->sendbuf_payload = 0; |
1066 | 0 | } |
1067 | 0 | else if(result == CURLE_AGAIN) { |
1068 | 0 | if(ws->sendbuf_payload > Curl_bufq_len(&ws->sendbuf)) { |
1069 | | /* blocked, part of payload bytes remain, report length |
1070 | | * that we managed to send. */ |
1071 | 0 | size_t flushed = (ws->sendbuf_payload - Curl_bufq_len(&ws->sendbuf)); |
1072 | 0 | *pnsent += flushed; |
1073 | 0 | ws->sendbuf_payload -= flushed; |
1074 | 0 | return CURLE_OK; |
1075 | 0 | } |
1076 | 0 | else { |
1077 | | /* blocked before sending headers or 1st payload byte. We cannot report |
1078 | | * OK on 0-length send (caller counts only payload) and EAGAIN */ |
1079 | 0 | CURL_TRC_WS(data, "EAGAIN flushing sendbuf, payload_encoded: %zu/%zu", |
1080 | 0 | ws->sendbuf_payload, buflen); |
1081 | 0 | DEBUGASSERT(*pnsent == 0); |
1082 | 0 | return CURLE_AGAIN; |
1083 | 0 | } |
1084 | 0 | } |
1085 | 0 | else |
1086 | 0 | return result; /* real error sending the data */ |
1087 | 0 | } |
1088 | 0 | return CURLE_OK; |
1089 | 0 | } |
1090 | | |
1091 | | struct cr_ws_ctx { |
1092 | | struct Curl_creader super; |
1093 | | BIT(read_eos); /* we read an EOS from the next reader */ |
1094 | | BIT(eos); /* we have returned an EOS */ |
1095 | | }; |
1096 | | |
1097 | | static CURLcode cr_ws_init(struct Curl_easy *data, struct Curl_creader *reader) |
1098 | 0 | { |
1099 | 0 | (void)data; |
1100 | 0 | (void)reader; |
1101 | 0 | return CURLE_OK; |
1102 | 0 | } |
1103 | | |
1104 | | static void cr_ws_close(struct Curl_easy *data, struct Curl_creader *reader) |
1105 | 0 | { |
1106 | 0 | (void)data; |
1107 | 0 | (void)reader; |
1108 | 0 | } |
1109 | | |
1110 | | static CURLcode cr_ws_read(struct Curl_easy *data, |
1111 | | struct Curl_creader *reader, |
1112 | | char *buf, size_t blen, |
1113 | | size_t *pnread, bool *peos) |
1114 | 0 | { |
1115 | 0 | struct cr_ws_ctx *ctx = reader->ctx; |
1116 | 0 | CURLcode result = CURLE_OK; |
1117 | 0 | size_t nread, n; |
1118 | 0 | struct websocket *ws; |
1119 | 0 | bool eos; |
1120 | |
|
1121 | 0 | *pnread = 0; |
1122 | 0 | if(ctx->eos) { |
1123 | 0 | *peos = TRUE; |
1124 | 0 | return CURLE_OK; |
1125 | 0 | } |
1126 | | |
1127 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1128 | 0 | if(!ws) { |
1129 | 0 | failf(data, "[WS] not a websocket transfer"); |
1130 | 0 | return CURLE_FAILED_INIT; |
1131 | 0 | } |
1132 | | |
1133 | 0 | if(Curl_bufq_is_empty(&ws->sendbuf)) { |
1134 | 0 | if(ctx->read_eos) { |
1135 | 0 | ctx->eos = TRUE; |
1136 | 0 | *peos = TRUE; |
1137 | 0 | return CURLE_OK; |
1138 | 0 | } |
1139 | | |
1140 | 0 | if(ws->enc.payload_remain) { |
1141 | 0 | CURL_TRC_WS(data, "current frame, %" FMT_OFF_T " remaining", |
1142 | 0 | ws->enc.payload_remain); |
1143 | 0 | if(ws->enc.payload_remain < (curl_off_t)blen) |
1144 | 0 | blen = (size_t)ws->enc.payload_remain; |
1145 | 0 | } |
1146 | |
|
1147 | 0 | result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos); |
1148 | 0 | if(result) |
1149 | 0 | return result; |
1150 | 0 | ctx->read_eos = eos; |
1151 | |
|
1152 | 0 | if(!Curl_bufq_is_empty(&ws->sendbuf)) { |
1153 | | /* client_read started a new frame, we disregard any eos reported */ |
1154 | 0 | ctx->read_eos = FALSE; |
1155 | 0 | Curl_creader_clear_eos(data, reader->next); |
1156 | 0 | } |
1157 | 0 | else if(!nread) { |
1158 | | /* nothing to convert, return this right away */ |
1159 | 0 | if(ctx->read_eos) |
1160 | 0 | ctx->eos = TRUE; |
1161 | 0 | *pnread = nread; |
1162 | 0 | *peos = ctx->eos; |
1163 | 0 | goto out; |
1164 | 0 | } |
1165 | | |
1166 | 0 | if(!ws->enc.payload_remain && Curl_bufq_is_empty(&ws->sendbuf)) { |
1167 | | /* encode the data as a new BINARY frame */ |
1168 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, CURLWS_BINARY, nread, |
1169 | 0 | &ws->sendbuf); |
1170 | 0 | if(result) |
1171 | 0 | goto out; |
1172 | 0 | } |
1173 | | |
1174 | 0 | result = ws_enc_write_payload(&ws->enc, data, (unsigned char *)buf, |
1175 | 0 | nread, &ws->sendbuf, &n); |
1176 | 0 | if(result) |
1177 | 0 | goto out; |
1178 | 0 | CURL_TRC_READ(data, "cr_ws_read, added %zu payload, len=%zu", nread, n); |
1179 | 0 | } |
1180 | | |
1181 | 0 | DEBUGASSERT(!Curl_bufq_is_empty(&ws->sendbuf)); |
1182 | 0 | *peos = FALSE; |
1183 | 0 | result = Curl_bufq_cread(&ws->sendbuf, buf, blen, pnread); |
1184 | 0 | if(!result && ctx->read_eos && Curl_bufq_is_empty(&ws->sendbuf)) { |
1185 | | /* no more data, read all, done. */ |
1186 | 0 | ctx->eos = TRUE; |
1187 | 0 | *peos = TRUE; |
1188 | 0 | } |
1189 | |
|
1190 | 0 | out: |
1191 | 0 | CURL_TRC_READ(data, "cr_ws_read(len=%zu) -> %d, nread=%zu, eos=%d", |
1192 | 0 | blen, result, *pnread, *peos); |
1193 | 0 | return result; |
1194 | 0 | } |
1195 | | |
1196 | | static const struct Curl_crtype ws_cr_encode = { |
1197 | | "ws-encode", |
1198 | | cr_ws_init, |
1199 | | cr_ws_read, |
1200 | | cr_ws_close, |
1201 | | Curl_creader_def_needs_rewind, |
1202 | | Curl_creader_def_total_length, |
1203 | | Curl_creader_def_resume_from, |
1204 | | Curl_creader_def_cntrl, |
1205 | | Curl_creader_def_is_paused, |
1206 | | Curl_creader_def_done, |
1207 | | sizeof(struct cr_ws_ctx) |
1208 | | }; |
1209 | | |
1210 | | |
1211 | | struct wsfield { |
1212 | | const char *name; |
1213 | | const char *val; |
1214 | | }; |
1215 | | |
1216 | | CURLcode Curl_ws_request(struct Curl_easy *data, struct dynbuf *req) |
1217 | 0 | { |
1218 | 0 | unsigned int i; |
1219 | 0 | CURLcode result = CURLE_OK; |
1220 | 0 | unsigned char rand[16]; |
1221 | 0 | char *randstr; |
1222 | 0 | size_t randlen; |
1223 | 0 | char keyval[40]; |
1224 | 0 | struct SingleRequest *k = &data->req; |
1225 | 0 | struct wsfield heads[]= { |
1226 | 0 | { |
1227 | | /* The request MUST contain an |Upgrade| header field whose value |
1228 | | MUST include the "websocket" keyword. */ |
1229 | 0 | "Upgrade", "websocket" |
1230 | 0 | }, |
1231 | 0 | { |
1232 | | /* The request MUST include a header field with the name |
1233 | | |Sec-WebSocket-Version|. The value of this header field MUST be |
1234 | | 13. */ |
1235 | 0 | "Sec-WebSocket-Version", "13", |
1236 | 0 | }, |
1237 | 0 | { |
1238 | | /* The request MUST include a header field with the name |
1239 | | |Sec-WebSocket-Key|. The value of this header field MUST be a nonce |
1240 | | consisting of a randomly selected 16-byte value that has been |
1241 | | base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be |
1242 | | selected randomly for each connection. */ |
1243 | 0 | "Sec-WebSocket-Key", NULL, |
1244 | 0 | } |
1245 | 0 | }; |
1246 | 0 | heads[2].val = &keyval[0]; |
1247 | | |
1248 | | /* 16 bytes random */ |
1249 | 0 | result = Curl_rand(data, (unsigned char *)rand, sizeof(rand)); |
1250 | 0 | if(result) |
1251 | 0 | return result; |
1252 | 0 | result = curlx_base64_encode((char *)rand, sizeof(rand), &randstr, &randlen); |
1253 | 0 | if(result) |
1254 | 0 | return result; |
1255 | 0 | DEBUGASSERT(randlen < sizeof(keyval)); |
1256 | 0 | if(randlen >= sizeof(keyval)) { |
1257 | 0 | free(randstr); |
1258 | 0 | return CURLE_FAILED_INIT; |
1259 | 0 | } |
1260 | 0 | strcpy(keyval, randstr); |
1261 | 0 | free(randstr); |
1262 | 0 | for(i = 0; !result && (i < CURL_ARRAYSIZE(heads)); i++) { |
1263 | 0 | if(!Curl_checkheaders(data, heads[i].name, strlen(heads[i].name))) { |
1264 | 0 | result = curlx_dyn_addf(req, "%s: %s\r\n", heads[i].name, |
1265 | 0 | heads[i].val); |
1266 | 0 | } |
1267 | 0 | } |
1268 | 0 | data->state.http_hd_upgrade = TRUE; |
1269 | 0 | k->upgr101 = UPGR101_WS; |
1270 | 0 | data->conn->bits.upgrade_in_progress = TRUE; |
1271 | 0 | return result; |
1272 | 0 | } |
1273 | | |
1274 | | static void ws_conn_dtor(void *key, size_t klen, void *entry) |
1275 | 0 | { |
1276 | 0 | struct websocket *ws = entry; |
1277 | 0 | (void)key; |
1278 | 0 | (void)klen; |
1279 | 0 | Curl_bufq_free(&ws->recvbuf); |
1280 | 0 | Curl_bufq_free(&ws->sendbuf); |
1281 | 0 | free(ws); |
1282 | 0 | } |
1283 | | |
1284 | | /* |
1285 | | * 'nread' is number of bytes of websocket data already in the buffer at |
1286 | | * 'mem'. |
1287 | | */ |
1288 | | CURLcode Curl_ws_accept(struct Curl_easy *data, |
1289 | | const char *mem, size_t nread) |
1290 | 0 | { |
1291 | 0 | struct SingleRequest *k = &data->req; |
1292 | 0 | struct websocket *ws; |
1293 | 0 | struct Curl_cwriter *ws_dec_writer = NULL; |
1294 | 0 | struct Curl_creader *ws_enc_reader = NULL; |
1295 | 0 | CURLcode result; |
1296 | |
|
1297 | 0 | DEBUGASSERT(data->conn); |
1298 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1299 | 0 | if(!ws) { |
1300 | 0 | size_t chunk_size = WS_CHUNK_SIZE; |
1301 | 0 | ws = calloc(1, sizeof(*ws)); |
1302 | 0 | if(!ws) |
1303 | 0 | return CURLE_OUT_OF_MEMORY; |
1304 | | #ifdef DEBUGBUILD |
1305 | | { |
1306 | | const char *p = getenv("CURL_WS_CHUNK_SIZE"); |
1307 | | if(p) { |
1308 | | curl_off_t l; |
1309 | | if(!curlx_str_number(&p, &l, 1*1024*1024)) |
1310 | | chunk_size = (size_t)l; |
1311 | | } |
1312 | | } |
1313 | | #endif |
1314 | 0 | CURL_TRC_WS(data, "WS, using chunk size %zu", chunk_size); |
1315 | 0 | Curl_bufq_init2(&ws->recvbuf, chunk_size, WS_CHUNK_COUNT, |
1316 | 0 | BUFQ_OPT_SOFT_LIMIT); |
1317 | 0 | Curl_bufq_init2(&ws->sendbuf, chunk_size, WS_CHUNK_COUNT, |
1318 | 0 | BUFQ_OPT_SOFT_LIMIT); |
1319 | 0 | ws_dec_init(&ws->dec); |
1320 | 0 | ws_enc_init(&ws->enc); |
1321 | 0 | result = Curl_conn_meta_set(data->conn, CURL_META_PROTO_WS_CONN, |
1322 | 0 | ws, ws_conn_dtor); |
1323 | 0 | if(result) |
1324 | 0 | return result; |
1325 | 0 | } |
1326 | 0 | else { |
1327 | 0 | Curl_bufq_reset(&ws->recvbuf); |
1328 | 0 | ws_dec_reset(&ws->dec); |
1329 | 0 | ws_enc_reset(&ws->enc); |
1330 | 0 | } |
1331 | | /* Verify the Sec-WebSocket-Accept response. |
1332 | | |
1333 | | The sent value is the base64 encoded version of a SHA-1 hash done on the |
1334 | | |Sec-WebSocket-Key| header field concatenated with |
1335 | | the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11". |
1336 | | */ |
1337 | | |
1338 | | /* If the response includes a |Sec-WebSocket-Extensions| header field and |
1339 | | this header field indicates the use of an extension that was not present |
1340 | | in the client's handshake (the server has indicated an extension not |
1341 | | requested by the client), the client MUST Fail the WebSocket Connection. |
1342 | | */ |
1343 | | |
1344 | | /* If the response includes a |Sec-WebSocket-Protocol| header field |
1345 | | and this header field indicates the use of a subprotocol that was |
1346 | | not present in the client's handshake (the server has indicated a |
1347 | | subprotocol not requested by the client), the client MUST Fail |
1348 | | the WebSocket Connection. */ |
1349 | | |
1350 | 0 | infof(data, "[WS] Received 101, switch to WebSocket"); |
1351 | | |
1352 | | /* Install our client writer that decodes WS frames payload */ |
1353 | 0 | result = Curl_cwriter_create(&ws_dec_writer, data, &ws_cw_decode, |
1354 | 0 | CURL_CW_CONTENT_DECODE); |
1355 | 0 | if(result) |
1356 | 0 | goto out; |
1357 | 0 | result = Curl_cwriter_add(data, ws_dec_writer); |
1358 | 0 | if(result) |
1359 | 0 | goto out; |
1360 | 0 | ws_dec_writer = NULL; /* owned by transfer now */ |
1361 | |
|
1362 | 0 | k->header = FALSE; /* we will not get more response headers */ |
1363 | |
|
1364 | 0 | if(data->set.connect_only) { |
1365 | 0 | size_t nwritten; |
1366 | | /* In CONNECT_ONLY setup, the payloads from `mem` need to be received |
1367 | | * when using `curl_ws_recv` later on after this transfer is already |
1368 | | * marked as DONE. */ |
1369 | 0 | result = Curl_bufq_write(&ws->recvbuf, (const unsigned char *)mem, |
1370 | 0 | nread, &nwritten); |
1371 | 0 | if(result) |
1372 | 0 | goto out; |
1373 | 0 | DEBUGASSERT(nread == nwritten); |
1374 | 0 | k->keepon &= ~KEEP_RECV; /* read no more content */ |
1375 | 0 | } |
1376 | 0 | else { /* !connect_only */ |
1377 | 0 | if(data->set.method == HTTPREQ_PUT) { |
1378 | 0 | CURL_TRC_WS(data, "UPLOAD set, add ws-encode reader"); |
1379 | 0 | result = Curl_creader_set_fread(data, -1); |
1380 | 0 | if(result) |
1381 | 0 | goto out; |
1382 | | |
1383 | 0 | if(!data->set.ws_raw_mode) { |
1384 | | /* Add our client readerr encoding WS BINARY frames */ |
1385 | 0 | result = Curl_creader_create(&ws_enc_reader, data, &ws_cr_encode, |
1386 | 0 | CURL_CR_CONTENT_ENCODE); |
1387 | 0 | if(result) |
1388 | 0 | goto out; |
1389 | 0 | result = Curl_creader_add(data, ws_enc_reader); |
1390 | 0 | if(result) |
1391 | 0 | goto out; |
1392 | 0 | ws_enc_reader = NULL; /* owned by transfer now */ |
1393 | 0 | } |
1394 | | |
1395 | | /* start over with sending */ |
1396 | 0 | data->req.eos_read = FALSE; |
1397 | 0 | data->req.upload_done = FALSE; |
1398 | 0 | k->keepon |= KEEP_SEND; |
1399 | 0 | } |
1400 | | |
1401 | | /* And pass any additional data to the writers */ |
1402 | 0 | if(nread) { |
1403 | 0 | result = Curl_client_write(data, CLIENTWRITE_BODY, mem, nread); |
1404 | 0 | if(result) |
1405 | 0 | goto out; |
1406 | 0 | } |
1407 | 0 | } |
1408 | | |
1409 | 0 | k->upgr101 = UPGR101_RECEIVED; |
1410 | 0 | k->header = FALSE; /* we will not get more responses */ |
1411 | |
|
1412 | 0 | out: |
1413 | 0 | if(ws_dec_writer) |
1414 | 0 | Curl_cwriter_free(data, ws_dec_writer); |
1415 | 0 | if(ws_enc_reader) |
1416 | 0 | Curl_creader_free(data, ws_enc_reader); |
1417 | 0 | if(result) |
1418 | 0 | CURL_TRC_WS(data, "Curl_ws_accept() failed -> %d", result); |
1419 | 0 | else |
1420 | 0 | CURL_TRC_WS(data, "websocket established, %s mode", |
1421 | 0 | data->set.connect_only ? "connect-only" : "callback"); |
1422 | 0 | return result; |
1423 | 0 | } |
1424 | | |
1425 | | struct ws_collect { |
1426 | | struct Curl_easy *data; |
1427 | | struct websocket *ws; |
1428 | | unsigned char *buffer; |
1429 | | size_t buflen; |
1430 | | size_t bufidx; |
1431 | | int frame_age; |
1432 | | int frame_flags; |
1433 | | curl_off_t payload_offset; |
1434 | | curl_off_t payload_len; |
1435 | | bool written; |
1436 | | }; |
1437 | | |
1438 | | static CURLcode ws_client_collect(const unsigned char *buf, size_t buflen, |
1439 | | int frame_age, int frame_flags, |
1440 | | curl_off_t payload_offset, |
1441 | | curl_off_t payload_len, |
1442 | | void *userp, |
1443 | | size_t *pnwritten) |
1444 | 0 | { |
1445 | 0 | struct ws_collect *ctx = userp; |
1446 | 0 | struct Curl_easy *data = ctx->data; |
1447 | 0 | bool auto_pong = !data->set.ws_no_auto_pong; |
1448 | 0 | curl_off_t remain = (payload_len - (payload_offset + buflen)); |
1449 | 0 | CURLcode result = CURLE_OK; |
1450 | |
|
1451 | 0 | *pnwritten = 0; |
1452 | 0 | if(!ctx->bufidx) { |
1453 | | /* first write */ |
1454 | 0 | ctx->frame_age = frame_age; |
1455 | 0 | ctx->frame_flags = frame_flags; |
1456 | 0 | ctx->payload_offset = payload_offset; |
1457 | 0 | ctx->payload_len = payload_len; |
1458 | 0 | } |
1459 | |
|
1460 | 0 | if(auto_pong && (frame_flags & CURLWS_PING) && !remain) { |
1461 | | /* auto-respond to PINGs, only works for single-frame payloads atm */ |
1462 | 0 | CURL_TRC_WS(data, "auto PONG to [PING payload=%" FMT_OFF_T |
1463 | 0 | "/%" FMT_OFF_T "]", payload_offset, payload_len); |
1464 | | /* send back the exact same content as a PONG */ |
1465 | 0 | result = ws_enc_add_cntrl(ctx->data, ctx->ws, buf, buflen, CURLWS_PONG); |
1466 | 0 | if(result) |
1467 | 0 | return result; |
1468 | 0 | *pnwritten = buflen; |
1469 | 0 | } |
1470 | 0 | else { |
1471 | 0 | size_t write_len; |
1472 | |
|
1473 | 0 | ctx->written = TRUE; |
1474 | 0 | DEBUGASSERT(ctx->buflen >= ctx->bufidx); |
1475 | 0 | write_len = CURLMIN(buflen, ctx->buflen - ctx->bufidx); |
1476 | 0 | if(!write_len) { |
1477 | 0 | if(!buflen) /* 0 length write, we accept that */ |
1478 | 0 | return CURLE_OK; |
1479 | 0 | return CURLE_AGAIN; /* no more space */ |
1480 | 0 | } |
1481 | 0 | memcpy(ctx->buffer + ctx->bufidx, buf, write_len); |
1482 | 0 | ctx->bufidx += write_len; |
1483 | 0 | *pnwritten = write_len; |
1484 | 0 | } |
1485 | 0 | return result; |
1486 | 0 | } |
1487 | | |
1488 | | static CURLcode nw_in_recv(void *reader_ctx, |
1489 | | unsigned char *buf, size_t buflen, |
1490 | | size_t *pnread) |
1491 | 0 | { |
1492 | 0 | struct Curl_easy *data = reader_ctx; |
1493 | 0 | return curl_easy_recv(data, buf, buflen, pnread); |
1494 | 0 | } |
1495 | | |
1496 | | CURLcode curl_ws_recv(CURL *d, void *buffer, |
1497 | | size_t buflen, size_t *nread, |
1498 | | const struct curl_ws_frame **metap) |
1499 | 0 | { |
1500 | 0 | struct Curl_easy *data = d; |
1501 | 0 | struct connectdata *conn; |
1502 | 0 | struct websocket *ws; |
1503 | 0 | struct ws_collect ctx; |
1504 | |
|
1505 | 0 | *nread = 0; |
1506 | 0 | *metap = NULL; |
1507 | 0 | if(!GOOD_EASY_HANDLE(data) || (buflen && !buffer)) |
1508 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1509 | | |
1510 | 0 | conn = data->conn; |
1511 | 0 | if(!conn) { |
1512 | | /* Unhappy hack with lifetimes of transfers and connection */ |
1513 | 0 | if(!data->set.connect_only) { |
1514 | 0 | failf(data, "[WS] CONNECT_ONLY is required"); |
1515 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
1516 | 0 | } |
1517 | | |
1518 | 0 | Curl_getconnectinfo(data, &conn); |
1519 | 0 | if(!conn) { |
1520 | 0 | failf(data, "[WS] connection not found"); |
1521 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1522 | 0 | } |
1523 | 0 | } |
1524 | 0 | ws = Curl_conn_meta_get(conn, CURL_META_PROTO_WS_CONN); |
1525 | 0 | if(!ws) { |
1526 | 0 | failf(data, "[WS] connection is not setup for websocket"); |
1527 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1528 | 0 | } |
1529 | | |
1530 | | |
1531 | 0 | memset(&ctx, 0, sizeof(ctx)); |
1532 | 0 | ctx.data = data; |
1533 | 0 | ctx.ws = ws; |
1534 | 0 | ctx.buffer = buffer; |
1535 | 0 | ctx.buflen = buflen; |
1536 | |
|
1537 | 0 | while(1) { |
1538 | 0 | CURLcode result; |
1539 | | |
1540 | | /* receive more when our buffer is empty */ |
1541 | 0 | if(Curl_bufq_is_empty(&ws->recvbuf)) { |
1542 | 0 | size_t n; |
1543 | 0 | result = Curl_bufq_slurp(&ws->recvbuf, nw_in_recv, data, &n); |
1544 | 0 | if(result) |
1545 | 0 | return result; |
1546 | 0 | else if(n == 0) { |
1547 | | /* connection closed */ |
1548 | 0 | infof(data, "[WS] connection expectedly closed?"); |
1549 | 0 | return CURLE_GOT_NOTHING; |
1550 | 0 | } |
1551 | 0 | CURL_TRC_WS(data, "curl_ws_recv, added %zu bytes from network", |
1552 | 0 | Curl_bufq_len(&ws->recvbuf)); |
1553 | 0 | } |
1554 | | |
1555 | 0 | result = ws_dec_pass(&ws->dec, data, &ws->recvbuf, |
1556 | 0 | ws_client_collect, &ctx); |
1557 | 0 | if(result == CURLE_AGAIN) { |
1558 | 0 | if(!ctx.written) { |
1559 | 0 | ws_dec_info(&ws->dec, data, "need more input"); |
1560 | 0 | continue; /* nothing written, try more input */ |
1561 | 0 | } |
1562 | 0 | break; |
1563 | 0 | } |
1564 | 0 | else if(result) { |
1565 | 0 | return result; |
1566 | 0 | } |
1567 | 0 | else if(ctx.written) { |
1568 | | /* The decoded frame is passed back to our caller. |
1569 | | * There are frames like PING were we auto-respond to and |
1570 | | * that we do not return. For these `ctx.written` is not set. */ |
1571 | 0 | break; |
1572 | 0 | } |
1573 | 0 | } |
1574 | | |
1575 | | /* update frame information to be passed back */ |
1576 | 0 | update_meta(ws, ctx.frame_age, ctx.frame_flags, ctx.payload_offset, |
1577 | 0 | ctx.payload_len, ctx.bufidx); |
1578 | 0 | *metap = &ws->recvframe; |
1579 | 0 | *nread = ws->recvframe.len; |
1580 | 0 | CURL_TRC_WS(data, "curl_ws_recv(len=%zu) -> %zu bytes (frame at %" |
1581 | 0 | FMT_OFF_T ", %" FMT_OFF_T " left)", |
1582 | 0 | buflen, *nread, ws->recvframe.offset, |
1583 | 0 | ws->recvframe.bytesleft); |
1584 | | /* all's well, try to send any pending control. we do not know |
1585 | | * when the application will call `curl_ws_send()` again. */ |
1586 | 0 | if(!data->set.ws_raw_mode && ws->pending.type) { |
1587 | 0 | CURLcode r2 = ws_enc_add_pending(data, ws); |
1588 | 0 | if(!r2) |
1589 | 0 | (void)ws_flush(data, ws, Curl_is_in_callback(data)); |
1590 | 0 | } |
1591 | 0 | return CURLE_OK; |
1592 | 0 | } |
1593 | | |
1594 | | static CURLcode ws_flush(struct Curl_easy *data, struct websocket *ws, |
1595 | | bool blocking) |
1596 | 0 | { |
1597 | 0 | if(!Curl_bufq_is_empty(&ws->sendbuf)) { |
1598 | 0 | CURLcode result; |
1599 | 0 | const unsigned char *out; |
1600 | 0 | size_t outlen, n; |
1601 | | #ifdef DEBUGBUILD |
1602 | | /* Simulate a blocking send after this chunk has been sent */ |
1603 | | bool eagain_next = FALSE; |
1604 | | size_t chunk_egain = 0; |
1605 | | const char *p = getenv("CURL_WS_CHUNK_EAGAIN"); |
1606 | | if(p) { |
1607 | | curl_off_t l; |
1608 | | if(!curlx_str_number(&p, &l, 1*1024*1024)) |
1609 | | chunk_egain = (size_t)l; |
1610 | | } |
1611 | | #endif |
1612 | |
|
1613 | 0 | while(Curl_bufq_peek(&ws->sendbuf, &out, &outlen)) { |
1614 | | #ifdef DEBUGBUILD |
1615 | | if(eagain_next) |
1616 | | return CURLE_AGAIN; |
1617 | | if(chunk_egain && (outlen > chunk_egain)) { |
1618 | | outlen = chunk_egain; |
1619 | | eagain_next = TRUE; |
1620 | | } |
1621 | | #endif |
1622 | 0 | if(blocking) { |
1623 | 0 | result = ws_send_raw_blocking(data, ws, (const char *)out, outlen); |
1624 | 0 | n = result ? 0 : outlen; |
1625 | 0 | } |
1626 | 0 | else if(data->set.connect_only || Curl_is_in_callback(data)) |
1627 | 0 | result = Curl_senddata(data, out, outlen, &n); |
1628 | 0 | else { |
1629 | 0 | result = Curl_xfer_send(data, out, outlen, FALSE, &n); |
1630 | 0 | if(!result && !n && outlen) |
1631 | 0 | result = CURLE_AGAIN; |
1632 | 0 | } |
1633 | |
|
1634 | 0 | if(result == CURLE_AGAIN) { |
1635 | 0 | CURL_TRC_WS(data, "flush EAGAIN, %zu bytes remain in buffer", |
1636 | 0 | Curl_bufq_len(&ws->sendbuf)); |
1637 | 0 | return result; |
1638 | 0 | } |
1639 | 0 | else if(result) { |
1640 | 0 | failf(data, "[WS] flush, write error %d", result); |
1641 | 0 | return result; |
1642 | 0 | } |
1643 | 0 | else { |
1644 | 0 | CURL_TRC_WS(data, "flushed %zu bytes", n); |
1645 | 0 | Curl_bufq_skip(&ws->sendbuf, n); |
1646 | 0 | } |
1647 | 0 | } |
1648 | 0 | } |
1649 | 0 | return CURLE_OK; |
1650 | 0 | } |
1651 | | |
1652 | | static CURLcode ws_send_raw_blocking(struct Curl_easy *data, |
1653 | | struct websocket *ws, |
1654 | | const char *buffer, size_t buflen) |
1655 | 0 | { |
1656 | 0 | CURLcode result = CURLE_OK; |
1657 | 0 | size_t nwritten; |
1658 | |
|
1659 | 0 | (void)ws; |
1660 | 0 | while(buflen) { |
1661 | 0 | result = Curl_xfer_send(data, buffer, buflen, FALSE, &nwritten); |
1662 | 0 | if(result) |
1663 | 0 | return result; |
1664 | 0 | DEBUGASSERT(nwritten <= buflen); |
1665 | 0 | buffer += nwritten; |
1666 | 0 | buflen -= nwritten; |
1667 | 0 | if(buflen) { |
1668 | 0 | curl_socket_t sock = data->conn->sock[FIRSTSOCKET]; |
1669 | 0 | timediff_t left_ms; |
1670 | 0 | int ev; |
1671 | |
|
1672 | 0 | CURL_TRC_WS(data, "ws_send_raw_blocking() partial, %zu left to send", |
1673 | 0 | buflen); |
1674 | 0 | left_ms = Curl_timeleft(data, NULL, FALSE); |
1675 | 0 | if(left_ms < 0) { |
1676 | 0 | failf(data, "[WS] Timeout waiting for socket becoming writable"); |
1677 | 0 | return CURLE_SEND_ERROR; |
1678 | 0 | } |
1679 | | |
1680 | | /* POLLOUT socket */ |
1681 | 0 | if(sock == CURL_SOCKET_BAD) |
1682 | 0 | return CURLE_SEND_ERROR; |
1683 | 0 | ev = Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, sock, |
1684 | 0 | left_ms ? left_ms : 500); |
1685 | 0 | if(ev < 0) { |
1686 | 0 | failf(data, "[WS] Error while waiting for socket becoming writable"); |
1687 | 0 | return CURLE_SEND_ERROR; |
1688 | 0 | } |
1689 | 0 | } |
1690 | 0 | } |
1691 | 0 | return result; |
1692 | 0 | } |
1693 | | |
1694 | | static CURLcode ws_send_raw(struct Curl_easy *data, const void *buffer, |
1695 | | size_t buflen, size_t *pnwritten) |
1696 | 0 | { |
1697 | 0 | struct websocket *ws; |
1698 | 0 | CURLcode result; |
1699 | |
|
1700 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1701 | 0 | if(!ws) { |
1702 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1703 | 0 | return CURLE_SEND_ERROR; |
1704 | 0 | } |
1705 | 0 | if(!buflen) |
1706 | 0 | return CURLE_OK; |
1707 | | |
1708 | 0 | if(Curl_is_in_callback(data)) { |
1709 | | /* When invoked from inside callbacks, we do a blocking send as the |
1710 | | * callback will probably not implement partial writes that may then |
1711 | | * mess up the ws framing subsequently. |
1712 | | * We need any pending data to be flushed before sending. */ |
1713 | 0 | result = ws_flush(data, ws, TRUE); |
1714 | 0 | if(result) |
1715 | 0 | return result; |
1716 | 0 | result = ws_send_raw_blocking(data, ws, buffer, buflen); |
1717 | 0 | } |
1718 | 0 | else { |
1719 | | /* We need any pending data to be sent or EAGAIN this call. */ |
1720 | 0 | result = ws_flush(data, ws, FALSE); |
1721 | 0 | if(result) |
1722 | 0 | return result; |
1723 | 0 | result = Curl_senddata(data, buffer, buflen, pnwritten); |
1724 | 0 | } |
1725 | | |
1726 | 0 | CURL_TRC_WS(data, "ws_send_raw(len=%zu) -> %d, %zu", |
1727 | 0 | buflen, result, *pnwritten); |
1728 | 0 | return result; |
1729 | 0 | } |
1730 | | |
1731 | | CURLcode curl_ws_send(CURL *d, const void *buffer_arg, |
1732 | | size_t buflen, size_t *sent, |
1733 | | curl_off_t fragsize, |
1734 | | unsigned int flags) |
1735 | 0 | { |
1736 | 0 | struct websocket *ws; |
1737 | 0 | const unsigned char *buffer = buffer_arg; |
1738 | 0 | CURLcode result = CURLE_OK; |
1739 | 0 | struct Curl_easy *data = d; |
1740 | 0 | size_t ndummy; |
1741 | 0 | size_t *pnsent = sent ? sent : &ndummy; |
1742 | |
|
1743 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1744 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1745 | 0 | CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T |
1746 | 0 | ", flags=%x), raw=%d", |
1747 | 0 | buflen, fragsize, flags, data->set.ws_raw_mode); |
1748 | |
|
1749 | 0 | *pnsent = 0; |
1750 | |
|
1751 | 0 | if(!buffer && buflen) { |
1752 | 0 | failf(data, "[WS] buffer is NULL when buflen is not"); |
1753 | 0 | result = CURLE_BAD_FUNCTION_ARGUMENT; |
1754 | 0 | goto out; |
1755 | 0 | } |
1756 | | |
1757 | 0 | if(!data->conn && data->set.connect_only) { |
1758 | 0 | result = Curl_connect_only_attach(data); |
1759 | 0 | if(result) |
1760 | 0 | goto out; |
1761 | 0 | } |
1762 | 0 | if(!data->conn) { |
1763 | 0 | failf(data, "[WS] No associated connection"); |
1764 | 0 | result = CURLE_SEND_ERROR; |
1765 | 0 | goto out; |
1766 | 0 | } |
1767 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1768 | 0 | if(!ws) { |
1769 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1770 | 0 | result = CURLE_SEND_ERROR; |
1771 | 0 | goto out; |
1772 | 0 | } |
1773 | | |
1774 | 0 | if(data->set.ws_raw_mode) { |
1775 | | /* In raw mode, we write directly to the connection */ |
1776 | | /* try flushing any content still waiting to be sent. */ |
1777 | 0 | result = ws_flush(data, ws, FALSE); |
1778 | 0 | if(result) |
1779 | 0 | goto out; |
1780 | | |
1781 | 0 | if(!buffer) { |
1782 | 0 | failf(data, "[WS] buffer is NULL in raw mode"); |
1783 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1784 | 0 | } |
1785 | 0 | if(!sent) { |
1786 | 0 | failf(data, "[WS] sent is NULL in raw mode"); |
1787 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1788 | 0 | } |
1789 | 0 | if(fragsize || flags) { |
1790 | 0 | failf(data, "[WS] fragsize and flags must be zero in raw mode"); |
1791 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1792 | 0 | } |
1793 | 0 | result = ws_send_raw(data, buffer, buflen, pnsent); |
1794 | 0 | goto out; |
1795 | 0 | } |
1796 | | |
1797 | | /* Not RAW mode, we do the frame encoding */ |
1798 | 0 | result = ws_enc_send(data, ws, buffer, buflen, fragsize, flags, pnsent); |
1799 | |
|
1800 | 0 | out: |
1801 | 0 | CURL_TRC_WS(data, "curl_ws_send(len=%zu, fragsize=%" FMT_OFF_T |
1802 | 0 | ", flags=%x, raw=%d) -> %d, %zu", |
1803 | 0 | buflen, fragsize, flags, data->set.ws_raw_mode, result, |
1804 | 0 | *pnsent); |
1805 | 0 | return result; |
1806 | 0 | } |
1807 | | |
1808 | | static CURLcode ws_setup_conn(struct Curl_easy *data, |
1809 | | struct connectdata *conn) |
1810 | 0 | { |
1811 | | /* WebSocket is 1.1 only (for now) */ |
1812 | 0 | data->state.http_neg.accept_09 = FALSE; |
1813 | 0 | data->state.http_neg.only_10 = FALSE; |
1814 | 0 | data->state.http_neg.wanted = CURL_HTTP_V1x; |
1815 | 0 | data->state.http_neg.allowed = CURL_HTTP_V1x; |
1816 | 0 | return Curl_http_setup_conn(data, conn); |
1817 | 0 | } |
1818 | | |
1819 | | |
1820 | | const struct curl_ws_frame *curl_ws_meta(CURL *d) |
1821 | 0 | { |
1822 | | /* we only return something for websocket, called from within the callback |
1823 | | when not using raw mode */ |
1824 | 0 | struct Curl_easy *data = d; |
1825 | 0 | if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && |
1826 | 0 | data->conn && !data->set.ws_raw_mode) { |
1827 | 0 | struct websocket *ws; |
1828 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1829 | 0 | if(ws) |
1830 | 0 | return &ws->recvframe; |
1831 | |
|
1832 | 0 | } |
1833 | 0 | return NULL; |
1834 | 0 | } |
1835 | | |
1836 | | CURL_EXTERN CURLcode curl_ws_start_frame(CURL *d, |
1837 | | unsigned int flags, |
1838 | | curl_off_t frame_len) |
1839 | 0 | { |
1840 | 0 | struct websocket *ws; |
1841 | 0 | CURLcode result = CURLE_OK; |
1842 | 0 | struct Curl_easy *data = d; |
1843 | |
|
1844 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1845 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1846 | 0 | if(data->set.ws_raw_mode) { |
1847 | 0 | failf(data, "cannot curl_ws_start_frame() with CURLWS_RAW_MODE enabled"); |
1848 | 0 | return CURLE_FAILED_INIT; |
1849 | 0 | } |
1850 | | |
1851 | 0 | CURL_TRC_WS(data, "curl_ws_start_frame(flags=%x, frame_len=%" FMT_OFF_T, |
1852 | 0 | flags, frame_len); |
1853 | |
|
1854 | 0 | if(!data->conn) { |
1855 | 0 | failf(data, "[WS] No associated connection"); |
1856 | 0 | result = CURLE_SEND_ERROR; |
1857 | 0 | goto out; |
1858 | 0 | } |
1859 | 0 | ws = Curl_conn_meta_get(data->conn, CURL_META_PROTO_WS_CONN); |
1860 | 0 | if(!ws) { |
1861 | 0 | failf(data, "[WS] Not a websocket transfer"); |
1862 | 0 | result = CURLE_SEND_ERROR; |
1863 | 0 | goto out; |
1864 | 0 | } |
1865 | | |
1866 | 0 | if(data->set.ws_raw_mode) { |
1867 | 0 | failf(data, "[WS] cannot start frame in raw mode"); |
1868 | 0 | result = CURLE_SEND_ERROR; |
1869 | 0 | goto out; |
1870 | 0 | } |
1871 | | |
1872 | 0 | if(ws->enc.payload_remain) { |
1873 | 0 | failf(data, "[WS] previous frame not finished"); |
1874 | 0 | result = CURLE_SEND_ERROR; |
1875 | 0 | goto out; |
1876 | 0 | } |
1877 | | |
1878 | 0 | result = ws_enc_write_head(data, ws, &ws->enc, flags, frame_len, |
1879 | 0 | &ws->sendbuf); |
1880 | 0 | if(result) |
1881 | 0 | CURL_TRC_WS(data, "curl_start_frame(), error adding frame head %d", |
1882 | 0 | result); |
1883 | |
|
1884 | 0 | out: |
1885 | 0 | return result; |
1886 | 0 | } |
1887 | | |
1888 | | const struct Curl_handler Curl_handler_ws = { |
1889 | | "WS", /* scheme */ |
1890 | | ws_setup_conn, /* setup_connection */ |
1891 | | Curl_http, /* do_it */ |
1892 | | Curl_http_done, /* done */ |
1893 | | ZERO_NULL, /* do_more */ |
1894 | | Curl_http_connect, /* connect_it */ |
1895 | | ZERO_NULL, /* connecting */ |
1896 | | ZERO_NULL, /* doing */ |
1897 | | ZERO_NULL, /* proto_pollset */ |
1898 | | Curl_http_do_pollset, /* doing_pollset */ |
1899 | | ZERO_NULL, /* domore_pollset */ |
1900 | | ZERO_NULL, /* perform_pollset */ |
1901 | | ZERO_NULL, /* disconnect */ |
1902 | | Curl_http_write_resp, /* write_resp */ |
1903 | | Curl_http_write_resp_hd, /* write_resp_hd */ |
1904 | | ZERO_NULL, /* connection_check */ |
1905 | | ZERO_NULL, /* attach connection */ |
1906 | | Curl_http_follow, /* follow */ |
1907 | | PORT_HTTP, /* defport */ |
1908 | | CURLPROTO_WS, /* protocol */ |
1909 | | CURLPROTO_HTTP, /* family */ |
1910 | | PROTOPT_CREDSPERREQUEST | /* flags */ |
1911 | | PROTOPT_USERPWDCTRL |
1912 | | }; |
1913 | | |
1914 | | #ifdef USE_SSL |
1915 | | const struct Curl_handler Curl_handler_wss = { |
1916 | | "WSS", /* scheme */ |
1917 | | ws_setup_conn, /* setup_connection */ |
1918 | | Curl_http, /* do_it */ |
1919 | | Curl_http_done, /* done */ |
1920 | | ZERO_NULL, /* do_more */ |
1921 | | Curl_http_connect, /* connect_it */ |
1922 | | NULL, /* connecting */ |
1923 | | ZERO_NULL, /* doing */ |
1924 | | NULL, /* proto_pollset */ |
1925 | | Curl_http_do_pollset, /* doing_pollset */ |
1926 | | ZERO_NULL, /* domore_pollset */ |
1927 | | ZERO_NULL, /* perform_pollset */ |
1928 | | ZERO_NULL, /* disconnect */ |
1929 | | Curl_http_write_resp, /* write_resp */ |
1930 | | Curl_http_write_resp_hd, /* write_resp_hd */ |
1931 | | ZERO_NULL, /* connection_check */ |
1932 | | ZERO_NULL, /* attach connection */ |
1933 | | Curl_http_follow, /* follow */ |
1934 | | PORT_HTTPS, /* defport */ |
1935 | | CURLPROTO_WSS, /* protocol */ |
1936 | | CURLPROTO_HTTP, /* family */ |
1937 | | PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */ |
1938 | | PROTOPT_USERPWDCTRL |
1939 | | }; |
1940 | | #endif |
1941 | | |
1942 | | |
1943 | | #else |
1944 | | |
1945 | | CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen, |
1946 | | size_t *nread, |
1947 | | const struct curl_ws_frame **metap) |
1948 | | { |
1949 | | (void)curl; |
1950 | | (void)buffer; |
1951 | | (void)buflen; |
1952 | | (void)nread; |
1953 | | (void)metap; |
1954 | | return CURLE_NOT_BUILT_IN; |
1955 | | } |
1956 | | |
1957 | | CURLcode curl_ws_send(CURL *curl, const void *buffer, |
1958 | | size_t buflen, size_t *sent, |
1959 | | curl_off_t fragsize, |
1960 | | unsigned int flags) |
1961 | | { |
1962 | | (void)curl; |
1963 | | (void)buffer; |
1964 | | (void)buflen; |
1965 | | (void)sent; |
1966 | | (void)fragsize; |
1967 | | (void)flags; |
1968 | | return CURLE_NOT_BUILT_IN; |
1969 | | } |
1970 | | |
1971 | | const struct curl_ws_frame *curl_ws_meta(CURL *data) |
1972 | | { |
1973 | | (void)data; |
1974 | | return NULL; |
1975 | | } |
1976 | | |
1977 | | CURL_EXTERN CURLcode curl_ws_start_frame(CURL *curl, |
1978 | | unsigned int flags, |
1979 | | curl_off_t frame_len) |
1980 | | { |
1981 | | (void)curl; |
1982 | | (void)flags; |
1983 | | (void)frame_len; |
1984 | | return CURLE_NOT_BUILT_IN; |
1985 | | } |
1986 | | |
1987 | | #endif /* !CURL_DISABLE_WEBSOCKETS */ |