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