Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/cli_ws_pingpong.c
2649 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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 "first.h"
25
26
#include "testtrace.h"
27
#include "memdebug.h"
28
29
#ifndef CURL_DISABLE_WEBSOCKETS
30
31
static CURLcode pingpong(CURL *curl, const char *payload)
32
{
33
CURLcode res;
34
int i;
35
36
res = ws_send_ping(curl, payload);
37
if(res)
38
return res;
39
for(i = 0; i < 10; ++i) {
40
curl_mfprintf(stderr, "Receive pong\n");
41
res = ws_recv_pong(curl, payload);
42
if(res == CURLE_AGAIN) {
43
curlx_wait_ms(100);
44
continue;
45
}
46
ws_close(curl);
47
return res;
48
}
49
ws_close(curl);
50
return CURLE_RECV_ERROR;
51
}
52
53
#endif
54
55
static CURLcode test_cli_ws_pingpong(const char *URL)
56
{
57
#ifndef CURL_DISABLE_WEBSOCKETS
58
CURL *curl;
59
CURLcode result = CURLE_OK;
60
const char *payload;
61
62
if(!URL || !libtest_arg2) {
63
curl_mfprintf(stderr, "need args: URL payload\n");
64
return (CURLcode)2;
65
}
66
payload = libtest_arg2;
67
68
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
69
curl_mfprintf(stderr, "curl_global_init() failed\n");
70
return (CURLcode)3;
71
}
72
73
curl = curl_easy_init();
74
if(curl) {
75
curl_easy_setopt(curl, CURLOPT_URL, URL);
76
77
/* use the callback style */
78
curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong");
79
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
80
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
81
result = curl_easy_perform(curl);
82
curl_mfprintf(stderr, "curl_easy_perform() returned %u\n", result);
83
if(result == CURLE_OK)
84
result = pingpong(curl, payload);
85
86
/* always cleanup */
87
curl_easy_cleanup(curl);
88
}
89
curl_global_cleanup();
90
return result;
91
92
#else /* !CURL_DISABLE_WEBSOCKETS */
93
(void)URL;
94
curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n");
95
return (CURLcode)1;
96
#endif /* CURL_DISABLE_WEBSOCKETS */
97
}
98
99