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#define NUM_HANDLES 4
31
32int test(char *URL)
33{
34  int res = 0;
35  CURL *curl[NUM_HANDLES];
36  int running;
37  CURLM *m = NULL;
38  int i;
39  char target_url[256];
40  int handles_added = 0;
41
42  for(i=0; i < NUM_HANDLES; i++)
43    curl[i] = NULL;
44
45  start_test_timing();
46
47  global_init(CURL_GLOBAL_ALL);
48
49  multi_init(m);
50
51  /* get NUM_HANDLES easy handles */
52  for(i=0; i < NUM_HANDLES; i++) {
53    /* get an easy handle */
54    easy_init(curl[i]);
55    /* specify target */
56    snprintf(target_url, sizeof(target_url), "%s%04i", URL, i + 1);
57    target_url[sizeof(target_url) - 1] = '\0';
58    easy_setopt(curl[i], CURLOPT_URL, target_url);
59    /* go verbose */
60    easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
61    /* include headers */
62    easy_setopt(curl[i], CURLOPT_HEADER, 1L);
63  }
64
65  /* Add the first handle to multi. We do this to let libcurl detect
66     that the server can do pipelining. The rest of the handles will be
67     added later. */
68  multi_add_handle(m, curl[handles_added++]);
69
70  multi_setopt(m, CURLMOPT_PIPELINING, 1L);
71
72  fprintf(stderr, "Start at URL 0\n");
73
74  for(;;) {
75    struct timeval interval;
76    fd_set rd, wr, exc;
77    int maxfd = -99;
78
79    interval.tv_sec = 1;
80    interval.tv_usec = 0;
81
82    multi_perform(m, &running);
83
84    abort_on_test_timeout();
85
86    if(!running) {
87      if(handles_added >= NUM_HANDLES)
88        break; /* done */
89
90      /* Add the rest of the handles now that the first handle has completed
91         its request. */
92      while(handles_added < NUM_HANDLES)
93        multi_add_handle(m, curl[handles_added++]);
94    }
95
96    FD_ZERO(&rd);
97    FD_ZERO(&wr);
98    FD_ZERO(&exc);
99
100    multi_fdset(m, &rd, &wr, &exc, &maxfd);
101
102    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
103
104    select_test(maxfd+1, &rd, &wr, &exc, &interval);
105
106    abort_on_test_timeout();
107  }
108
109test_cleanup:
110
111  /* proper cleanup sequence - type PB */
112
113  for(i=0; i < NUM_HANDLES; i++) {
114    curl_multi_remove_handle(m, curl[i]);
115    curl_easy_cleanup(curl[i]);
116  }
117
118  curl_multi_cleanup(m);
119  curl_global_cleanup();
120
121  return res;
122}
123