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