2
0

multi-formadd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <curl/curl.h>
  35. #ifdef __GNUC__
  36. #pragma GCC diagnostic push
  37. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  38. #endif
  39. int main(void)
  40. {
  41. CURL *curl;
  42. CURLM *multi_handle;
  43. int still_running = 0;
  44. struct curl_httppost *formpost = NULL;
  45. struct curl_httppost *lastptr = NULL;
  46. struct curl_slist *headerlist = NULL;
  47. static const char buf[] = "Expect:";
  48. /* Fill in the file upload field. This makes libcurl load data from
  49. the given file name when curl_easy_perform() is called. */
  50. curl_formadd(&formpost,
  51. &lastptr,
  52. CURLFORM_COPYNAME, "sendfile",
  53. CURLFORM_FILE, "multi-formadd.c",
  54. CURLFORM_END);
  55. /* Fill in the filename field */
  56. curl_formadd(&formpost,
  57. &lastptr,
  58. CURLFORM_COPYNAME, "filename",
  59. CURLFORM_COPYCONTENTS, "multi-formadd.c",
  60. CURLFORM_END);
  61. /* Fill in the submit field too, even if this is rarely needed */
  62. curl_formadd(&formpost,
  63. &lastptr,
  64. CURLFORM_COPYNAME, "submit",
  65. CURLFORM_COPYCONTENTS, "send",
  66. CURLFORM_END);
  67. curl = curl_easy_init();
  68. multi_handle = curl_multi_init();
  69. /* initialize custom header list (stating that Expect: 100-continue is not
  70. wanted */
  71. headerlist = curl_slist_append(headerlist, buf);
  72. if(curl && multi_handle) {
  73. /* what URL that receives this POST */
  74. curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
  75. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  76. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  77. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  78. curl_multi_add_handle(multi_handle, curl);
  79. do {
  80. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  81. if(still_running)
  82. /* wait for activity, timeout or "nothing" */
  83. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  84. if(mc)
  85. break;
  86. } while(still_running);
  87. curl_multi_cleanup(multi_handle);
  88. /* always cleanup */
  89. curl_easy_cleanup(curl);
  90. /* then cleanup the formpost chain */
  91. curl_formfree(formpost);
  92. /* free slist */
  93. curl_slist_free_all(headerlist);
  94. }
  95. return 0;
  96. }
  97. #ifdef __GNUC__
  98. #pragma GCC diagnostic pop
  99. #endif