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