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