2
0

lib2302.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 *exected_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(exected_payload)) {
  47. if(!memcmp(exected_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 *buffer, size_t size, size_t nitems, void *p)
  86. {
  87. CURL *easy = p;
  88. size_t i;
  89. size_t incoming = nitems;
  90. const struct curl_ws_frame *meta;
  91. (void)size;
  92. for(i = 0; i < nitems; i++)
  93. printf("%02x ", (unsigned char)buffer[i]);
  94. printf("\n");
  95. meta = curl_ws_meta(easy);
  96. if(meta)
  97. printf("RECFLAGS: %x\n", meta->flags);
  98. else
  99. fprintf(stderr, "RECFLAGS: NULL\n");
  100. /* this assumes we get a simple TEXT frame first */
  101. {
  102. CURLcode result = CURLE_OK;
  103. fprintf(stderr, "send back a TEXT\n");
  104. (void)easy;
  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_WRITEFUNCTION, writecb);
  124. curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
  125. res = curl_easy_perform(curl);
  126. fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
  127. #if 0
  128. if(res == CURLE_OK)
  129. websocket(curl);
  130. #endif
  131. /* always cleanup */
  132. curl_easy_cleanup(curl);
  133. }
  134. curl_global_cleanup();
  135. return (int)res;
  136. }
  137. #else
  138. NO_SUPPORT_BUILT_IN
  139. #endif