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 #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 off_t const testDataSize = sizeof(testData) - 1;
35
test(char * URL)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,
61 (curl_off_t)testDataSize);
62 curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
63
64 /* we start some action by calling perform right away */
65 curl_multi_perform(multi_handle, &still_running);
66
67 abort_on_test_timeout();
68
69 do {
70 struct timeval timeout;
71 int rc; /* select() return code */
72 CURLMcode mc; /* curl_multi_fdset() return code */
73
74 fd_set fdread;
75 fd_set fdwrite;
76 fd_set fdexcep;
77 int maxfd = -1;
78
79 long curl_timeo = -1;
80
81 FD_ZERO(&fdread);
82 FD_ZERO(&fdwrite);
83 FD_ZERO(&fdexcep);
84
85 /* set a suitable timeout to play around with */
86 timeout.tv_sec = 1;
87 timeout.tv_usec = 0;
88
89 curl_multi_timeout(multi_handle, &curl_timeo);
90 if(curl_timeo >= 0) {
91 curlx_mstotv(&timeout, curl_timeo);
92 if(timeout.tv_sec > 1) {
93 timeout.tv_sec = 1;
94 timeout.tv_usec = 0;
95 }
96 }
97
98 /* get file descriptors from the transfers */
99 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
100
101 if(mc != CURLM_OK) {
102 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
103 break;
104 }
105
106 /* On success the value of maxfd is guaranteed to be >= -1. We call
107 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
108 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
109 to sleep 100ms, which is the minimum suggested value in the
110 curl_multi_fdset() doc. */
111
112 if(maxfd == -1) {
113 #if defined(_WIN32)
114 Sleep(100);
115 rc = 0;
116 #else
117 /* Portable sleep for platforms other than Windows. */
118 struct timeval wait = { 0, 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 printf("HTTP transfer completed with status %d\n", msg->data.result);
146 break;
147 }
148
149 abort_on_test_timeout();
150 } while(msg);
151
152 test_cleanup:
153 curl_multi_cleanup(multi_handle);
154
155 /* Free the CURL handles */
156 curl_easy_cleanup(easy);
157 curl_global_cleanup();
158
159 return res;
160 }
161