Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / multi-post.c
blob118085f3d44f00e7730966e3ee755c453e26b477
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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".
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/time.h>
17 #include <curl/curl.h>
19 int main(int argc, char *argv[])
21 CURL *curl;
23 CURLM *multi_handle;
24 int still_running;
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,
34 &lastptr,
35 CURLFORM_COPYNAME, "sendfile",
36 CURLFORM_FILE, "postit2.c",
37 CURLFORM_END);
39 /* Fill in the filename field */
40 curl_formadd(&formpost,
41 &lastptr,
42 CURLFORM_COPYNAME, "filename",
43 CURLFORM_COPYCONTENTS, "postit2.c",
44 CURLFORM_END);
46 /* Fill in the submit field too, even if this is rarely needed */
47 curl_formadd(&formpost,
48 &lastptr,
49 CURLFORM_COPYNAME, "submit",
50 CURLFORM_COPYCONTENTS, "send",
51 CURLFORM_END);
53 curl = curl_easy_init();
54 multi_handle = curl_multi_init();
56 /* initalize custom header list (stating that Expect: 100-continue is not
57 wanted */
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 */
78 fd_set fdread;
79 fd_set fdwrite;
80 fd_set fdexcep;
81 int maxfd;
83 FD_ZERO(&fdread);
84 FD_ZERO(&fdwrite);
85 FD_ZERO(&fdexcep);
87 /* set a suitable timeout to play around with */
88 timeout.tv_sec = 1;
89 timeout.tv_usec = 0;
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);
100 switch(rc) {
101 case -1:
102 /* select error */
103 break;
104 case 0:
105 printf("timeout!\n");
106 default:
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);
112 break;
116 curl_multi_cleanup(multi_handle);
118 /* always cleanup */
119 curl_easy_cleanup(curl);
121 /* then cleanup the formpost chain */
122 curl_formfree(formpost);
124 /* free slist */
125 curl_slist_free_all (headerlist);
127 return 0;