quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib540.c (5779B)


      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 /* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
     25  * mailing list on 10 Jul 2007, converted to a test case.
     26  *
     27  * argv1 = URL
     28  * argv2 = proxy
     29  * argv3 = proxyuser:password
     30  * argv4 = host name to use for the custom Host: header
     31  */
     32 
     33 #include "first.h"
     34 
     35 #include "memdebug.h"
     36 
     37 #define PROXY libtest_arg2
     38 #define PROXYUSERPWD libtest_arg3
     39 #define HOST test_argv[4]
     40 
     41 static CURL *testeh[2];
     42 
     43 static CURLcode init(int num, CURLM *cm, const char *url, const char *userpwd,
     44                      struct curl_slist *headers)
     45 {
     46   CURLcode res = CURLE_OK;
     47 
     48   res_easy_init(testeh[num]);
     49   if(res)
     50     goto init_failed;
     51 
     52   res_easy_setopt(testeh[num], CURLOPT_URL, url);
     53   if(res)
     54     goto init_failed;
     55 
     56   res_easy_setopt(testeh[num], CURLOPT_PROXY, PROXY);
     57   if(res)
     58     goto init_failed;
     59 
     60   res_easy_setopt(testeh[num], CURLOPT_PROXYUSERPWD, userpwd);
     61   if(res)
     62     goto init_failed;
     63 
     64   res_easy_setopt(testeh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
     65   if(res)
     66     goto init_failed;
     67 
     68   res_easy_setopt(testeh[num], CURLOPT_VERBOSE, 1L);
     69   if(res)
     70     goto init_failed;
     71 
     72   res_easy_setopt(testeh[num], CURLOPT_HEADER, 1L);
     73   if(res)
     74     goto init_failed;
     75 
     76   res_easy_setopt(testeh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
     77   if(res)
     78     goto init_failed;
     79 
     80   res_multi_add_handle(cm, testeh[num]);
     81   if(res)
     82     goto init_failed;
     83 
     84   return CURLE_OK; /* success */
     85 
     86 init_failed:
     87 
     88   curl_easy_cleanup(testeh[num]);
     89   testeh[num] = NULL;
     90 
     91   return res; /* failure */
     92 }
     93 
     94 static CURLcode loop(int num, CURLM *cm, const char *url, const char *userpwd,
     95                      struct curl_slist *headers)
     96 {
     97   CURLMsg *msg;
     98   long L;
     99   int Q, U = -1;
    100   fd_set R, W, E;
    101   struct timeval T;
    102   CURLcode res = CURLE_OK;
    103 
    104   res = init(num, cm, url, userpwd, headers);
    105   if(res)
    106     return res;
    107 
    108   while(U) {
    109 
    110     int M = -99;
    111 
    112     res_multi_perform(cm, &U);
    113     if(res)
    114       return res;
    115 
    116     res_test_timedout();
    117     if(res)
    118       return res;
    119 
    120     if(U) {
    121       FD_ZERO(&R);
    122       FD_ZERO(&W);
    123       FD_ZERO(&E);
    124 
    125       res_multi_fdset(cm, &R, &W, &E, &M);
    126       if(res)
    127         return res;
    128 
    129       /* At this point, M is guaranteed to be greater or equal than -1. */
    130 
    131       res_multi_timeout(cm, &L);
    132       if(res)
    133         return res;
    134 
    135       /* At this point, L is guaranteed to be greater or equal than -1. */
    136 
    137       if(L != -1) {
    138         int itimeout;
    139 #if LONG_MAX > INT_MAX
    140         itimeout = (L > INT_MAX) ? INT_MAX : (int)L;
    141 #else
    142         itimeout = (int)L;
    143 #endif
    144         T.tv_sec = itimeout/1000;
    145         T.tv_usec = (itimeout%1000)*1000;
    146       }
    147       else {
    148         T.tv_sec = 5;
    149         T.tv_usec = 0;
    150       }
    151 
    152       res_select_test(M + 1, &R, &W, &E, &T);
    153       if(res)
    154         return res;
    155     }
    156 
    157     while(1) {
    158       msg = curl_multi_info_read(cm, &Q);
    159       if(!msg)
    160         break;
    161       if(msg->msg == CURLMSG_DONE) {
    162         size_t i;
    163         CURL *e = msg->easy_handle;
    164         curl_mfprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
    165                       curl_easy_strerror(msg->data.result));
    166         curl_multi_remove_handle(cm, e);
    167         curl_easy_cleanup(e);
    168         for(i = 0; i < CURL_ARRAYSIZE(testeh); i++) {
    169           if(testeh[i] == e) {
    170             testeh[i] = NULL;
    171             break;
    172           }
    173         }
    174       }
    175       else
    176         curl_mfprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
    177     }
    178 
    179     res_test_timedout();
    180     if(res)
    181       return res;
    182   }
    183 
    184   return CURLE_OK;
    185 }
    186 
    187 static CURLcode test_lib540(char *URL)
    188 {
    189   CURLM *cm = NULL;
    190   struct curl_slist *headers = NULL;
    191   char buffer[246]; /* naively fixed-size */
    192   CURLcode res = CURLE_OK;
    193   size_t i;
    194 
    195   for(i = 0; i < CURL_ARRAYSIZE(testeh); i++)
    196     testeh[i] = NULL;
    197 
    198   start_test_timing();
    199 
    200   if(test_argc < 4)
    201     return TEST_ERR_MAJOR_BAD;
    202 
    203   curl_msnprintf(buffer, sizeof(buffer), "Host: %s", HOST);
    204 
    205   /* now add a custom Host: header */
    206   headers = curl_slist_append(headers, buffer);
    207   if(!headers) {
    208     curl_mfprintf(stderr, "curl_slist_append() failed\n");
    209     return TEST_ERR_MAJOR_BAD;
    210   }
    211 
    212   res_global_init(CURL_GLOBAL_ALL);
    213   if(res) {
    214     curl_slist_free_all(headers);
    215     return res;
    216   }
    217 
    218   res_multi_init(cm);
    219   if(res) {
    220     curl_global_cleanup();
    221     curl_slist_free_all(headers);
    222     return res;
    223   }
    224 
    225   res = loop(0, cm, URL, PROXYUSERPWD, headers);
    226   if(res)
    227     goto test_cleanup;
    228 
    229   curl_mfprintf(stderr, "lib540: now we do the request again\n");
    230 
    231   res = loop(1, cm, URL, PROXYUSERPWD, headers);
    232 
    233 test_cleanup:
    234 
    235   /* proper cleanup sequence - type PB */
    236 
    237   for(i = 0; i < CURL_ARRAYSIZE(testeh); i++) {
    238     curl_multi_remove_handle(cm, testeh[i]);
    239     curl_easy_cleanup(testeh[i]);
    240   }
    241 
    242   curl_multi_cleanup(cm);
    243   curl_global_cleanup();
    244 
    245   curl_slist_free_all(headers);
    246 
    247   return res;
    248 }