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, "http://www.example.com/upload.cgi");
62 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
64 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
65 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
67 curl_multi_add_handle(multi_handle, curl);
69 curl_multi_perform(multi_handle, &still_running);
71 while(still_running) {
72 struct timeval timeout;
73 int rc; /* select() return code */
86 /* set a suitable timeout to play around with */
90 curl_multi_timeout(multi_handle, &curl_timeo);
92 timeout.tv_sec = curl_timeo / 1000;
93 if(timeout.tv_sec > 1)
96 timeout.tv_usec = (curl_timeo % 1000) * 1000;
99 /* get file descriptors from the transfers */
100 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
102 /* In a real-world program you OF COURSE check the return code of the
103 function calls. On success, the value of maxfd is guaranteed to be
104 greater or equal than -1. We call select(maxfd + 1, ...), specially in
105 case of (maxfd == -1), we call select(0, ...), which is basically equal
108 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
116 /* timeout or readable/writable sockets */
117 printf("perform!\n");
118 curl_multi_perform(multi_handle, &still_running);
119 printf("running: %d!\n", still_running);
124 curl_multi_cleanup(multi_handle);
127 curl_easy_cleanup(curl);
129 /* then cleanup the formpost chain */
130 curl_formfree(formpost);
133 curl_slist_free_all (headerlist);