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   curl_mime *form = NULL;
41   curl_mimepart *field = NULL;
42   struct curl_slist *headerlist = NULL;
43   static const char buf[] = "Expect:";
44 
45   curl = curl_easy_init();
46   multi_handle = curl_multi_init();
47 
48   if(curl && multi_handle) {
49     /* Create the form */
50     form = curl_mime_init(curl);
51 
52     /* Fill in the file upload field */
53     field = curl_mime_addpart(form);
54     curl_mime_name(field, "sendfile");
55     curl_mime_filedata(field, "multi-post.c");
56 
57     /* Fill in the filename field */
58     field = curl_mime_addpart(form);
59     curl_mime_name(field, "filename");
60     curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
61 
62     /* Fill in the submit field too, even if this is rarely needed */
63     field = curl_mime_addpart(form);
64     curl_mime_name(field, "submit");
65     curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
66 
67     /* initialize custom header list (stating that Expect: 100-continue is not
68        wanted */
69     headerlist = curl_slist_append(headerlist, buf);
70 
71     /* what URL that receives this POST */
72     curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
73     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
74 
75     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
76     curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
77 
78     curl_multi_add_handle(multi_handle, curl);
79 
80     do {
81       CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
82 
83       if(still_running)
84         /* wait for activity, timeout or "nothing" */
85         mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
86 
87       if(mc)
88         break;
89     } while(still_running);
90 
91     curl_multi_cleanup(multi_handle);
92 
93     /* always cleanup */
94     curl_easy_cleanup(curl);
95 
96     /* then cleanup the form */
97     curl_mime_free(form);
98 
99     /* free slist */
100     curl_slist_free_all(headerlist);
101   }
102   return 0;
103 }
104