Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1531.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
#include "test.h"
25
26
#include "testutil.h"
27
#include "timediff.h"
28
#include "warnless.h"
29
#include "memdebug.h"
30
31
#define TEST_HANG_TIMEOUT 60 * 1000
32
33
static char const testData[] = ".abc\0xyz";
34
static curl_off_t const testDataSize = sizeof(testData) - 1;
35
36
CURLcode test(char *URL)
37
{
38
CURL *easy;
39
CURLM *multi_handle;
40
int still_running; /* keep number of running handles */
41
CURLMsg *msg; /* for picking up messages with the transfer status */
42
int msgs_left; /* how many messages are left */
43
CURLcode res = CURLE_OK;
44
45
start_test_timing();
46
47
global_init(CURL_GLOBAL_ALL);
48
49
/* Allocate one curl handle per transfer */
50
easy = curl_easy_init();
51
52
/* init a multi stack */
53
multi_handle = curl_multi_init();
54
55
/* add the individual transfer */
56
curl_multi_add_handle(multi_handle, easy);
57
58
/* set the options (I left out a few, you'll get the point anyway) */
59
curl_easy_setopt(easy, CURLOPT_URL, URL);
60
curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, testDataSize);
61
curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
62
63
/* we start some action by calling perform right away */
64
curl_multi_perform(multi_handle, &still_running);
65
66
abort_on_test_timeout();
67
68
do {
69
struct timeval timeout;
70
int rc; /* select() return code */
71
CURLMcode mc; /* curl_multi_fdset() return code */
72
73
fd_set fdread;
74
fd_set fdwrite;
75
fd_set fdexcep;
76
int maxfd = -1;
77
78
long curl_timeo = -1;
79
80
FD_ZERO(&fdread);
81
FD_ZERO(&fdwrite);
82
FD_ZERO(&fdexcep);
83
84
/* set a suitable timeout to play around with */
85
timeout.tv_sec = 1;
86
timeout.tv_usec = 0;
87
88
curl_multi_timeout(multi_handle, &curl_timeo);
89
if(curl_timeo >= 0) {
90
curlx_mstotv(&timeout, curl_timeo);
91
if(timeout.tv_sec > 1) {
92
timeout.tv_sec = 1;
93
timeout.tv_usec = 0;
94
}
95
}
96
97
/* get file descriptors from the transfers */
98
mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
99
100
if(mc != CURLM_OK) {
101
curl_mfprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
102
break;
103
}
104
105
/* On success the value of maxfd is guaranteed to be >= -1. We call
106
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
107
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
108
to sleep 100ms, which is the minimum suggested value in the
109
curl_multi_fdset() doc. */
110
111
if(maxfd == -1) {
112
#ifdef _WIN32
113
Sleep(100);
114
rc = 0;
115
#else
116
/* Portable sleep for platforms other than Windows. */
117
struct timeval wait = {0};
118
wait.tv_usec = 100 * 1000; /* 100ms */
119
rc = select(0, NULL, NULL, NULL, &wait);
120
#endif
121
}
122
else {
123
/* Note that on some platforms 'timeout' may be modified by select().
124
If you need access to the original value save a copy beforehand. */
125
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
126
}
127
128
switch(rc) {
129
case -1:
130
/* select error */
131
break;
132
case 0: /* timeout */
133
default: /* action */
134
curl_multi_perform(multi_handle, &still_running);
135
break;
136
}
137
138
abort_on_test_timeout();
139
} while(still_running);
140
141
/* See how the transfers went */
142
do {
143
msg = curl_multi_info_read(multi_handle, &msgs_left);
144
if(msg && msg->msg == CURLMSG_DONE) {
145
curl_mprintf("HTTP transfer completed with status %d\n",
146
msg->data.result);
147
break;
148
}
149
150
abort_on_test_timeout();
151
} while(msg);
152
153
test_cleanup:
154
curl_multi_cleanup(multi_handle);
155
156
/* Free the curl handles */
157
curl_easy_cleanup(easy);
158
curl_global_cleanup();
159
160
return res;
161
}
162
163