multi-post.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  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. curl_multi_perform(multi_handle, &still_running);
  66. while(still_running) {
  67. struct timeval timeout;
  68. int rc; /* select() return code */
  69. CURLMcode mc; /* curl_multi_fdset() return code */
  70. fd_set fdread;
  71. fd_set fdwrite;
  72. fd_set fdexcep;
  73. int maxfd = -1;
  74. long curl_timeo = -1;
  75. FD_ZERO(&fdread);
  76. FD_ZERO(&fdwrite);
  77. FD_ZERO(&fdexcep);
  78. /* set a suitable timeout to play around with */
  79. timeout.tv_sec = 1;
  80. timeout.tv_usec = 0;
  81. curl_multi_timeout(multi_handle, &curl_timeo);
  82. if(curl_timeo >= 0) {
  83. timeout.tv_sec = curl_timeo / 1000;
  84. if(timeout.tv_sec > 1)
  85. timeout.tv_sec = 1;
  86. else
  87. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  88. }
  89. /* get file descriptors from the transfers */
  90. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  91. if(mc != CURLM_OK) {
  92. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  93. break;
  94. }
  95. /* On success the value of maxfd is guaranteed to be >= -1. We call
  96. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  97. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  98. to sleep 100ms, which is the minimum suggested value in the
  99. curl_multi_fdset() doc. */
  100. if(maxfd == -1) {
  101. #ifdef _WIN32
  102. Sleep(100);
  103. rc = 0;
  104. #else
  105. /* Portable sleep for platforms other than Windows. */
  106. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  107. rc = select(0, NULL, NULL, NULL, &wait);
  108. #endif
  109. }
  110. else {
  111. /* Note that on some platforms 'timeout' may be modified by select().
  112. If you need access to the original value save a copy beforehand. */
  113. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  114. }
  115. switch(rc) {
  116. case -1:
  117. /* select error */
  118. break;
  119. case 0:
  120. default:
  121. /* timeout or readable/writable sockets */
  122. printf("perform!\n");
  123. curl_multi_perform(multi_handle, &still_running);
  124. printf("running: %d!\n", still_running);
  125. break;
  126. }
  127. }
  128. curl_multi_cleanup(multi_handle);
  129. /* always cleanup */
  130. curl_easy_cleanup(curl);
  131. /* then cleanup the form */
  132. curl_mime_free(form);
  133. /* free slist */
  134. curl_slist_free_all(headerlist);
  135. }
  136. return 0;
  137. }