Coverage Report

Created: 2025-08-29 07:11

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