multi-post.c 3.0 KB

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