multi-post.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * This is an example application source code using the multi interface
  11. * to do a multipart formpost without "blocking".
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/time.h>
  16. #include <curl/curl.h>
  17. int main(int argc, char *argv[])
  18. {
  19. CURL *curl;
  20. CURLM *multi_handle;
  21. int still_running;
  22. struct curl_httppost *formpost=NULL;
  23. struct curl_httppost *lastptr=NULL;
  24. struct curl_slist *headerlist=NULL;
  25. static const char buf[] = "Expect:";
  26. /* Fill in the file upload field. This makes libcurl load data from
  27. the given file name when curl_easy_perform() is called. */
  28. curl_formadd(&formpost,
  29. &lastptr,
  30. CURLFORM_COPYNAME, "sendfile",
  31. CURLFORM_FILE, "postit2.c",
  32. CURLFORM_END);
  33. /* Fill in the filename field */
  34. curl_formadd(&formpost,
  35. &lastptr,
  36. CURLFORM_COPYNAME, "filename",
  37. CURLFORM_COPYCONTENTS, "postit2.c",
  38. CURLFORM_END);
  39. /* Fill in the submit field too, even if this is rarely needed */
  40. curl_formadd(&formpost,
  41. &lastptr,
  42. CURLFORM_COPYNAME, "submit",
  43. CURLFORM_COPYCONTENTS, "send",
  44. CURLFORM_END);
  45. curl = curl_easy_init();
  46. multi_handle = curl_multi_init();
  47. /* initalize custom header list (stating that Expect: 100-continue is not
  48. wanted */
  49. headerlist = curl_slist_append(headerlist, buf);
  50. if(curl && multi_handle) {
  51. /* what URL that receives this POST */
  52. curl_easy_setopt(curl, CURLOPT_URL,
  53. "http://www.fillinyoururl.com/upload.cgi");
  54. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  55. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  56. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  57. curl_multi_add_handle(multi_handle, curl);
  58. while(CURLM_CALL_MULTI_PERFORM ==
  59. curl_multi_perform(multi_handle, &still_running));
  60. while(still_running) {
  61. struct timeval timeout;
  62. int rc; /* select() return code */
  63. fd_set fdread;
  64. fd_set fdwrite;
  65. fd_set fdexcep;
  66. int maxfd;
  67. FD_ZERO(&fdread);
  68. FD_ZERO(&fdwrite);
  69. FD_ZERO(&fdexcep);
  70. /* set a suitable timeout to play around with */
  71. timeout.tv_sec = 1;
  72. timeout.tv_usec = 0;
  73. /* get file descriptors from the transfers */
  74. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  75. /* In a real-world program you OF COURSE check the return code of the
  76. function calls, *and* you make sure that maxfd is bigger than -1
  77. so that the call to select() below makes sense! */
  78. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  79. switch(rc) {
  80. case -1:
  81. /* select error */
  82. break;
  83. case 0:
  84. printf("timeout!\n");
  85. default:
  86. /* timeout or readable/writable sockets */
  87. printf("perform!\n");
  88. while(CURLM_CALL_MULTI_PERFORM ==
  89. curl_multi_perform(multi_handle, &still_running));
  90. printf("running: %d!\n", still_running);
  91. break;
  92. }
  93. }
  94. curl_multi_cleanup(multi_handle);
  95. /* always cleanup */
  96. curl_easy_cleanup(curl);
  97. /* then cleanup the formpost chain */
  98. curl_formfree(formpost);
  99. /* free slist */
  100. curl_slist_free_all (headerlist);
  101. }
  102. return 0;
  103. }