2
0

multi-post.c 3.0 KB

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