lib1507.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "test.h"
00023 
00024 #include "testutil.h"
00025 #include "warnless.h"
00026 #include "memdebug.h"
00027 
00028 /*
00029  * This is the list of basic details you need to tweak to get things right.
00030  */
00031 #define USERNAME "user@example.com"
00032 #define PASSWORD "123qwerty"
00033 #define RECIPIENT "<1507-recipient@example.com>"
00034 #define MAILFROM "<1507-realuser@example.com>"
00035 
00036 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
00037 
00038 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00039 {
00040   (void)ptr;
00041   (void)size;
00042   (void)nmemb;
00043   (void)userp;
00044   return CURL_READFUNC_ABORT;
00045 }
00046 
00047 static struct timeval tvnow(void)
00048 {
00049   /*
00050   ** time() returns the value of time in seconds since the Epoch.
00051   */
00052   struct timeval now;
00053   now.tv_sec = (long)time(NULL);
00054   now.tv_usec = 0;
00055   return now;
00056 }
00057 
00058 static long tvdiff(struct timeval newer, struct timeval older)
00059 {
00060   return (newer.tv_sec-older.tv_sec)*1000+
00061     (newer.tv_usec-older.tv_usec)/1000;
00062 }
00063 
00064 int test(char *URL)
00065 {
00066    int res = 0;
00067    CURL *curl = NULL;
00068    CURLM *mcurl = NULL;
00069    int still_running = 1;
00070    struct timeval mp_start;
00071    struct curl_slist *rcpt_list = NULL;
00072 
00073    curl_global_init(CURL_GLOBAL_DEFAULT);
00074 
00075    easy_init(curl);
00076 
00077    multi_init(mcurl);
00078 
00079    rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
00080    /* more addresses can be added here
00081       rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
00082    */
00083 
00084    curl_easy_setopt(curl, CURLOPT_URL, URL);
00085 #if 0
00086    curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
00087    curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
00088 #endif
00089    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00090    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00091    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
00092    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
00093    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00094    multi_add_handle(mcurl, curl);
00095 
00096    mp_start = tvnow();
00097 
00098   /* we start some action by calling perform right away */
00099   curl_multi_perform(mcurl, &still_running);
00100 
00101   while(still_running) {
00102     struct timeval timeout;
00103     int rc; /* select() return code */
00104 
00105     fd_set fdread;
00106     fd_set fdwrite;
00107     fd_set fdexcep;
00108     int maxfd = -1;
00109 
00110     long curl_timeo = -1;
00111 
00112     FD_ZERO(&fdread);
00113     FD_ZERO(&fdwrite);
00114     FD_ZERO(&fdexcep);
00115 
00116     /* set a suitable timeout to play around with */
00117     timeout.tv_sec = 1;
00118     timeout.tv_usec = 0;
00119 
00120     curl_multi_timeout(mcurl, &curl_timeo);
00121     if(curl_timeo >= 0) {
00122       timeout.tv_sec = curl_timeo / 1000;
00123       if(timeout.tv_sec > 1)
00124         timeout.tv_sec = 1;
00125       else
00126         timeout.tv_usec = (curl_timeo % 1000) * 1000;
00127     }
00128 
00129     /* get file descriptors from the transfers */
00130     curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
00131 
00132     /* In a real-world program you OF COURSE check the return code of the
00133        function calls.  On success, the value of maxfd is guaranteed to be
00134        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
00135        case of (maxfd == -1), we call select(0, ...), which is basically equal
00136        to sleep. */
00137 
00138     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00139 
00140     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
00141       fprintf(stderr, "ABORTING TEST, since it seems "
00142               "that it would have run forever.\n");
00143       break;
00144     }
00145 
00146     switch(rc) {
00147     case -1:
00148       /* select error */
00149       break;
00150     case 0: /* timeout */
00151     default: /* action */
00152       curl_multi_perform(mcurl, &still_running);
00153       break;
00154     }
00155   }
00156 
00157 test_cleanup:
00158 
00159   curl_slist_free_all(rcpt_list);
00160   curl_multi_remove_handle(mcurl, curl);
00161   curl_multi_cleanup(mcurl);
00162   curl_easy_cleanup(curl);
00163   curl_global_cleanup();
00164 
00165   return res;
00166 }
00167 
00168 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05