• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
read_callback(void * ptr,size_t size,size_t nmemb,void * userp)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 
tvnow(void)47 static struct timeval tvnow(void)
48 {
49   /*
50   ** time() returns the value of time in seconds since the Epoch.
51   */
52   struct timeval now;
53   now.tv_sec = (long)time(NULL);
54   now.tv_usec = 0;
55   return now;
56 }
57 
tvdiff(struct timeval newer,struct timeval older)58 static long tvdiff(struct timeval newer, struct timeval older)
59 {
60   return (newer.tv_sec-older.tv_sec)*1000+
61     (newer.tv_usec-older.tv_usec)/1000;
62 }
63 
test(char * URL)64 int test(char *URL)
65 {
66    int res = 0;
67    CURL *curl = NULL;
68    CURLM *mcurl = NULL;
69    int still_running = 1;
70    struct timeval mp_start;
71    struct curl_slist* rcpt_list = NULL;
72 
73    curl_global_init(CURL_GLOBAL_DEFAULT);
74 
75    easy_init(curl);
76 
77    multi_init(mcurl);
78 
79    rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
80    /* more addresses can be added here
81       rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
82    */
83 
84    curl_easy_setopt(curl, CURLOPT_URL, URL);
85 #if 0
86    curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
87    curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
88 #endif
89    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
90    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
91    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
92    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
93    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
94    multi_add_handle(mcurl, curl);
95 
96    mp_start = tvnow();
97 
98   /* we start some action by calling perform right away */
99   curl_multi_perform(mcurl, &still_running);
100 
101   while(still_running) {
102     struct timeval timeout;
103     int rc; /* select() return code */
104 
105     fd_set fdread;
106     fd_set fdwrite;
107     fd_set fdexcep;
108     int maxfd = -1;
109 
110     long curl_timeo = -1;
111 
112     FD_ZERO(&fdread);
113     FD_ZERO(&fdwrite);
114     FD_ZERO(&fdexcep);
115 
116     /* set a suitable timeout to play around with */
117     timeout.tv_sec = 1;
118     timeout.tv_usec = 0;
119 
120     curl_multi_timeout(mcurl, &curl_timeo);
121     if(curl_timeo >= 0) {
122       timeout.tv_sec = curl_timeo / 1000;
123       if(timeout.tv_sec > 1)
124         timeout.tv_sec = 1;
125       else
126         timeout.tv_usec = (curl_timeo % 1000) * 1000;
127     }
128 
129     /* get file descriptors from the transfers */
130     curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
131 
132     /* In a real-world program you OF COURSE check the return code of the
133        function calls.  On success, the value of maxfd is guaranteed to be
134        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
135        case of (maxfd == -1), we call select(0, ...), which is basically equal
136        to sleep. */
137 
138     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
139 
140     if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
141       fprintf(stderr, "ABORTING TEST, since it seems "
142               "that it would have run forever.\n");
143       break;
144     }
145 
146     switch(rc) {
147     case -1:
148       /* select error */
149       break;
150     case 0: /* timeout */
151     default: /* action */
152       curl_multi_perform(mcurl, &still_running);
153       break;
154     }
155   }
156 
157 test_cleanup:
158 
159   curl_slist_free_all(rcpt_list);
160   curl_multi_remove_handle(mcurl, curl);
161   curl_multi_cleanup(mcurl);
162   curl_easy_cleanup(curl);
163   curl_global_cleanup();
164 
165   return res;
166 }
167 
168 
169