2
0

multi-formadd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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. struct curl_httppost *formpost = NULL;
  36. struct curl_httppost *lastptr = NULL;
  37. struct curl_slist *headerlist = NULL;
  38. static const char buf[] = "Expect:";
  39. /* Fill in the file upload field. This makes libcurl load data from
  40. the given file name when curl_easy_perform() is called. */
  41. curl_formadd(&formpost,
  42. &lastptr,
  43. CURLFORM_COPYNAME, "sendfile",
  44. CURLFORM_FILE, "postit2.c",
  45. CURLFORM_END);
  46. /* Fill in the filename field */
  47. curl_formadd(&formpost,
  48. &lastptr,
  49. CURLFORM_COPYNAME, "filename",
  50. CURLFORM_COPYCONTENTS, "postit2.c",
  51. CURLFORM_END);
  52. /* Fill in the submit field too, even if this is rarely needed */
  53. curl_formadd(&formpost,
  54. &lastptr,
  55. CURLFORM_COPYNAME, "submit",
  56. CURLFORM_COPYCONTENTS, "send",
  57. CURLFORM_END);
  58. curl = curl_easy_init();
  59. multi_handle = curl_multi_init();
  60. /* initialize custom header list (stating that Expect: 100-continue is not
  61. wanted */
  62. headerlist = curl_slist_append(headerlist, buf);
  63. if(curl && multi_handle) {
  64. /* what URL that receives this POST */
  65. curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
  66. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  67. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  68. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  69. curl_multi_add_handle(multi_handle, curl);
  70. curl_multi_perform(multi_handle, &still_running);
  71. while(still_running) {
  72. struct timeval timeout;
  73. int rc; /* select() return code */
  74. CURLMcode mc; /* curl_multi_fdset() return code */
  75. fd_set fdread;
  76. fd_set fdwrite;
  77. fd_set fdexcep;
  78. int maxfd = -1;
  79. long curl_timeo = -1;
  80. FD_ZERO(&fdread);
  81. FD_ZERO(&fdwrite);
  82. FD_ZERO(&fdexcep);
  83. /* set a suitable timeout to play around with */
  84. timeout.tv_sec = 1;
  85. timeout.tv_usec = 0;
  86. curl_multi_timeout(multi_handle, &curl_timeo);
  87. if(curl_timeo >= 0) {
  88. timeout.tv_sec = curl_timeo / 1000;
  89. if(timeout.tv_sec > 1)
  90. timeout.tv_sec = 1;
  91. else
  92. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  93. }
  94. /* get file descriptors from the transfers */
  95. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  96. if(mc != CURLM_OK) {
  97. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  98. break;
  99. }
  100. /* On success the value of maxfd is guaranteed to be >= -1. We call
  101. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  102. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  103. to sleep 100ms, which is the minimum suggested value in the
  104. curl_multi_fdset() doc. */
  105. if(maxfd == -1) {
  106. #ifdef _WIN32
  107. Sleep(100);
  108. rc = 0;
  109. #else
  110. /* Portable sleep for platforms other than Windows. */
  111. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  112. rc = select(0, NULL, NULL, NULL, &wait);
  113. #endif
  114. }
  115. else {
  116. /* Note that on some platforms 'timeout' may be modified by select().
  117. If you need access to the original value save a copy beforehand. */
  118. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  119. }
  120. switch(rc) {
  121. case -1:
  122. /* select error */
  123. break;
  124. case 0:
  125. default:
  126. /* timeout or readable/writable sockets */
  127. printf("perform!\n");
  128. curl_multi_perform(multi_handle, &still_running);
  129. printf("running: %d!\n", still_running);
  130. break;
  131. }
  132. }
  133. curl_multi_cleanup(multi_handle);
  134. /* always cleanup */
  135. curl_easy_cleanup(curl);
  136. /* then cleanup the formpost chain */
  137. curl_formfree(formpost);
  138. /* free slist */
  139. curl_slist_free_all(headerlist);
  140. }
  141. return 0;
  142. }