Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/http/clients/tls-session-reuse.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
* TLS session reuse
26
* </DESC>
27
*/
28
#include <curl/curl.h>
29
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
35
{
36
/*
37
* This is the trace look that is similar to what libcurl makes on its
38
* own.
39
*/
40
static const char * const s_infotype[] = {
41
"* ", "< ", "> ", "{ ", "} ", "{ ", "} "
42
};
43
if(idsbuf && *idsbuf)
44
fprintf(log, "%s%s", idsbuf, s_infotype[type]);
45
else
46
fputs(s_infotype[type], log);
47
}
48
49
#define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
50
#define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
51
CURL_FORMAT_CURL_OFF_T "] "
52
/*
53
** callback for CURLOPT_DEBUGFUNCTION
54
*/
55
static int debug_cb(CURL *handle, curl_infotype type,
56
char *data, size_t size,
57
void *userdata)
58
{
59
FILE *output = stderr;
60
static int newl = 0;
61
static int traced_data = 0;
62
char idsbuf[60];
63
curl_off_t xfer_id, conn_id;
64
65
(void)handle; /* not used */
66
(void)userdata;
67
68
if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
69
if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
70
conn_id >= 0) {
71
curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2, xfer_id,
72
conn_id);
73
}
74
else {
75
curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
76
}
77
}
78
else
79
idsbuf[0] = 0;
80
81
switch(type) {
82
case CURLINFO_HEADER_OUT:
83
if(size > 0) {
84
size_t st = 0;
85
size_t i;
86
for(i = 0; i < size - 1; i++) {
87
if(data[i] == '\n') { /* LF */
88
if(!newl) {
89
log_line_start(output, idsbuf, type);
90
}
91
(void)fwrite(data + st, i - st + 1, 1, output);
92
st = i + 1;
93
newl = 0;
94
}
95
}
96
if(!newl)
97
log_line_start(output, idsbuf, type);
98
(void)fwrite(data + st, i - st + 1, 1, output);
99
}
100
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
101
traced_data = 0;
102
break;
103
case CURLINFO_TEXT:
104
case CURLINFO_HEADER_IN:
105
if(!newl)
106
log_line_start(output, idsbuf, type);
107
(void)fwrite(data, size, 1, output);
108
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
109
traced_data = 0;
110
break;
111
case CURLINFO_DATA_OUT:
112
case CURLINFO_DATA_IN:
113
case CURLINFO_SSL_DATA_IN:
114
case CURLINFO_SSL_DATA_OUT:
115
if(!traced_data) {
116
if(!newl)
117
log_line_start(output, idsbuf, type);
118
fprintf(output, "[%ld bytes data]\n", (long)size);
119
newl = 0;
120
traced_data = 1;
121
}
122
break;
123
default: /* nada */
124
newl = 0;
125
traced_data = 1;
126
break;
127
}
128
129
return 0;
130
}
131
132
static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
133
{
134
(void)ptr;
135
(void)opaque;
136
return size * nmemb;
137
}
138
139
static int add_transfer(CURLM *multi, CURLSH *share,
140
struct curl_slist *resolve,
141
const char *url, long http_version)
142
{
143
CURL *easy;
144
CURLMcode mc;
145
146
easy = curl_easy_init();
147
if(!easy) {
148
fprintf(stderr, "curl_easy_init failed\n");
149
return 1;
150
}
151
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
152
curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, debug_cb);
153
curl_easy_setopt(easy, CURLOPT_URL, url);
154
curl_easy_setopt(easy, CURLOPT_SHARE, share);
155
curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
156
curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
157
curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
158
curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, http_version);
159
curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
160
curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
161
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
162
curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
163
if(resolve)
164
curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve);
165
166
167
mc = curl_multi_add_handle(multi, easy);
168
if(mc != CURLM_OK) {
169
fprintf(stderr, "curl_multi_add_handle: %s\n",
170
curl_multi_strerror(mc));
171
curl_easy_cleanup(easy);
172
return 1;
173
}
174
return 0;
175
}
176
177
int main(int argc, char *argv[])
178
{
179
const char *url;
180
CURLM *multi = NULL;
181
CURLMcode mc;
182
int running_handles = 0, numfds;
183
CURLMsg *msg;
184
CURLSH *share = NULL;
185
CURLU *cu;
186
struct curl_slist *resolve = NULL;
187
char resolve_buf[1024];
188
int msgs_in_queue;
189
int add_more, waits, ongoing = 0;
190
char *host = NULL, *port = NULL;
191
int http_version = CURL_HTTP_VERSION_1_1;
192
int exitcode = 1;
193
194
if(argc != 3) {
195
fprintf(stderr, "%s proto URL\n", argv[0]);
196
return 2;
197
}
198
199
if(!strcmp("h2", argv[1]))
200
http_version = CURL_HTTP_VERSION_2;
201
else if(!strcmp("h3", argv[1]))
202
http_version = CURL_HTTP_VERSION_3ONLY;
203
204
url = argv[2];
205
cu = curl_url();
206
if(!cu) {
207
fprintf(stderr, "out of memory\n");
208
return 1;
209
}
210
if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
211
fprintf(stderr, "not a URL: '%s'\n", url);
212
goto cleanup;
213
}
214
if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
215
fprintf(stderr, "could not get host of '%s'\n", url);
216
goto cleanup;
217
}
218
if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
219
fprintf(stderr, "could not get port of '%s'\n", url);
220
goto cleanup;
221
}
222
223
curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
224
host, port);
225
resolve = curl_slist_append(resolve, resolve_buf);
226
227
multi = curl_multi_init();
228
if(!multi) {
229
fprintf(stderr, "curl_multi_init failed\n");
230
goto cleanup;
231
}
232
233
share = curl_share_init();
234
if(!share) {
235
fprintf(stderr, "curl_share_init failed\n");
236
goto cleanup;
237
}
238
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
239
240
241
if(add_transfer(multi, share, resolve, url, http_version))
242
goto cleanup;
243
++ongoing;
244
add_more = 6;
245
waits = 3;
246
do {
247
mc = curl_multi_perform(multi, &running_handles);
248
if(mc != CURLM_OK) {
249
fprintf(stderr, "curl_multi_perform: %s\n",
250
curl_multi_strerror(mc));
251
goto cleanup;
252
}
253
254
if(running_handles) {
255
mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
256
if(mc != CURLM_OK) {
257
fprintf(stderr, "curl_multi_poll: %s\n",
258
curl_multi_strerror(mc));
259
goto cleanup;
260
}
261
}
262
263
if(waits) {
264
--waits;
265
}
266
else {
267
while(add_more) {
268
if(add_transfer(multi, share, resolve, url, http_version))
269
goto cleanup;
270
++ongoing;
271
--add_more;
272
}
273
}
274
275
/* Check for finished handles and remove. */
276
/* !checksrc! disable EQUALSNULL 1 */
277
while((msg = curl_multi_info_read(multi, &msgs_in_queue)) != NULL) {
278
if(msg->msg == CURLMSG_DONE) {
279
long status = 0;
280
curl_off_t xfer_id;
281
curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
282
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
283
if(msg->data.result == CURLE_SEND_ERROR ||
284
msg->data.result == CURLE_RECV_ERROR) {
285
/* We get these if the server had a GOAWAY in transit on
286
* re-using a connection */
287
}
288
else if(msg->data.result) {
289
fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
290
": failed with %d\n", xfer_id, msg->data.result);
291
goto cleanup;
292
}
293
else if(status != 200) {
294
fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
295
": wrong http status %ld (expected 200)\n", xfer_id, status);
296
goto cleanup;
297
}
298
curl_multi_remove_handle(multi, msg->easy_handle);
299
curl_easy_cleanup(msg->easy_handle);
300
--ongoing;
301
fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
302
"(%d now running)\n", xfer_id, running_handles);
303
}
304
}
305
306
fprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
307
running_handles, add_more);
308
309
} while(ongoing || add_more);
310
311
fprintf(stderr, "exiting\n");
312
exitcode = EXIT_SUCCESS;
313
314
cleanup:
315
316
if(multi) {
317
CURL **list = curl_multi_get_handles(multi);
318
if(list) {
319
int i;
320
for(i = 0; list[i]; i++) {
321
curl_multi_remove_handle(multi, list[i]);
322
curl_easy_cleanup(list[i]);
323
}
324
curl_free(list);
325
}
326
curl_multi_cleanup(multi);
327
}
328
curl_share_cleanup(share);
329
curl_slist_free_all(resolve);
330
curl_free(host);
331
curl_free(port);
332
curl_url_cleanup(cu);
333
334
return exitcode;
335
}
336
337