2
0

lib2305.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 %d, sent %d\n", 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", result);
  54. return;
  55. }
  56. printf("%d: nread %zu Age %d 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. CURLcode test(char *URL)
  68. {
  69. CURL *curl;
  70. CURLcode res = CURLE_OK;
  71. global_init(CURL_GLOBAL_ALL);
  72. curl = curl_easy_init();
  73. if(curl) {
  74. curl_easy_setopt(curl, CURLOPT_URL, URL);
  75. /* use the callback style */
  76. curl_easy_setopt(curl, CURLOPT_USERAGENT, "websocket/2304");
  77. libtest_debug_config.nohex = 1;
  78. libtest_debug_config.tracetime = 1;
  79. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
  80. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  81. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  82. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  83. res = curl_easy_perform(curl);
  84. fprintf(stderr, "curl_easy_perform() returned %d\n", res);
  85. if(res == CURLE_OK)
  86. websocket(curl);
  87. /* always cleanup */
  88. curl_easy_cleanup(curl);
  89. }
  90. curl_global_cleanup();
  91. return res;
  92. }
  93. #else
  94. NO_SUPPORT_BUILT_IN
  95. #endif