lib2304.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "test.h"
  25. #ifndef CURL_DISABLE_WEBSOCKETS
  26. static CURLcode send_ping(CURL *curl, const char *send_payload)
  27. {
  28. size_t sent;
  29. CURLcode result =
  30. curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
  31. CURLWS_PING);
  32. fprintf(stderr,
  33. "ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
  34. return result;
  35. }
  36. static CURLcode recv_pong(CURL *curl, const char *expected_payload)
  37. {
  38. size_t rlen;
  39. const struct curl_ws_frame *meta;
  40. char buffer[256];
  41. CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
  42. if(!result) {
  43. if(meta->flags & CURLWS_PONG) {
  44. int same = 0;
  45. fprintf(stderr, "ws: got PONG back\n");
  46. if(rlen == strlen(expected_payload)) {
  47. if(!memcmp(expected_payload, buffer, rlen)) {
  48. fprintf(stderr, "ws: got the same payload back\n");
  49. same = 1;
  50. }
  51. }
  52. if(!same)
  53. fprintf(stderr, "ws: did NOT get the same payload back\n");
  54. }
  55. else {
  56. fprintf(stderr, "recv_pong: got %d bytes rflags %x\n", (int)rlen,
  57. meta->flags);
  58. }
  59. }
  60. fprintf(stderr, "ws: curl_ws_recv returned %d, received %d\n", result,
  61. (int)rlen);
  62. return result;
  63. }
  64. static CURLcode recv_any(CURL *curl)
  65. {
  66. size_t rlen;
  67. const struct curl_ws_frame *meta;
  68. char buffer[256];
  69. CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
  70. if(result)
  71. return result;
  72. fprintf(stderr, "recv_any: got %u bytes rflags %x\n", (int)rlen,
  73. meta->flags);
  74. return CURLE_OK;
  75. }
  76. /* just close the connection */
  77. static void websocket_close(CURL *curl)
  78. {
  79. size_t sent;
  80. CURLcode result =
  81. curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
  82. fprintf(stderr,
  83. "ws: curl_ws_send returned %d, sent %u\n", result, (int)sent);
  84. }
  85. static void websocket(CURL *curl)
  86. {
  87. int i = 0;
  88. fprintf(stderr, "ws: websocket() starts\n");
  89. do {
  90. recv_any(curl);
  91. fprintf(stderr, "Send ping\n");
  92. if(send_ping(curl, "foobar"))
  93. return;
  94. fprintf(stderr, "Receive pong\n");
  95. if(recv_pong(curl, "foobar")) {
  96. printf("Connection closed\n");
  97. return;
  98. }
  99. sleep(2);
  100. } while(i++ < 10);
  101. websocket_close(curl);
  102. }
  103. CURLcode test(char *URL)
  104. {
  105. CURL *curl;
  106. CURLcode res = CURLE_OK;
  107. global_init(CURL_GLOBAL_ALL);
  108. curl = curl_easy_init();
  109. if(curl) {
  110. curl_easy_setopt(curl, CURLOPT_URL, URL);
  111. /* use the callback style */
  112. curl_easy_setopt(curl, CURLOPT_USERAGENT, "websocket/2304");
  113. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  114. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  115. res = curl_easy_perform(curl);
  116. fprintf(stderr, "curl_easy_perform() returned %d\n", res);
  117. if(res == CURLE_OK)
  118. websocket(curl);
  119. /* always cleanup */
  120. curl_easy_cleanup(curl);
  121. }
  122. curl_global_cleanup();
  123. return res;
  124. }
  125. #else
  126. NO_SUPPORT_BUILT_IN
  127. #endif