lib1507.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. #include "test.h"
  23. #include "testutil.h"
  24. #include "warnless.h"
  25. #include "memdebug.h"
  26. /*
  27. * This is the list of basic details you need to tweak to get things right.
  28. */
  29. #define USERNAME "user@example.com"
  30. #define PASSWORD "123qwerty"
  31. #define RECIPIENT "<1507-recipient@example.com>"
  32. #define MAILFROM "<1507-realuser@example.com>"
  33. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  34. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  35. {
  36. (void)ptr;
  37. (void)size;
  38. (void)nmemb;
  39. (void)userp;
  40. return CURL_READFUNC_ABORT;
  41. }
  42. static struct timeval tvnow(void)
  43. {
  44. /*
  45. ** time() returns the value of time in seconds since the Epoch.
  46. */
  47. struct timeval now;
  48. now.tv_sec = (long)time(NULL);
  49. now.tv_usec = 0;
  50. return now;
  51. }
  52. static long tvdiff(struct timeval newer, struct timeval older)
  53. {
  54. return (newer.tv_sec-older.tv_sec)*1000+
  55. (newer.tv_usec-older.tv_usec)/1000;
  56. }
  57. int test(char *URL)
  58. {
  59. int res = 0;
  60. CURL *curl = NULL;
  61. CURLM *mcurl = NULL;
  62. int still_running = 1;
  63. struct timeval mp_start;
  64. struct curl_slist* rcpt_list = NULL;
  65. curl_global_init(CURL_GLOBAL_DEFAULT);
  66. easy_init(curl);
  67. multi_init(mcurl);
  68. rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
  69. /* more addresses can be added here
  70. rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
  71. */
  72. curl_easy_setopt(curl, CURLOPT_URL, URL);
  73. #if 0
  74. curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
  75. curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
  76. #endif
  77. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  78. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  79. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
  80. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
  81. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  82. multi_add_handle(mcurl, curl);
  83. mp_start = tvnow();
  84. /* we start some action by calling perform right away */
  85. curl_multi_perform(mcurl, &still_running);
  86. while(still_running) {
  87. struct timeval timeout;
  88. int rc; /* select() return code */
  89. fd_set fdread;
  90. fd_set fdwrite;
  91. fd_set fdexcep;
  92. int maxfd = -1;
  93. long curl_timeo = -1;
  94. FD_ZERO(&fdread);
  95. FD_ZERO(&fdwrite);
  96. FD_ZERO(&fdexcep);
  97. /* set a suitable timeout to play around with */
  98. timeout.tv_sec = 1;
  99. timeout.tv_usec = 0;
  100. curl_multi_timeout(mcurl, &curl_timeo);
  101. if(curl_timeo >= 0) {
  102. timeout.tv_sec = curl_timeo / 1000;
  103. if(timeout.tv_sec > 1)
  104. timeout.tv_sec = 1;
  105. else
  106. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  107. }
  108. /* get file descriptors from the transfers */
  109. curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  110. /* In a real-world program you OF COURSE check the return code of the
  111. function calls. On success, the value of maxfd is guaranteed to be
  112. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  113. case of (maxfd == -1), we call select(0, ...), which is basically equal
  114. to sleep. */
  115. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  116. if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  117. fprintf(stderr, "ABORTING TEST, since it seems "
  118. "that it would have run forever.\n");
  119. break;
  120. }
  121. switch(rc) {
  122. case -1:
  123. /* select error */
  124. break;
  125. case 0: /* timeout */
  126. default: /* action */
  127. curl_multi_perform(mcurl, &still_running);
  128. break;
  129. }
  130. }
  131. test_cleanup:
  132. curl_slist_free_all(rcpt_list);
  133. curl_multi_remove_handle(mcurl, curl);
  134. curl_multi_cleanup(mcurl);
  135. curl_easy_cleanup(curl);
  136. curl_global_cleanup();
  137. return res;
  138. }