multi-post.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. /* This is an example application source code using the multi interface
  23. * to do a multipart formpost without "blocking". */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/time.h>
  27. #include <curl/curl.h>
  28. int main(void)
  29. {
  30. CURL *curl;
  31. CURLM *multi_handle;
  32. int still_running;
  33. struct curl_httppost *formpost=NULL;
  34. struct curl_httppost *lastptr=NULL;
  35. struct curl_slist *headerlist=NULL;
  36. static const char buf[] = "Expect:";
  37. /* Fill in the file upload field. This makes libcurl load data from
  38. the given file name when curl_easy_perform() is called. */
  39. curl_formadd(&formpost,
  40. &lastptr,
  41. CURLFORM_COPYNAME, "sendfile",
  42. CURLFORM_FILE, "postit2.c",
  43. CURLFORM_END);
  44. /* Fill in the filename field */
  45. curl_formadd(&formpost,
  46. &lastptr,
  47. CURLFORM_COPYNAME, "filename",
  48. CURLFORM_COPYCONTENTS, "postit2.c",
  49. CURLFORM_END);
  50. /* Fill in the submit field too, even if this is rarely needed */
  51. curl_formadd(&formpost,
  52. &lastptr,
  53. CURLFORM_COPYNAME, "submit",
  54. CURLFORM_COPYCONTENTS, "send",
  55. CURLFORM_END);
  56. curl = curl_easy_init();
  57. multi_handle = curl_multi_init();
  58. /* initalize custom header list (stating that Expect: 100-continue is not
  59. wanted */
  60. headerlist = curl_slist_append(headerlist, buf);
  61. if(curl && multi_handle) {
  62. /* what URL that receives this POST */
  63. curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
  64. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  65. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  66. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  67. curl_multi_add_handle(multi_handle, curl);
  68. curl_multi_perform(multi_handle, &still_running);
  69. do {
  70. struct timeval timeout;
  71. int rc; /* select() return code */
  72. fd_set fdread;
  73. fd_set fdwrite;
  74. fd_set fdexcep;
  75. int maxfd = -1;
  76. long curl_timeo = -1;
  77. FD_ZERO(&fdread);
  78. FD_ZERO(&fdwrite);
  79. FD_ZERO(&fdexcep);
  80. /* set a suitable timeout to play around with */
  81. timeout.tv_sec = 1;
  82. timeout.tv_usec = 0;
  83. curl_multi_timeout(multi_handle, &curl_timeo);
  84. if(curl_timeo >= 0) {
  85. timeout.tv_sec = curl_timeo / 1000;
  86. if(timeout.tv_sec > 1)
  87. timeout.tv_sec = 1;
  88. else
  89. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  90. }
  91. /* get file descriptors from the transfers */
  92. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  93. /* In a real-world program you OF COURSE check the return code of the
  94. function calls. On success, the value of maxfd is guaranteed to be
  95. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  96. case of (maxfd == -1), we call select(0, ...), which is basically equal
  97. to sleep. */
  98. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  99. switch(rc) {
  100. case -1:
  101. /* select error */
  102. break;
  103. case 0:
  104. default:
  105. /* timeout or readable/writable sockets */
  106. printf("perform!\n");
  107. curl_multi_perform(multi_handle, &still_running);
  108. printf("running: %d!\n", still_running);
  109. break;
  110. }
  111. } while(still_running);
  112. curl_multi_cleanup(multi_handle);
  113. /* always cleanup */
  114. curl_easy_cleanup(curl);
  115. /* then cleanup the formpost chain */
  116. curl_formfree(formpost);
  117. /* free slist */
  118. curl_slist_free_all (headerlist);
  119. }
  120. return 0;
  121. }