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