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