ws.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifdef USE_WEBSOCKETS
  28. #ifdef USE_HYPER
  29. #define REQTYPE void
  30. #else
  31. #define REQTYPE struct dynbuf
  32. #endif
  33. /* a client-side WS frame decoder, parsing frame headers and
  34. * payload, keeping track of current position and stats */
  35. enum ws_dec_state {
  36. WS_DEC_INIT,
  37. WS_DEC_HEAD,
  38. WS_DEC_PAYLOAD
  39. };
  40. struct ws_decoder {
  41. int frame_age; /* zero */
  42. int frame_flags; /* See the CURLWS_* defines */
  43. curl_off_t payload_offset; /* the offset parsing is at */
  44. curl_off_t payload_len;
  45. unsigned char head[10];
  46. int head_len, head_total;
  47. enum ws_dec_state state;
  48. };
  49. /* a client-side WS frame encoder, generating frame headers and
  50. * converting payloads, tracking remaining data in current frame */
  51. struct ws_encoder {
  52. curl_off_t payload_len; /* payload length of current frame */
  53. curl_off_t payload_remain; /* remaining payload of current */
  54. unsigned int xori; /* xor index */
  55. unsigned char mask[4]; /* 32 bit mask for this connection */
  56. unsigned char firstbyte; /* first byte of frame we encode */
  57. bool contfragment; /* set TRUE if the previous fragment sent was not final */
  58. };
  59. /* A websocket connection with en- and decoder that treat frames
  60. * and keep track of boundaries. */
  61. struct websocket {
  62. struct Curl_easy *data; /* used for write callback handling */
  63. struct ws_decoder dec; /* decode of we frames */
  64. struct ws_encoder enc; /* decode of we frames */
  65. struct bufq recvbuf; /* raw data from the server */
  66. struct bufq sendbuf; /* raw data to be sent to the server */
  67. struct curl_ws_frame frame; /* the current WS FRAME received */
  68. };
  69. CURLcode Curl_ws_request(struct Curl_easy *data, REQTYPE *req);
  70. CURLcode Curl_ws_accept(struct Curl_easy *data, const char *mem, size_t len);
  71. size_t Curl_ws_writecb(char *buffer, size_t size, size_t nitems, void *userp);
  72. void Curl_ws_done(struct Curl_easy *data);
  73. CURLcode Curl_ws_disconnect(struct Curl_easy *data,
  74. struct connectdata *conn,
  75. bool dead_connection);
  76. #else
  77. #define Curl_ws_request(x,y) CURLE_OK
  78. #define Curl_ws_done(x) Curl_nop_stmt
  79. #define Curl_ws_free(x) Curl_nop_stmt
  80. #endif
  81. #endif /* HEADER_CURL_WS_H */