1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* This is an example application source code using the multi interface
23 * to do a multipart formpost without "blocking". */
28 #include <curl/curl.h>
37 struct curl_httppost *formpost=NULL;
38 struct curl_httppost *lastptr=NULL;
39 struct curl_slist *headerlist=NULL;
40 static const char buf[] = "Expect:";
42 /* Fill in the file upload field. This makes libcurl load data from
43 the given file name when curl_easy_perform() is called. */
44 curl_formadd(&formpost,
46 CURLFORM_COPYNAME, "sendfile",
47 CURLFORM_FILE, "postit2.c",
50 /* Fill in the filename field */
51 curl_formadd(&formpost,
53 CURLFORM_COPYNAME, "filename",
54 CURLFORM_COPYCONTENTS, "postit2.c",
57 /* Fill in the submit field too, even if this is rarely needed */
58 curl_formadd(&formpost,
60 CURLFORM_COPYNAME, "submit",
61 CURLFORM_COPYCONTENTS, "send",
64 curl = curl_easy_init();
65 multi_handle = curl_multi_init();
67 /* initalize custom header list (stating that Expect: 100-continue is not
69 headerlist = curl_slist_append(headerlist, buf);
70 if(curl && multi_handle) {
72 /* what URL that receives this POST */
73 curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
74 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
76 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
77 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
79 curl_multi_add_handle(multi_handle, curl);
81 curl_multi_perform(multi_handle, &still_running);
84 struct timeval timeout;
85 int rc; /* select() return code */
98 /* set a suitable timeout to play around with */
102 curl_multi_timeout(multi_handle, &curl_timeo);
103 if(curl_timeo >= 0) {
104 timeout.tv_sec = curl_timeo / 1000;
105 if(timeout.tv_sec > 1)
108 timeout.tv_usec = (curl_timeo % 1000) * 1000;
111 /* get file descriptors from the transfers */
112 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
114 /* In a real-world program you OF COURSE check the return code of the
115 function calls. On success, the value of maxfd is guaranteed to be
116 greater or equal than -1. We call select(maxfd + 1, ...), specially in
117 case of (maxfd == -1), we call select(0, ...), which is basically equal
120 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
128 /* timeout or readable/writable sockets */
129 printf("perform!\n");
130 curl_multi_perform(multi_handle, &still_running);
131 printf("running: %d!\n", still_running);
134 } while(still_running);
136 curl_multi_cleanup(multi_handle);
139 curl_easy_cleanup(curl);
141 /* then cleanup the formpost chain */
142 curl_formfree(formpost);
145 curl_slist_free_all (headerlist);