lib2301.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifdef USE_WEBSOCKETS
  26. #if 0
  27. static int ping(CURL *curl, const char *send_payload)
  28. {
  29. size_t sent;
  30. CURLcode result =
  31. curl_ws_send(curl, send_payload, strlen(send_payload), &sent, CURLWS_PING);
  32. fprintf(stderr,
  33. "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
  34. return (int)result;
  35. }
  36. static int recv_pong(CURL *curl, const char *expected_payload)
  37. {
  38. size_t rlen;
  39. unsigned int rflags;
  40. char buffer[256];
  41. CURLcode result =
  42. curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &rflags);
  43. if(rflags & 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 %u bytes rflags %x\n", (int)rlen, rflags);
  57. }
  58. fprintf(stderr, "ws: curl_ws_recv returned %u, received %u\n", (int)result,
  59. rlen);
  60. return (int)result;
  61. }
  62. /* just close the connection */
  63. static void websocket_close(CURL *curl)
  64. {
  65. size_t sent;
  66. CURLcode result =
  67. curl_ws_send(curl, "", 0, &sent, CURLWS_CLOSE);
  68. fprintf(stderr,
  69. "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
  70. }
  71. static void websocket(CURL *curl)
  72. {
  73. int i = 0;
  74. fprintf(stderr, "ws: websocket() starts\n");
  75. do {
  76. if(ping(curl, "foobar"))
  77. return;
  78. if(recv_pong(curl, "foobar"))
  79. return;
  80. sleep(2);
  81. } while(i++ < 10);
  82. websocket_close(curl);
  83. }
  84. #endif
  85. static size_t writecb(char *b, size_t size, size_t nitems, void *p)
  86. {
  87. CURL *easy = p;
  88. unsigned char *buffer = (unsigned char *)b;
  89. size_t i;
  90. size_t sent;
  91. unsigned char pong[] = {
  92. 0x8a, 0x0
  93. };
  94. size_t incoming = nitems;
  95. fprintf(stderr, "Called CURLOPT_WRITEFUNCTION with %u bytes: ",
  96. (int)nitems);
  97. for(i = 0; i < nitems; i++)
  98. fprintf(stderr, "%02x ", (unsigned char)buffer[i]);
  99. fprintf(stderr, "\n");
  100. (void)size;
  101. if(buffer[0] == 0x89) {
  102. CURLcode result;
  103. fprintf(stderr, "send back a simple PONG\n");
  104. result = curl_ws_send(easy, pong, 2, &sent, 0, 0);
  105. if(result)
  106. nitems = 0;
  107. }
  108. if(nitems != incoming)
  109. fprintf(stderr, "returns error from callback\n");
  110. return nitems;
  111. }
  112. int test(char *URL)
  113. {
  114. CURL *curl;
  115. CURLcode res = CURLE_OK;
  116. global_init(CURL_GLOBAL_ALL);
  117. curl = curl_easy_init();
  118. if(curl) {
  119. curl_easy_setopt(curl, CURLOPT_URL, URL);
  120. /* use the callback style */
  121. curl_easy_setopt(curl, CURLOPT_USERAGENT, "webbie-sox/3");
  122. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  123. curl_easy_setopt(curl, CURLOPT_WS_OPTIONS, CURLWS_RAW_MODE);
  124. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  125. curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
  126. res = curl_easy_perform(curl);
  127. fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
  128. #if 0
  129. if(res == CURLE_OK)
  130. websocket(curl);
  131. #endif
  132. /* always cleanup */
  133. curl_easy_cleanup(curl);
  134. }
  135. curl_global_cleanup();
  136. return (int)res;
  137. }
  138. #else /* no websockets */
  139. NO_SUPPORT_BUILT_IN
  140. #endif