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