Imported Upstream version 7.48.0
[platform/upstream/curl.git] / tests / libtest / lib1531.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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
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, (curl_off_t)testDataSize);
53   curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
54
55   /* we start some action by calling perform right away */
56   curl_multi_perform(multi_handle, &still_running);
57
58   do {
59     struct timeval timeout;
60     int rc; /* select() return code */
61     CURLMcode mc; /* curl_multi_fdset() return code */
62
63     fd_set fdread;
64     fd_set fdwrite;
65     fd_set fdexcep;
66     int maxfd = -1;
67
68     long curl_timeo = -1;
69
70     FD_ZERO(&fdread);
71     FD_ZERO(&fdwrite);
72     FD_ZERO(&fdexcep);
73
74     /* set a suitable timeout to play around with */
75     timeout.tv_sec = 1;
76     timeout.tv_usec = 0;
77
78     curl_multi_timeout(multi_handle, &curl_timeo);
79     if(curl_timeo >= 0) {
80       timeout.tv_sec = curl_timeo / 1000;
81       if(timeout.tv_sec > 1)
82         timeout.tv_sec = 1;
83       else
84         timeout.tv_usec = (curl_timeo % 1000) * 1000;
85     }
86
87     /* get file descriptors from the transfers */
88     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
89
90     if(mc != CURLM_OK)
91     {
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