113498266Sopenharmony_ci#ifndef HEADER_CURL_WS_H
213498266Sopenharmony_ci#define HEADER_CURL_WS_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci#include "curl_setup.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#ifdef USE_HYPER
3113498266Sopenharmony_ci#define REQTYPE void
3213498266Sopenharmony_ci#else
3313498266Sopenharmony_ci#define REQTYPE struct dynbuf
3413498266Sopenharmony_ci#endif
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci/* a client-side WS frame decoder, parsing frame headers and
3713498266Sopenharmony_ci * payload, keeping track of current position and stats */
3813498266Sopenharmony_cienum ws_dec_state {
3913498266Sopenharmony_ci  WS_DEC_INIT,
4013498266Sopenharmony_ci  WS_DEC_HEAD,
4113498266Sopenharmony_ci  WS_DEC_PAYLOAD
4213498266Sopenharmony_ci};
4313498266Sopenharmony_ci
4413498266Sopenharmony_cistruct ws_decoder {
4513498266Sopenharmony_ci  int frame_age;        /* zero */
4613498266Sopenharmony_ci  int frame_flags;      /* See the CURLWS_* defines */
4713498266Sopenharmony_ci  curl_off_t payload_offset;   /* the offset parsing is at */
4813498266Sopenharmony_ci  curl_off_t payload_len;
4913498266Sopenharmony_ci  unsigned char head[10];
5013498266Sopenharmony_ci  int head_len, head_total;
5113498266Sopenharmony_ci  enum ws_dec_state state;
5213498266Sopenharmony_ci};
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci/* a client-side WS frame encoder, generating frame headers and
5513498266Sopenharmony_ci * converting payloads, tracking remaining data in current frame */
5613498266Sopenharmony_cistruct ws_encoder {
5713498266Sopenharmony_ci  curl_off_t payload_len;  /* payload length of current frame */
5813498266Sopenharmony_ci  curl_off_t payload_remain;  /* remaining payload of current */
5913498266Sopenharmony_ci  unsigned int xori; /* xor index */
6013498266Sopenharmony_ci  unsigned char mask[4]; /* 32 bit mask for this connection */
6113498266Sopenharmony_ci  unsigned char firstbyte; /* first byte of frame we encode */
6213498266Sopenharmony_ci  bool contfragment; /* set TRUE if the previous fragment sent was not final */
6313498266Sopenharmony_ci};
6413498266Sopenharmony_ci
6513498266Sopenharmony_ci/* A websocket connection with en- and decoder that treat frames
6613498266Sopenharmony_ci * and keep track of boundaries. */
6713498266Sopenharmony_cistruct websocket {
6813498266Sopenharmony_ci  struct Curl_easy *data; /* used for write callback handling */
6913498266Sopenharmony_ci  struct ws_decoder dec;  /* decode of we frames */
7013498266Sopenharmony_ci  struct ws_encoder enc;  /* decode of we frames */
7113498266Sopenharmony_ci  struct bufq recvbuf;    /* raw data from the server */
7213498266Sopenharmony_ci  struct bufq sendbuf;    /* raw data to be sent to the server */
7313498266Sopenharmony_ci  struct curl_ws_frame frame;  /* the current WS FRAME received */
7413498266Sopenharmony_ci};
7513498266Sopenharmony_ci
7613498266Sopenharmony_ciCURLcode Curl_ws_request(struct Curl_easy *data, REQTYPE *req);
7713498266Sopenharmony_ciCURLcode Curl_ws_accept(struct Curl_easy *data, const char *mem, size_t len);
7813498266Sopenharmony_civoid Curl_ws_done(struct Curl_easy *data);
7913498266Sopenharmony_ci
8013498266Sopenharmony_ciextern const struct Curl_handler Curl_handler_ws;
8113498266Sopenharmony_ci#ifdef USE_SSL
8213498266Sopenharmony_ciextern const struct Curl_handler Curl_handler_wss;
8313498266Sopenharmony_ci#endif
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci#else
8713498266Sopenharmony_ci#define Curl_ws_request(x,y) CURLE_OK
8813498266Sopenharmony_ci#define Curl_ws_done(x) Curl_nop_stmt
8913498266Sopenharmony_ci#define Curl_ws_free(x) Curl_nop_stmt
9013498266Sopenharmony_ci#endif
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci#endif /* HEADER_CURL_WS_H */
93