1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * using the multi interface to do a multipart formpost without blocking
31 #include <curl/curl.h>
40 curl_mime *form = NULL;
41 curl_mimepart *field = NULL;
42 struct curl_slist *headerlist = NULL;
43 static const char buf[] = "Expect:";
45 curl = curl_easy_init();
46 multi_handle = curl_multi_init();
48 if(curl && multi_handle) {
50 form = curl_mime_init(curl);
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");
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);
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);
67 /* initialize custom header list (stating that Expect: 100-continue is not
69 headerlist = curl_slist_append(headerlist, buf);
71 /* what URL that receives this POST */
72 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
73 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
75 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
76 curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
78 curl_multi_add_handle(multi_handle, curl);
80 curl_multi_perform(multi_handle, &still_running);
83 struct timeval timeout;
84 int rc; /* select() return code */
85 CURLMcode mc; /* curl_multi_fdset() return code */
98 /* set a suitable timeout to play around with */
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)
108 timeout.tv_usec = (curl_timeo % 1000) * 1000;
111 /* get file descriptors from the transfers */
112 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
115 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
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. */
130 /* Portable sleep for platforms other than Windows. */
131 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
132 rc = select(0, NULL, NULL, NULL, &wait);
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);
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);
153 } while(still_running);
155 curl_multi_cleanup(multi_handle);
158 curl_easy_cleanup(curl);
160 /* then cleanup the form */
161 curl_mime_free(form);
164 curl_slist_free_all(headerlist);