Coverage Report

Created: 2025-12-03 07:02

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