lib1507.c 4.2 KB

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