1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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 http://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 "warnless.h"
25
26/*
27 * Simply download a HTTPS file!
28 *
29 * This test was added after the HTTPS-using-multi-interface with OpenSSL
30 * regression of 7.19.1 to hopefully prevent this embarassing mistake from
31 * appearing again... Unfortunately the bug wasn't triggered by this test,
32 * which presumably is because the connect to a local server is too
33 * fast/different compared to the real/distant servers we saw the bug happen
34 * with.
35 */
36int test(char *URL)
37{
38  CURL *http_handle;
39  CURLM *multi_handle = NULL;
40  CURLMcode code;
41  int res;
42
43  int still_running; /* keep number of running handles */
44
45  http_handle = curl_easy_init();
46  if (!http_handle)
47    return TEST_ERR_MAJOR_BAD;
48
49  /* set options */
50  test_setopt(http_handle, CURLOPT_URL, URL);
51  test_setopt(http_handle, CURLOPT_HEADER, 1L);
52  test_setopt(http_handle, CURLOPT_SSL_VERIFYPEER, 0L);
53  test_setopt(http_handle, CURLOPT_SSL_VERIFYHOST, 0L);
54
55  /* init a multi stack */
56  multi_handle = curl_multi_init();
57  if (!multi_handle) {
58    curl_easy_cleanup(http_handle);
59    return TEST_ERR_MAJOR_BAD;
60  }
61
62  /* add the individual transfers */
63  curl_multi_add_handle(multi_handle, http_handle);
64
65  /* we start some action by calling perform right away */
66  do {
67    code = curl_multi_perform(multi_handle, &still_running);
68  } while(code == CURLM_CALL_MULTI_PERFORM);
69
70  while(still_running) {
71    struct timeval timeout;
72    int rc; /* select() return code */
73
74    fd_set fdread;
75    fd_set fdwrite;
76    fd_set fdexcep;
77    int maxfd;
78
79    FD_ZERO(&fdread);
80    FD_ZERO(&fdwrite);
81    FD_ZERO(&fdexcep);
82
83    /* set a suitable timeout to play around with */
84    timeout.tv_sec = 1;
85    timeout.tv_usec = 0;
86
87    /* get file descriptors from the transfers */
88    curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
89
90    /* In a real-world program you OF COURSE check the return code of the
91       function calls, *and* you make sure that maxfd is bigger than -1 so
92       that the call to select() below makes sense! */
93
94    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
95
96    switch(rc) {
97    case -1:
98      /* select error */
99      break;
100    case 0:
101    default:
102      /* timeout or readable/writable sockets */
103      do {
104        code = curl_multi_perform(multi_handle, &still_running);
105      } while(code == CURLM_CALL_MULTI_PERFORM);
106      break;
107    }
108  }
109
110test_cleanup:
111
112  if(multi_handle)
113    curl_multi_cleanup(multi_handle);
114
115  curl_easy_cleanup(http_handle);
116  curl_global_cleanup();
117
118  return res;
119}
120