2
0

multi-post.c 3.1 KB

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