multi-formadd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * Warning: this example uses the deprecated form api. See "multi-post.c"
  30. * for a similar example using the mime api.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/time.h>
  35. #include <curl/curl.h>
  36. int main(void)
  37. {
  38. CURL *curl;
  39. CURLM *multi_handle;
  40. int still_running = 0;
  41. struct curl_httppost *formpost = NULL;
  42. struct curl_httppost *lastptr = NULL;
  43. struct curl_slist *headerlist = NULL;
  44. static const char buf[] = "Expect:";
  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, "multi-formadd.c",
  51. CURLFORM_END);
  52. /* Fill in the filename field */
  53. curl_formadd(&formpost,
  54. &lastptr,
  55. CURLFORM_COPYNAME, "filename",
  56. CURLFORM_COPYCONTENTS, "multi-formadd.c",
  57. CURLFORM_END);
  58. /* Fill in the submit field too, even if this is rarely needed */
  59. curl_formadd(&formpost,
  60. &lastptr,
  61. CURLFORM_COPYNAME, "submit",
  62. CURLFORM_COPYCONTENTS, "send",
  63. CURLFORM_END);
  64. curl = curl_easy_init();
  65. multi_handle = curl_multi_init();
  66. /* initialize custom header list (stating that Expect: 100-continue is not
  67. wanted */
  68. headerlist = curl_slist_append(headerlist, buf);
  69. if(curl && multi_handle) {
  70. /* what URL that receives this POST */
  71. curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
  72. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  73. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  74. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  75. curl_multi_add_handle(multi_handle, curl);
  76. do {
  77. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  78. if(still_running)
  79. /* wait for activity, timeout or "nothing" */
  80. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  81. if(mc)
  82. break;
  83. } while(still_running);
  84. curl_multi_cleanup(multi_handle);
  85. /* always cleanup */
  86. curl_easy_cleanup(curl);
  87. /* then cleanup the formpost chain */
  88. curl_formfree(formpost);
  89. /* free slist */
  90. curl_slist_free_all(headerlist);
  91. }
  92. return 0;
  93. }