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