Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/clients/ws-pingpong.c
2066 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
/* <DESC>
25
* WebSockets pingpong
26
* </DESC>
27
*/
28
/* curl stuff */
29
#include "curl_setup.h"
30
#include <curl/curl.h>
31
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#ifdef _WIN32
37
#include <windows.h>
38
#else
39
#include <sys/time.h>
40
#endif
41
42
#ifndef CURL_DISABLE_WEBSOCKETS
43
44
static CURLcode ping(CURL *curl, const char *send_payload)
45
{
46
size_t sent;
47
CURLcode result =
48
curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
49
CURLWS_PING);
50
fprintf(stderr,
51
"ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
52
53
return result;
54
}
55
56
static CURLcode recv_pong(CURL *curl, const char *expected_payload)
57
{
58
size_t rlen;
59
const struct curl_ws_frame *meta;
60
char buffer[256];
61
CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
62
if(result) {
63
fprintf(stderr, "ws: curl_ws_recv returned %u, received %ld\n",
64
(int)result, (long)rlen);
65
return result;
66
}
67
68
if(!(meta->flags & CURLWS_PONG)) {
69
fprintf(stderr, "recv_pong: wrong frame, got %d bytes rflags %x\n",
70
(int)rlen, meta->flags);
71
return CURLE_RECV_ERROR;
72
}
73
74
fprintf(stderr, "ws: got PONG back\n");
75
if(rlen == strlen(expected_payload) &&
76
!memcmp(expected_payload, buffer, rlen)) {
77
fprintf(stderr, "ws: got the same payload back\n");
78
return CURLE_OK;
79
}
80
fprintf(stderr, "ws: did NOT get the same payload back\n");
81
return CURLE_RECV_ERROR;
82
}
83
84
/* just close the connection */
85
static void websocket_close(CURL *curl)
86
{
87
size_t sent;
88
CURLcode result =
89
curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
90
fprintf(stderr,
91
"ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
92
}
93
94
#if defined(__TANDEM)
95
# include <cextdecs.h(PROCESS_DELAY_)>
96
#endif
97
static CURLcode pingpong(CURL *curl, const char *payload)
98
{
99
CURLcode res;
100
int i;
101
102
res = ping(curl, payload);
103
if(res)
104
return res;
105
for(i = 0; i < 10; ++i) {
106
fprintf(stderr, "Receive pong\n");
107
res = recv_pong(curl, payload);
108
if(res == CURLE_AGAIN) {
109
#ifdef _WIN32
110
Sleep(100);
111
#elif defined(__TANDEM)
112
/* NonStop only defines usleep when building for a threading model */
113
# if defined(_PUT_MODEL_) || defined(_KLT_MODEL_)
114
usleep(100*1000);
115
# else
116
PROCESS_DELAY_(100*1000);
117
# endif
118
#else
119
usleep(100*1000);
120
#endif
121
continue;
122
}
123
websocket_close(curl);
124
return res;
125
}
126
websocket_close(curl);
127
return CURLE_RECV_ERROR;
128
}
129
130
#endif
131
132
int main(int argc, char *argv[])
133
{
134
#ifndef CURL_DISABLE_WEBSOCKETS
135
CURL *curl;
136
CURLcode res = CURLE_OK;
137
const char *url, *payload;
138
139
if(argc != 3) {
140
fprintf(stderr, "usage: ws-pingpong url payload\n");
141
return 2;
142
}
143
url = argv[1];
144
payload = argv[2];
145
146
curl_global_init(CURL_GLOBAL_ALL);
147
148
curl = curl_easy_init();
149
if(curl) {
150
curl_easy_setopt(curl, CURLOPT_URL, url);
151
152
/* use the callback style */
153
curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong");
154
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
155
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
156
res = curl_easy_perform(curl);
157
fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
158
if(res == CURLE_OK)
159
res = pingpong(curl, payload);
160
161
/* always cleanup */
162
curl_easy_cleanup(curl);
163
}
164
curl_global_cleanup();
165
return (int)res;
166
167
#else /* !CURL_DISABLE_WEBSOCKETS */
168
(void)argc;
169
(void)argv;
170
fprintf(stderr, "WebSockets not enabled in libcurl\n");
171
return 1;
172
#endif /* CURL_DISABLE_WEBSOCKETS */
173
}
174
175