2
0

lib2305.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "testtrace.h"
  26. #ifdef USE_WEBSOCKETS
  27. /* just close the connection */
  28. static void websocket_close(CURL *curl)
  29. {
  30. size_t sent;
  31. CURLcode result =
  32. curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
  33. fprintf(stderr,
  34. "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
  35. }
  36. static void websocket(CURL *curl)
  37. {
  38. char buffer[256];
  39. const struct curl_ws_frame *meta;
  40. size_t nread;
  41. size_t i = 0;
  42. FILE *save = fopen(libtest_arg2, FOPEN_WRITETEXT);
  43. if(!save)
  44. return;
  45. /* Three 4097-bytes frames are expected, 12291 bytes */
  46. while(i < 12291) {
  47. CURLcode result =
  48. curl_ws_recv(curl, buffer, sizeof(buffer), &nread, &meta);
  49. if(result) {
  50. if(result == CURLE_AGAIN)
  51. /* crude busy-loop */
  52. continue;
  53. printf("curl_ws_recv returned %d\n", (int)result);
  54. return;
  55. }
  56. printf("%u: nread %u Age %u Flags %x "
  57. "Offset %" CURL_FORMAT_CURL_OFF_T " "
  58. "Bytesleft %" CURL_FORMAT_CURL_OFF_T "\n",
  59. (int)i,
  60. nread, meta->age, meta->flags, meta->offset, meta->bytesleft);
  61. i += meta->len;
  62. fwrite(buffer, 1, nread, save);
  63. }
  64. fclose(save);
  65. websocket_close(curl);
  66. }
  67. extern struct libtest_trace_cfg libtest_debug_config;
  68. int test(char *URL)
  69. {
  70. CURL *curl;
  71. CURLcode res = CURLE_OK;
  72. global_init(CURL_GLOBAL_ALL);
  73. curl = curl_easy_init();
  74. if(curl) {
  75. curl_easy_setopt(curl, CURLOPT_URL, URL);
  76. /* use the callback style */
  77. curl_easy_setopt(curl, CURLOPT_USERAGENT, "websocket/2304");
  78. libtest_debug_config.nohex = 1;
  79. libtest_debug_config.tracetime = 1;
  80. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
  81. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  82. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  83. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  84. res = curl_easy_perform(curl);
  85. fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
  86. if(res == CURLE_OK)
  87. websocket(curl);
  88. /* always cleanup */
  89. curl_easy_cleanup(curl);
  90. }
  91. curl_global_cleanup();
  92. return (int)res;
  93. }
  94. #else
  95. NO_SUPPORT_BUILT_IN
  96. #endif