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