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