lib1507.c 4.3 KB

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