1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * This is an example application source code using the multi interface
10 * to do a multipart formpost without "blocking".
16 #include <curl/curl.h>
18 int main(int argc, char *argv[])
25 struct curl_httppost *formpost=NULL;
26 struct curl_httppost *lastptr=NULL;
27 struct curl_slist *headerlist=NULL;
28 static const char buf[] = "Expect:";
30 /* Fill in the file upload field. This makes libcurl load data from
31 the given file name when curl_easy_perform() is called. */
32 curl_formadd(&formpost,
34 CURLFORM_COPYNAME, "sendfile",
35 CURLFORM_FILE, "postit2.c",
38 /* Fill in the filename field */
39 curl_formadd(&formpost,
41 CURLFORM_COPYNAME, "filename",
42 CURLFORM_COPYCONTENTS, "postit2.c",
45 /* Fill in the submit field too, even if this is rarely needed */
46 curl_formadd(&formpost,
48 CURLFORM_COPYNAME, "submit",
49 CURLFORM_COPYCONTENTS, "send",
52 curl = curl_easy_init();
53 multi_handle = curl_multi_init();
55 /* initalize custom header list (stating that Expect: 100-continue is not
57 headerlist = curl_slist_append(headerlist, buf);
58 if(curl && multi_handle) {
60 /* what URL that receives this POST */
61 curl_easy_setopt(curl, CURLOPT_URL,
62 "http://www.fillinyoururl.com/upload.cgi");
63 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
65 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
66 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
68 curl_multi_add_handle(multi_handle, curl);
70 curl_multi_perform(multi_handle, &still_running);
72 while(still_running) {
73 struct timeval timeout;
74 int rc; /* select() return code */
87 /* set a suitable timeout to play around with */
91 curl_multi_timeout(multi_handle, &curl_timeo);
93 timeout.tv_sec = curl_timeo / 1000;
94 if(timeout.tv_sec > 1)
97 timeout.tv_usec = (curl_timeo % 1000) * 1000;
100 /* get file descriptors from the transfers */
101 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
103 /* In a real-world program you OF COURSE check the return code of the
104 function calls. On success, the value of maxfd is guaranteed to be
105 greater or equal than -1. We call select(maxfd + 1, ...), specially in
106 case of (maxfd == -1), we call select(0, ...), which is basically equal
109 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
116 printf("timeout!\n");
118 /* timeout or readable/writable sockets */
119 printf("perform!\n");
120 curl_multi_perform(multi_handle, &still_running);
121 printf("running: %d!\n", still_running);
126 curl_multi_cleanup(multi_handle);
129 curl_easy_cleanup(curl);
131 /* then cleanup the formpost chain */
132 curl_formfree(formpost);
135 curl_slist_free_all (headerlist);