lib1507.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. #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. int test(char *URL)
  43. {
  44. int res = 0;
  45. CURL *curl = NULL;
  46. CURLM *mcurl = NULL;
  47. int still_running = 1;
  48. struct timeval mp_start;
  49. struct curl_slist *rcpt_list = NULL;
  50. curl_global_init(CURL_GLOBAL_DEFAULT);
  51. easy_init(curl);
  52. multi_init(mcurl);
  53. rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
  54. /* more addresses can be added here
  55. rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
  56. */
  57. curl_easy_setopt(curl, CURLOPT_URL, URL);
  58. #if 0
  59. curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
  60. curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
  61. #endif
  62. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  63. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  64. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
  65. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
  66. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  67. multi_add_handle(mcurl, curl);
  68. mp_start = tutil_tvnow();
  69. /* we start some action by calling perform right away */
  70. curl_multi_perform(mcurl, &still_running);
  71. while(still_running) {
  72. struct timeval timeout;
  73. int rc; /* select() return code */
  74. fd_set fdread;
  75. fd_set fdwrite;
  76. fd_set fdexcep;
  77. int maxfd = -1;
  78. long curl_timeo = -1;
  79. FD_ZERO(&fdread);
  80. FD_ZERO(&fdwrite);
  81. FD_ZERO(&fdexcep);
  82. /* set a suitable timeout to play around with */
  83. timeout.tv_sec = 1;
  84. timeout.tv_usec = 0;
  85. curl_multi_timeout(mcurl, &curl_timeo);
  86. if(curl_timeo >= 0) {
  87. timeout.tv_sec = curl_timeo / 1000;
  88. if(timeout.tv_sec > 1)
  89. timeout.tv_sec = 1;
  90. else
  91. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  92. }
  93. /* get file descriptors from the transfers */
  94. curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  95. /* In a real-world program you OF COURSE check the return code of the
  96. function calls. On success, the value of maxfd is guaranteed to be
  97. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  98. case of (maxfd == -1), we call select(0, ...), which is basically equal
  99. to sleep. */
  100. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  101. if(tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  102. fprintf(stderr, "ABORTING TEST, since it seems "
  103. "that it would have run forever.\n");
  104. break;
  105. }
  106. switch(rc) {
  107. case -1:
  108. /* select error */
  109. break;
  110. case 0: /* timeout */
  111. default: /* action */
  112. curl_multi_perform(mcurl, &still_running);
  113. break;
  114. }
  115. }
  116. test_cleanup:
  117. curl_slist_free_all(rcpt_list);
  118. curl_multi_remove_handle(mcurl, curl);
  119. curl_multi_cleanup(mcurl);
  120. curl_easy_cleanup(curl);
  121. curl_global_cleanup();
  122. return res;
  123. }