xref: /curl/tests/libtest/lib2305.c (revision 25cbc2f7)
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 #include "testtrace.h"
27 
28 #ifdef USE_WEBSOCKETS
29 
30 /* just close the connection */
websocket_close(CURL * curl)31 static void websocket_close(CURL *curl)
32 {
33   size_t sent;
34   CURLcode result =
35     curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
36   fprintf(stderr,
37           "ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
38 }
39 
websocket(CURL * curl)40 static void websocket(CURL *curl)
41 {
42   char buffer[256];
43   const struct curl_ws_frame *meta;
44   size_t nread;
45   size_t i = 0;
46   FILE *save = fopen(libtest_arg2, FOPEN_WRITETEXT);
47   if(!save)
48     return;
49 
50   /* Three 4097-bytes frames are expected, 12291 bytes */
51   while(i < 12291) {
52     CURLcode result =
53       curl_ws_recv(curl, buffer, sizeof(buffer), &nread, &meta);
54     if(result) {
55       if(result == CURLE_AGAIN)
56         /* crude busy-loop */
57         continue;
58       printf("curl_ws_recv returned %d\n", result);
59       return;
60     }
61     printf("%d: nread %zu Age %d Flags %x "
62            "Offset %" CURL_FORMAT_CURL_OFF_T " "
63            "Bytesleft %" CURL_FORMAT_CURL_OFF_T "\n",
64            (int)i,
65            nread, meta->age, meta->flags, meta->offset, meta->bytesleft);
66     i += meta->len;
67     fwrite(buffer, 1, nread, save);
68   }
69   fclose(save);
70 
71   websocket_close(curl);
72 }
73 
test(char * URL)74 CURLcode test(char *URL)
75 {
76   CURL *curl;
77   CURLcode res = CURLE_OK;
78 
79   global_init(CURL_GLOBAL_ALL);
80 
81   curl = curl_easy_init();
82   if(curl) {
83     curl_easy_setopt(curl, CURLOPT_URL, URL);
84 
85     /* use the callback style */
86     curl_easy_setopt(curl, CURLOPT_USERAGENT, "websocket/2304");
87     libtest_debug_config.nohex = 1;
88     libtest_debug_config.tracetime = 1;
89     curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
90     curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
91     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
92     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
93     res = curl_easy_perform(curl);
94     fprintf(stderr, "curl_easy_perform() returned %d\n", res);
95     if(res == CURLE_OK)
96       websocket(curl);
97 
98     /* always cleanup */
99     curl_easy_cleanup(curl);
100   }
101   curl_global_cleanup();
102   return res;
103 }
104 
105 #else
106 NO_SUPPORT_BUILT_IN
107 #endif
108