multi-formadd.c (3600B)
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 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 CURL_IGNORE_DEPRECATION( 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 75 curl = curl_easy_init(); 76 multi_handle = curl_multi_init(); 77 78 /* initialize custom header list (stating that Expect: 100-continue is not 79 wanted */ 80 headerlist = curl_slist_append(headerlist, buf); 81 if(curl && multi_handle) { 82 83 /* what URL that receives this POST */ 84 curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi"); 85 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 86 87 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); 88 CURL_IGNORE_DEPRECATION( 89 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); 90 ) 91 92 curl_multi_add_handle(multi_handle, curl); 93 94 do { 95 CURLMcode mc = curl_multi_perform(multi_handle, &still_running); 96 97 if(still_running) 98 /* wait for activity, timeout or "nothing" */ 99 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); 100 101 if(mc) 102 break; 103 104 } while(still_running); 105 106 curl_multi_cleanup(multi_handle); 107 108 /* always cleanup */ 109 curl_easy_cleanup(curl); 110 111 CURL_IGNORE_DEPRECATION( 112 /* then cleanup the formpost chain */ 113 curl_formfree(formpost); 114 ) 115 116 /* free slist */ 117 curl_slist_free_all(headerlist); 118 } 119 return 0; 120 }