• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, 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 /* <DESC>
23  * using the multi interface to do a multipart formpost without blocking
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 
31 #include <curl/curl.h>
32 
main(void)33 int main(void)
34 {
35   CURL *curl;
36 
37   CURLM *multi_handle;
38   int still_running = 0;
39 
40   curl_mime *form = NULL;
41   curl_mimepart *field = NULL;
42   struct curl_slist *headerlist = NULL;
43   static const char buf[] = "Expect:";
44 
45   curl = curl_easy_init();
46   multi_handle = curl_multi_init();
47 
48   if(curl && multi_handle) {
49     /* Create the form */
50     form = curl_mime_init(curl);
51 
52     /* Fill in the file upload field */
53     field = curl_mime_addpart(form);
54     curl_mime_name(field, "sendfile");
55     curl_mime_filedata(field, "multi-post.c");
56 
57     /* Fill in the filename field */
58     field = curl_mime_addpart(form);
59     curl_mime_name(field, "filename");
60     curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
61 
62     /* Fill in the submit field too, even if this is rarely needed */
63     field = curl_mime_addpart(form);
64     curl_mime_name(field, "submit");
65     curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
66 
67     /* initialize custom header list (stating that Expect: 100-continue is not
68        wanted */
69     headerlist = curl_slist_append(headerlist, buf);
70 
71     /* what URL that receives this POST */
72     curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
73     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
74 
75     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
76     curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
77 
78     curl_multi_add_handle(multi_handle, curl);
79 
80     curl_multi_perform(multi_handle, &still_running);
81 
82     while(still_running) {
83       struct timeval timeout;
84       int rc; /* select() return code */
85       CURLMcode mc; /* curl_multi_fdset() return code */
86 
87       fd_set fdread;
88       fd_set fdwrite;
89       fd_set fdexcep;
90       int maxfd = -1;
91 
92       long curl_timeo = -1;
93 
94       FD_ZERO(&fdread);
95       FD_ZERO(&fdwrite);
96       FD_ZERO(&fdexcep);
97 
98       /* set a suitable timeout to play around with */
99       timeout.tv_sec = 1;
100       timeout.tv_usec = 0;
101 
102       curl_multi_timeout(multi_handle, &curl_timeo);
103       if(curl_timeo >= 0) {
104         timeout.tv_sec = curl_timeo / 1000;
105         if(timeout.tv_sec > 1)
106           timeout.tv_sec = 1;
107         else
108           timeout.tv_usec = (curl_timeo % 1000) * 1000;
109       }
110 
111       /* get file descriptors from the transfers */
112       mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
113 
114       if(mc != CURLM_OK) {
115         fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
116         break;
117       }
118 
119       /* On success the value of maxfd is guaranteed to be >= -1. We call
120          select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
121          no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
122          to sleep 100ms, which is the minimum suggested value in the
123          curl_multi_fdset() doc. */
124 
125       if(maxfd == -1) {
126 #ifdef _WIN32
127         Sleep(100);
128         rc = 0;
129 #else
130         /* Portable sleep for platforms other than Windows. */
131         struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
132         rc = select(0, NULL, NULL, NULL, &wait);
133 #endif
134       }
135       else {
136         /* Note that on some platforms 'timeout' may be modified by select().
137            If you need access to the original value save a copy beforehand. */
138         rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
139       }
140 
141       switch(rc) {
142       case -1:
143         /* select error */
144         break;
145       case 0:
146       default:
147         /* timeout or readable/writable sockets */
148         printf("perform!\n");
149         curl_multi_perform(multi_handle, &still_running);
150         printf("running: %d!\n", still_running);
151         break;
152       }
153     }
154 
155     curl_multi_cleanup(multi_handle);
156 
157     /* always cleanup */
158     curl_easy_cleanup(curl);
159 
160     /* then cleanup the form */
161     curl_mime_free(form);
162 
163     /* free slist */
164     curl_slist_free_all(headerlist);
165   }
166   return 0;
167 }
168