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