1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: multi-post.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman 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
[])
26 struct curl_httppost
*formpost
=NULL
;
27 struct curl_httppost
*lastptr
=NULL
;
28 struct curl_slist
*headerlist
=NULL
;
29 static const char buf
[] = "Expect:";
31 /* Fill in the file upload field. This makes libcurl load data from
32 the given file name when curl_easy_perform() is called. */
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",
46 /* Fill in the submit field too, even if this is rarely needed */
47 curl_formadd(&formpost
,
49 CURLFORM_COPYNAME
, "submit",
50 CURLFORM_COPYCONTENTS
, "send",
53 curl
= curl_easy_init();
54 multi_handle
= curl_multi_init();
56 /* initalize custom header list (stating that Expect: 100-continue is not
58 headerlist
= curl_slist_append(headerlist
, buf
);
59 if(curl
&& multi_handle
) {
61 /* what URL that receives this POST */
62 curl_easy_setopt(curl
, CURLOPT_URL
,
63 "http://www.fillinyoururl.com/upload.cgi");
64 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, 1L);
66 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headerlist
);
67 curl_easy_setopt(curl
, CURLOPT_HTTPPOST
, formpost
);
69 curl_multi_add_handle(multi_handle
, curl
);
71 while(CURLM_CALL_MULTI_PERFORM
==
72 curl_multi_perform(multi_handle
, &still_running
));
74 while(still_running
) {
75 struct timeval timeout
;
76 int rc
; /* select() return code */
87 /* set a suitable timeout to play around with */
91 /* get file descriptors from the transfers */
92 curl_multi_fdset(multi_handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
94 /* In a real-world program you OF COURSE check the return code of the
95 function calls, *and* you make sure that maxfd is bigger than -1
96 so that the call to select() below makes sense! */
98 rc
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
105 printf("timeout!\n");
107 /* timeout or readable/writable sockets */
108 printf("perform!\n");
109 while(CURLM_CALL_MULTI_PERFORM
==
110 curl_multi_perform(multi_handle
, &still_running
));
111 printf("running: %d!\n", still_running
);
116 curl_multi_cleanup(multi_handle
);
119 curl_easy_cleanup(curl
);
121 /* then cleanup the formpost chain */
122 curl_formfree(formpost
);
125 curl_slist_free_all (headerlist
);