1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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 https://curl.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * using the multi interface to do a multipart formpost without blocking
26 * </DESC>
27 */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/time.h>
32
33 #include <curl/curl.h>
34
main(void)35 int main(void)
36 {
37 CURL *curl;
38
39 CURLM *multi_handle;
40 int still_running = 0;
41
42 curl_mime *form = NULL;
43 curl_mimepart *field = NULL;
44 struct curl_slist *headerlist = NULL;
45 static const char buf[] = "Expect:";
46
47 curl = curl_easy_init();
48 multi_handle = curl_multi_init();
49
50 if(curl && multi_handle) {
51 /* Create the form */
52 form = curl_mime_init(curl);
53
54 /* Fill in the file upload field */
55 field = curl_mime_addpart(form);
56 curl_mime_name(field, "sendfile");
57 curl_mime_filedata(field, "multi-post.c");
58
59 /* Fill in the filename field */
60 field = curl_mime_addpart(form);
61 curl_mime_name(field, "filename");
62 curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
63
64 /* Fill in the submit field too, even if this is rarely needed */
65 field = curl_mime_addpart(form);
66 curl_mime_name(field, "submit");
67 curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
68
69 /* initialize custom header list (stating that Expect: 100-continue is not
70 wanted */
71 headerlist = curl_slist_append(headerlist, buf);
72
73 /* what URL that receives this POST */
74 curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
75 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
76
77 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
78 curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
79
80 curl_multi_add_handle(multi_handle, curl);
81
82 do {
83 CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
84
85 if(still_running)
86 /* wait for activity, timeout or "nothing" */
87 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
88
89 if(mc)
90 break;
91 } while(still_running);
92
93 curl_multi_cleanup(multi_handle);
94
95 /* always cleanup */
96 curl_easy_cleanup(curl);
97
98 /* then cleanup the form */
99 curl_mime_free(form);
100
101 /* free slist */
102 curl_slist_free_all(headerlist);
103 }
104 return 0;
105 }
106