2
0

ws.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef HEADER_CURL_WS_H
  2. #define HEADER_CURL_WS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  28. /* a client-side WS frame decoder, parsing frame headers and
  29. * payload, keeping track of current position and stats */
  30. enum ws_dec_state {
  31. WS_DEC_INIT,
  32. WS_DEC_HEAD,
  33. WS_DEC_PAYLOAD
  34. };
  35. struct ws_decoder {
  36. int frame_age; /* zero */
  37. int frame_flags; /* See the CURLWS_* defines */
  38. curl_off_t payload_offset; /* the offset parsing is at */
  39. curl_off_t payload_len;
  40. unsigned char head[10];
  41. int head_len, head_total;
  42. enum ws_dec_state state;
  43. };
  44. /* a client-side WS frame encoder, generating frame headers and
  45. * converting payloads, tracking remaining data in current frame */
  46. struct ws_encoder {
  47. curl_off_t payload_len; /* payload length of current frame */
  48. curl_off_t payload_remain; /* remaining payload of current */
  49. unsigned int xori; /* xor index */
  50. unsigned char mask[4]; /* 32-bit mask for this connection */
  51. unsigned char firstbyte; /* first byte of frame we encode */
  52. bool contfragment; /* set TRUE if the previous fragment sent was not final */
  53. };
  54. /* A websocket connection with en- and decoder that treat frames
  55. * and keep track of boundaries. */
  56. struct websocket {
  57. struct Curl_easy *data; /* used for write callback handling */
  58. struct ws_decoder dec; /* decode of we frames */
  59. struct ws_encoder enc; /* decode of we frames */
  60. struct bufq recvbuf; /* raw data from the server */
  61. struct bufq sendbuf; /* raw data to be sent to the server */
  62. struct curl_ws_frame frame; /* the current WS FRAME received */
  63. };
  64. CURLcode Curl_ws_request(struct Curl_easy *data, struct dynbuf *req);
  65. CURLcode Curl_ws_accept(struct Curl_easy *data, const char *mem, size_t len);
  66. extern const struct Curl_handler Curl_handler_ws;
  67. #ifdef USE_SSL
  68. extern const struct Curl_handler Curl_handler_wss;
  69. #endif
  70. #else
  71. #define Curl_ws_request(x,y) CURLE_OK
  72. #define Curl_ws_free(x) Curl_nop_stmt
  73. #endif
  74. #endif /* HEADER_CURL_WS_H */