1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 http://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 struct curl_httppost *formpost=NULL;
41 struct curl_httppost *lastptr=NULL;
42 struct curl_slist *headerlist=NULL;
43 static const char buf[] = "Expect:";
45 /* Fill in the file upload field. This makes libcurl load data from
46 the given file name when curl_easy_perform() is called. */
47 curl_formadd(&formpost,
49 CURLFORM_COPYNAME, "sendfile",
50 CURLFORM_FILE, "postit2.c",
53 /* Fill in the filename field */
54 curl_formadd(&formpost,
56 CURLFORM_COPYNAME, "filename",
57 CURLFORM_COPYCONTENTS, "postit2.c",
60 /* Fill in the submit field too, even if this is rarely needed */
61 curl_formadd(&formpost,
63 CURLFORM_COPYNAME, "submit",
64 CURLFORM_COPYCONTENTS, "send",
67 curl = curl_easy_init();
68 multi_handle = curl_multi_init();
70 /* initialize custom header list (stating that Expect: 100-continue is not
72 headerlist = curl_slist_append(headerlist, buf);
73 if(curl && multi_handle) {
75 /* what URL that receives this POST */
76 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
77 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
79 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
80 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
82 curl_multi_add_handle(multi_handle, curl);
84 curl_multi_perform(multi_handle, &still_running);
87 struct timeval timeout;
88 int rc; /* select() return code */
89 CURLMcode mc; /* curl_multi_fdset() return code */
102 /* set a suitable timeout to play around with */
106 curl_multi_timeout(multi_handle, &curl_timeo);
107 if(curl_timeo >= 0) {
108 timeout.tv_sec = curl_timeo / 1000;
109 if(timeout.tv_sec > 1)
112 timeout.tv_usec = (curl_timeo % 1000) * 1000;
115 /* get file descriptors from the transfers */
116 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
120 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
124 /* On success the value of maxfd is guaranteed to be >= -1. We call
125 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
126 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
127 to sleep 100ms, which is the minimum suggested value in the
128 curl_multi_fdset() doc. */
135 /* Portable sleep for platforms other than Windows. */
136 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
137 rc = select(0, NULL, NULL, NULL, &wait);
141 /* Note that on some platforms 'timeout' may be modified by select().
142 If you need access to the original value save a copy beforehand. */
143 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
152 /* timeout or readable/writable sockets */
153 printf("perform!\n");
154 curl_multi_perform(multi_handle, &still_running);
155 printf("running: %d!\n", still_running);
158 } while(still_running);
160 curl_multi_cleanup(multi_handle);
163 curl_easy_cleanup(curl);
165 /* then cleanup the formpost chain */
166 curl_formfree(formpost);
169 curl_slist_free_all (headerlist);