1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: multi-post.c,v 1.1 2002/05/06 13:38:28 bagder Exp $
10 * This is an example application source code using the multi interface
11 * to do a multipart formpost without "blocking".
17 #include <curl/curl.h>
19 int main(int argc, char *argv[])
27 struct HttpPost *formpost=NULL;
28 struct HttpPost *lastptr=NULL;
29 struct curl_slist *headerlist=NULL;
30 char buf[] = "Expect:";
32 /* Fill in the file upload field */
33 curl_formadd(&formpost,
35 CURLFORM_COPYNAME, "sendfile",
36 CURLFORM_FILE, "postit2.c",
39 /* Fill in the filename field */
40 curl_formadd(&formpost,
42 CURLFORM_COPYNAME, "filename",
43 CURLFORM_COPYCONTENTS, "postit2.c",
47 /* Fill in the submit field too, even if this is rarely needed */
48 curl_formadd(&formpost,
50 CURLFORM_COPYNAME, "submit",
51 CURLFORM_COPYCONTENTS, "send",
54 curl = curl_easy_init();
55 multi_handle = curl_multi_init();
57 /* initalize custom header list (stating that Expect: 100-continue is not
59 headerlist = curl_slist_append(headerlist, buf);
60 if(curl && multi_handle) {
63 /* what URL that receives this POST */
64 curl_easy_setopt(curl, CURLOPT_URL,
65 "http://www.fillinyoururl.com/upload.cgi");
66 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
68 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
69 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
71 curl_multi_add_handle(multi_handle, curl);
73 while(CURLM_CALL_MULTI_PERFORM ==
74 curl_multi_perform(multi_handle, &still_running));
76 while(still_running) {
77 struct timeval timeout;
78 int rc; /* select() return code */
89 /* set a suitable timeout to play around with */
93 /* get file descriptors from the transfers */
94 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
96 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
103 printf("timeout!\n");
105 /* timeout or readable/writable sockets */
106 printf("perform!\n");
107 while(CURLM_CALL_MULTI_PERFORM ==
108 curl_multi_perform(multi_handle, &still_running));
109 printf("running: %d!\n", still_running);
114 curl_multi_cleanup(multi_handle);
117 curl_easy_cleanup(curl);
119 /* then cleanup the formpost chain */
120 curl_formfree(formpost);
123 curl_slist_free_all (headerlist);