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