multi-formadd.c 3.3 KB

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