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 /*
30 * Warning: this example uses the deprecated form api. See "multi-post.c"
31 * for a similar example using the mime api.
32 */
33
34 #include <stdio.h>
35 #include <string.h>
36
37 #include <curl/curl.h>
38
main(void)39 int main(void)
40 {
41 CURL *curl;
42
43 CURLM *multi_handle;
44 int still_running = 0;
45
46 struct curl_httppost *formpost = NULL;
47 struct curl_httppost *lastptr = NULL;
48 struct curl_slist *headerlist = NULL;
49 static const char buf[] = "Expect:";
50
51 /* Fill in the file upload field. This makes libcurl load data from
52 the given file name when curl_easy_perform() is called. */
53 curl_formadd(&formpost,
54 &lastptr,
55 CURLFORM_COPYNAME, "sendfile",
56 CURLFORM_FILE, "multi-formadd.c",
57 CURLFORM_END);
58
59 /* Fill in the filename field */
60 curl_formadd(&formpost,
61 &lastptr,
62 CURLFORM_COPYNAME, "filename",
63 CURLFORM_COPYCONTENTS, "multi-formadd.c",
64 CURLFORM_END);
65
66 /* Fill in the submit field too, even if this is rarely needed */
67 curl_formadd(&formpost,
68 &lastptr,
69 CURLFORM_COPYNAME, "submit",
70 CURLFORM_COPYCONTENTS, "send",
71 CURLFORM_END);
72
73 curl = curl_easy_init();
74 multi_handle = curl_multi_init();
75
76 /* initialize custom header list (stating that Expect: 100-continue is not
77 wanted */
78 headerlist = curl_slist_append(headerlist, buf);
79 if(curl && multi_handle) {
80
81 /* what URL that receives this POST */
82 curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
83 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
84
85 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
86 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
87
88 curl_multi_add_handle(multi_handle, curl);
89
90 do {
91 CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
92
93 if(still_running)
94 /* wait for activity, timeout or "nothing" */
95 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
96
97 if(mc)
98 break;
99
100 } while(still_running);
101
102 curl_multi_cleanup(multi_handle);
103
104 /* always cleanup */
105 curl_easy_cleanup(curl);
106
107 /* then cleanup the formpost chain */
108 curl_formfree(formpost);
109
110 /* free slist */
111 curl_slist_free_all(headerlist);
112 }
113 return 0;
114 }
115