Coverage Report

Created: 2026-12-01 06:28

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