• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * A basic application source code using the multi interface doing two
26  * transfers in parallel without curl_multi_wait/poll.
27  * </DESC>
28  */
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 /* somewhat unix-specific */
34 #include <sys/time.h>
35 #include <unistd.h>
36 
37 /* curl stuff */
38 #include <curl/curl.h>
39 
40 /*
41  * Download an HTTP file and upload an FTP file simultaneously.
42  */
43 
44 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
45 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
46 #define FTP_HANDLE 1    /* Index for the FTP transfer */
47 
main(void)48 int main(void)
49 {
50   CURL *handles[HANDLECOUNT];
51   CURLM *multi_handle;
52 
53   int still_running = 0; /* keep number of running handles */
54   int i;
55 
56   CURLMsg *msg; /* for picking up messages with the transfer status */
57   int msgs_left; /* how many messages are left */
58 
59   /* Allocate one CURL handle per transfer */
60   for(i = 0; i<HANDLECOUNT; i++)
61     handles[i] = curl_easy_init();
62 
63   /* set the options (I left out a few, you will get the point anyway) */
64   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
65 
66   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
67   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
68 
69   /* init a multi stack */
70   multi_handle = curl_multi_init();
71 
72   /* add the individual transfers */
73   for(i = 0; i<HANDLECOUNT; i++)
74     curl_multi_add_handle(multi_handle, handles[i]);
75 
76   /* we start some action by calling perform right away */
77   curl_multi_perform(multi_handle, &still_running);
78 
79   while(still_running) {
80     struct timeval timeout;
81     int rc; /* select() return code */
82     CURLMcode mc; /* curl_multi_fdset() return code */
83 
84     fd_set fdread;
85     fd_set fdwrite;
86     fd_set fdexcep;
87     int maxfd = -1;
88 
89     long curl_timeo = -1;
90 
91     FD_ZERO(&fdread);
92     FD_ZERO(&fdwrite);
93     FD_ZERO(&fdexcep);
94 
95     /* set a suitable timeout to play around with */
96     timeout.tv_sec = 1;
97     timeout.tv_usec = 0;
98 
99     curl_multi_timeout(multi_handle, &curl_timeo);
100     if(curl_timeo >= 0) {
101       timeout.tv_sec = curl_timeo / 1000;
102       if(timeout.tv_sec > 1)
103         timeout.tv_sec = 1;
104       else
105         timeout.tv_usec = (int)(curl_timeo % 1000) * 1000;
106     }
107 
108     /* get file descriptors from the transfers */
109     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
110 
111     if(mc != CURLM_OK) {
112       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
113       break;
114     }
115 
116     /* On success the value of maxfd is guaranteed to be >= -1. We call
117        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
118        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
119        to sleep 100ms, which is the minimum suggested value in the
120        curl_multi_fdset() doc. */
121 
122     if(maxfd == -1) {
123 #ifdef _WIN32
124       Sleep(100);
125       rc = 0;
126 #else
127       /* Portable sleep for platforms other than Windows. */
128       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
129       rc = select(0, NULL, NULL, NULL, &wait);
130 #endif
131     }
132     else {
133       /* Note that on some platforms 'timeout' may be modified by select().
134          If you need access to the original value save a copy beforehand. */
135       rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
136     }
137 
138     switch(rc) {
139     case -1:
140       /* select error */
141       break;
142     case 0: /* timeout */
143     default: /* action */
144       curl_multi_perform(multi_handle, &still_running);
145       break;
146     }
147   }
148 
149   /* See how the transfers went */
150   while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
151     if(msg->msg == CURLMSG_DONE) {
152       int idx;
153 
154       /* Find out which handle this message is about */
155       for(idx = 0; idx<HANDLECOUNT; idx++) {
156         int found = (msg->easy_handle == handles[idx]);
157         if(found)
158           break;
159       }
160 
161       switch(idx) {
162       case HTTP_HANDLE:
163         printf("HTTP transfer completed with status %d\n", msg->data.result);
164         break;
165       case FTP_HANDLE:
166         printf("FTP transfer completed with status %d\n", msg->data.result);
167         break;
168       }
169     }
170   }
171 
172   curl_multi_cleanup(multi_handle);
173 
174   /* Free the CURL handles */
175   for(i = 0; i<HANDLECOUNT; i++)
176     curl_easy_cleanup(handles[i]);
177 
178   return 0;
179 }
180