1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
33 #define TEST_HANG_TIMEOUT 5 * 1000
34 #define MAX_EASY_HANDLES 3
36 static CURL *easy[MAX_EASY_HANDLES];
37 static curl_socket_t sockets[MAX_EASY_HANDLES];
40 static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)
42 ssize_t idx = ((CURL **) data) - easy;
47 const size_t failure = (size * nmemb) ? 0 : 1;
49 char *output = malloc(size * nmemb + 1);
51 fprintf(stderr, "output, malloc() failed\n");
52 res = TEST_ERR_MAJOR_BAD;
56 memcpy(output, ptr, size * nmemb);
57 output[size * nmemb] = '\0';
58 fprintf(stdout, "%s", output);
61 /* Get socket being used for this easy handle, otherwise CURL_SOCKET_BAD */
62 code = curl_easy_getinfo(easy[idx], CURLINFO_LASTSOCKET, &longdata);
63 if(CURLE_OK != code) {
64 fprintf(stderr, "%s:%d curl_easy_getinfo() failed, "
65 "with code %d (%s)\n",
66 __FILE__, __LINE__, (int)code, curl_easy_strerror(code));
67 res = TEST_ERR_MAJOR_BAD;
71 sock = CURL_SOCKET_BAD;
73 sock = (curl_socket_t)longdata;
75 if(sock != CURL_SOCKET_BAD) {
76 /* Track relationship between this easy handle and the socket. */
77 if(sockets[idx] == CURL_SOCKET_BAD) {
78 /* An easy handle without previous socket, record the socket. */
81 else if(sock != sockets[idx]) {
82 /* An easy handle with a socket different to previously
83 tracked one, log and fail right away. Known bug #37. */
84 fprintf(stderr, "Handle %d started on socket %d and moved to %d\n",
85 curlx_sztosi(idx), (int)sockets[idx], (int)sock);
86 res = TEST_ERR_MAJOR_BAD;
95 NeedSocketForNewHandle,
105 enum HandleState state = ReadyForNewHandle;
106 size_t urllen = strlen(url) + 4 + 1;
107 char *full_url = malloc(urllen);
112 fprintf(stderr, "Not enough memory for full url\n");
113 return TEST_ERR_MAJOR_BAD;
116 for(i = 0; i < MAX_EASY_HANDLES; ++i) {
118 sockets[i] = CURL_SOCKET_BAD;
121 res_global_init(CURL_GLOBAL_ALL);
129 #ifdef USE_PIPELINING
130 multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
131 multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
132 multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L);
136 struct timeval interval;
142 bool found_new_socket = FALSE;
144 /* Start a new handle if we aren't at the max */
145 if(state == ReadyForNewHandle) {
146 easy_init(easy[num_handles]);
148 if(num_handles % 3 == 2) {
149 snprintf(full_url, urllen, "%s0200", url);
150 easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
153 snprintf(full_url, urllen, "%s0100", url);
154 easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
156 easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
157 easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
158 easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
159 easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
160 easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
161 easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
162 easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
163 easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
165 multi_add_handle(multi, easy[num_handles]);
167 state = NeedSocketForNewHandle;
170 multi_perform(multi, &running);
172 abort_on_test_timeout();
174 if(!running && state == NoMoreHandles)
181 multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
183 /* At this point, maxfd is guaranteed to be greater or equal than -1. */
185 /* Any socket which is new in fdread is associated with the new handle */
186 for(i = 0; i <= maxfd; ++i) {
187 bool socket_exists = FALSE;
188 curl_socket_t curfd = (curl_socket_t)i;
190 if(!FD_ISSET(curfd, &fdread)) {
194 /* Check if this socket was already detected for an earlier handle (or
195 for this handle, num_handles-1, in the callback */
196 for(j = 0; j < num_handles; ++j) {
197 if(sockets[j] == curfd) {
198 socket_exists = TRUE;
206 if(found_new_socket || state != NeedSocketForNewHandle) {
207 fprintf(stderr, "Unexpected new socket\n");
208 res = TEST_ERR_MAJOR_BAD;
212 /* Now we know the socket is for the most recent handle, num_handles-1 */
213 if(sockets[num_handles-1] != CURL_SOCKET_BAD) {
214 /* A socket for this handle was already detected in the callback; if it
215 matched socket_exists should be true and we would never get here */
216 assert(curfd != sockets[num_handles-1]);
217 fprintf(stderr, "Handle %d wrote to socket %d then detected on %d\n",
218 num_handles-1, (int)sockets[num_handles-1], (int)curfd);
219 res = TEST_ERR_MAJOR_BAD;
223 sockets[num_handles-1] = curfd;
224 found_new_socket = TRUE;
225 /* continue to make sure there's only one new handle */
229 if(state == NeedSocketForNewHandle) {
230 if(maxfd != -1 && !found_new_socket) {
231 fprintf(stderr, "Warning: socket did not open immediately for new "
232 "handle (trying again)\n");
235 state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
239 multi_timeout(multi, &timeout);
241 /* At this point, timeout is guaranteed to be greater or equal than -1. */
243 fprintf(stderr, "%s:%d num_handles %d timeout %ld\n",
244 __FILE__, __LINE__, num_handles, timeout);
247 int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
248 interval.tv_sec = itimeout/1000;
249 interval.tv_usec = (itimeout%1000)*1000;
252 interval.tv_sec = TEST_HANG_TIMEOUT/1000+1;
253 interval.tv_usec = 0;
255 /* if there's no timeout and we get here on the last handle, we may
256 already have read the last part of the stream so waiting makes no
258 if(!running && num_handles == MAX_EASY_HANDLES) {
263 select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &interval);
265 abort_on_test_timeout();
270 /* proper cleanup sequence - type PB */
272 for(i = 0; i < MAX_EASY_HANDLES; i++) {
273 curl_multi_remove_handle(multi, easy[i]);
274 curl_easy_cleanup(easy[i]);
277 curl_multi_cleanup(multi);
278 curl_global_cleanup();