xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 | |
| 25 | #include "test.h" |
| 26 | |
| 27 | #ifdef USE_WEBSOCKETS |
| 28 | |
| 29 | #if 0 |
| 30 | |
| 31 | static int ping(CURL *curl, const char *send_payload) |
| 32 | { |
| 33 | size_t sent; |
| 34 | CURLcode result = |
| 35 | curl_ws_send(curl, send_payload, strlen(send_payload), &sent, CURLWS_PING); |
| 36 | fprintf(stderr, |
| 37 | "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent); |
| 38 | |
| 39 | return (int)result; |
| 40 | } |
| 41 | |
| 42 | static int recv_pong(CURL *curl, const char *exected_payload) |
| 43 | { |
| 44 | size_t rlen; |
| 45 | unsigned int rflags; |
| 46 | char buffer[256]; |
| 47 | CURLcode result = |
| 48 | curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &rflags); |
| 49 | if(rflags & CURLWS_PONG) { |
| 50 | int same = 0; |
| 51 | fprintf(stderr, "ws: got PONG back\n"); |
| 52 | if(rlen == strlen(exected_payload)) { |
| 53 | if(!memcmp(exected_payload, buffer, rlen)) { |
| 54 | fprintf(stderr, "ws: got the same payload back\n"); |
| 55 | same = 1; |
| 56 | } |
| 57 | } |
| 58 | if(!same) |
| 59 | fprintf(stderr, "ws: did NOT get the same payload back\n"); |
| 60 | } |
| 61 | else { |
| 62 | fprintf(stderr, "recv_pong: got %u bytes rflags %x\n", (int)rlen, rflags); |
| 63 | } |
| 64 | fprintf(stderr, "ws: curl_ws_recv returned %u, received %u\n", (int)result, |
| 65 | rlen); |
| 66 | return (int)result; |
| 67 | } |
| 68 | |
| 69 | /* just close the connection */ |
| 70 | static void websocket_close(CURL *curl) |
| 71 | { |
| 72 | size_t sent; |
| 73 | CURLcode result = |
| 74 | curl_ws_send(curl, "", 0, &sent, CURLWS_CLOSE); |
| 75 | fprintf(stderr, |
| 76 | "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent); |
| 77 | } |
| 78 | |
| 79 | static void websocket(CURL *curl) |
| 80 | { |
| 81 | int i = 0; |
| 82 | fprintf(stderr, "ws: websocket() starts\n"); |
| 83 | do { |
| 84 | if(ping(curl, "foobar")) |
| 85 | return; |
| 86 | if(recv_pong(curl, "foobar")) |
| 87 | return; |
| 88 | sleep(2); |
| 89 | } while(i++ < 10); |
| 90 | websocket_close(curl); |
| 91 | } |
| 92 | |
| 93 | #endif |
| 94 | |
| 95 | static size_t writecb(char *buffer, size_t size, size_t nitems, void *p) |
| 96 | { |
| 97 | CURL *easy = p; |
| 98 | size_t i; |
| 99 | size_t incoming = nitems; |
| 100 | struct curl_ws_frame *meta; |
| 101 | (void)size; |
| 102 | for(i = 0; i < nitems; i++) |
| 103 | printf("%02x ", (unsigned char)buffer[i]); |
| 104 | printf("\n"); |
| 105 | |
| 106 | meta = curl_ws_meta(easy); |
| 107 | if(meta) |
| 108 | printf("RECFLAGS: %x\n", meta->flags); |
| 109 | else |
| 110 | fprintf(stderr, "RECFLAGS: NULL\n"); |
| 111 | |
| 112 | /* this assumes we get a simple TEXT frame first */ |
| 113 | { |
| 114 | CURLcode result = CURLE_OK; |
| 115 | fprintf(stderr, "send back a TEXT\n"); |
| 116 | (void)easy; |
| 117 | /*result = curl_ws_send(easy, pong, 2, &sent, 0);*/ |
| 118 | if(result) |
| 119 | nitems = 0; |
| 120 | } |
| 121 | if(nitems != incoming) |
| 122 | fprintf(stderr, "returns error from callback\n"); |
| 123 | return nitems; |
| 124 | } |
| 125 | |
| 126 | int test(char *URL) |
| 127 | { |
| 128 | CURL *curl; |
| 129 | CURLcode res = CURLE_OK; |
| 130 | |
| 131 | global_init(CURL_GLOBAL_ALL); |
| 132 | |
| 133 | curl = curl_easy_init(); |
| 134 | if(curl) { |
| 135 | curl_easy_setopt(curl, CURLOPT_URL, URL); |
| 136 | |
| 137 | /* use the callback style */ |
| 138 | curl_easy_setopt(curl, CURLOPT_USERAGENT, "webbie-sox/3"); |
| 139 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| 140 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb); |
| 141 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl); |
| 142 | res = curl_easy_perform(curl); |
| 143 | fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res); |
| 144 | #if 0 |
| 145 | if(res == CURLE_OK) |
| 146 | websocket(curl); |
| 147 | #endif |
| 148 | /* always cleanup */ |
| 149 | curl_easy_cleanup(curl); |
| 150 | } |
| 151 | curl_global_cleanup(); |
| 152 | return (int)res; |
| 153 | } |
| 154 | |
| 155 | #else |
| 156 | NO_SUPPORT_BUILT_IN |
| 157 | #endif |