quickjs-tart

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

lib552.c (6032B)


      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 /* argv1 = URL
     25  * argv2 = proxy with embedded user+password
     26  */
     27 
     28 #include "first.h"
     29 
     30 #include "memdebug.h"
     31 
     32 struct t552_testdata {
     33   char trace_ascii; /* 1 or 0 */
     34 };
     35 
     36 static void dump(const char *text,
     37                  FILE *stream, unsigned char *ptr, size_t size,
     38                  char nohex)
     39 {
     40   size_t i;
     41   size_t c;
     42 
     43   unsigned int width = 0x10;
     44 
     45   if(nohex)
     46     /* without the hex output, we can fit more on screen */
     47     width = 0x40;
     48 
     49   curl_mfprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size);
     50 
     51   for(i = 0; i < size; i += width) {
     52 
     53     curl_mfprintf(stream, "%04zx: ", i);
     54 
     55     if(!nohex) {
     56       /* hex not disabled, show it */
     57       for(c = 0; c < width; c++)
     58         if(i + c < size)
     59           curl_mfprintf(stream, "%02x ", ptr[i + c]);
     60         else
     61           fputs("   ", stream);
     62     }
     63 
     64     for(c = 0; (c < width) && (i + c < size); c++) {
     65       /* check for 0D0A; if found, skip past and start a new line of output */
     66       if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
     67          ptr[i + c + 1] == 0x0A) {
     68         i += (c + 2 - width);
     69         break;
     70       }
     71       curl_mfprintf(stream, "%c",
     72               (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
     73       /* check again for 0D0A, to avoid an extra \n if it's at width */
     74       if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
     75          ptr[i + c + 2] == 0x0A) {
     76         i += (c + 3 - width);
     77         break;
     78       }
     79     }
     80     fputc('\n', stream); /* newline */
     81   }
     82   fflush(stream);
     83 }
     84 
     85 static int my_trace(CURL *handle, curl_infotype type,
     86                     char *data, size_t size,
     87                     void *userp)
     88 {
     89   struct t552_testdata *config = (struct t552_testdata *)userp;
     90   const char *text;
     91   (void)handle; /* prevent compiler warning */
     92 
     93   switch(type) {
     94   case CURLINFO_TEXT:
     95     curl_mfprintf(stderr, "== Info: %s", (char *)data);
     96     return 0;
     97   case CURLINFO_HEADER_OUT:
     98     text = "=> Send header";
     99     break;
    100   case CURLINFO_DATA_OUT:
    101     text = "=> Send data";
    102     break;
    103   case CURLINFO_SSL_DATA_OUT:
    104     text = "=> Send SSL data";
    105     break;
    106   case CURLINFO_HEADER_IN:
    107     text = "<= Recv header";
    108     break;
    109   case CURLINFO_DATA_IN:
    110     text = "<= Recv data";
    111     break;
    112   case CURLINFO_SSL_DATA_IN:
    113     text = "<= Recv SSL data";
    114     break;
    115   default: /* in case a new one is introduced to shock us */
    116     return 0;
    117   }
    118 
    119   dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
    120   return 0;
    121 }
    122 
    123 static size_t current_offset = 0;
    124 static char databuf[70000]; /* MUST be more than 64k OR
    125                                MAX_INITIAL_POST_SIZE */
    126 
    127 static size_t t552_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
    128 {
    129   size_t  amount = nmemb * size; /* Total bytes curl wants */
    130   size_t  available = sizeof(databuf) - current_offset; /* What we have to
    131                                                            give */
    132   size_t  given = amount < available ? amount : available; /* What is given */
    133   (void)stream;
    134   memcpy(ptr, databuf + current_offset, given);
    135   current_offset += given;
    136   return given;
    137 }
    138 
    139 static size_t t552_write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
    140 {
    141   int amount = curlx_uztosi(size * nmemb);
    142   curl_mprintf("%.*s", amount, (char *)ptr);
    143   (void)stream;
    144   return size * nmemb;
    145 }
    146 
    147 static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
    148 {
    149   (void)clientp;
    150   if(cmd == CURLIOCMD_RESTARTREAD) {
    151     curl_mprintf("APPLICATION received a CURLIOCMD_RESTARTREAD request\n");
    152     curl_mprintf("APPLICATION ** REWINDING! **\n");
    153     current_offset = 0;
    154     return CURLIOE_OK;
    155   }
    156   (void)handle;
    157   return CURLIOE_UNKNOWNCMD;
    158 }
    159 
    160 static CURLcode test_lib552(char *URL)
    161 {
    162   CURL *curl;
    163   CURLcode res = CURLE_OK;
    164   struct t552_testdata config;
    165   size_t i;
    166   static const char fill[] = "test data";
    167 
    168   config.trace_ascii = 1; /* enable ASCII tracing */
    169 
    170   global_init(CURL_GLOBAL_ALL);
    171   easy_init(curl);
    172 
    173   test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
    174   test_setopt(curl, CURLOPT_DEBUGDATA, &config);
    175   /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
    176   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    177 
    178   /* setup repeated data string */
    179   for(i = 0; i < sizeof(databuf); ++i)
    180     databuf[i] = fill[i % sizeof(fill)];
    181 
    182   /* Post */
    183   test_setopt(curl, CURLOPT_POST, 1L);
    184 
    185   /* Setup read callback */
    186   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
    187   test_setopt(curl, CURLOPT_READFUNCTION, t552_read_cb);
    188 
    189   /* Write callback */
    190   test_setopt(curl, CURLOPT_WRITEFUNCTION, t552_write_cb);
    191 
    192   /* Ioctl function */
    193   test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
    194 
    195   test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
    196 
    197   test_setopt(curl, CURLOPT_URL, URL);
    198 
    199   /* Accept any auth. But for this bug configure proxy with DIGEST, basic
    200      might work too, not NTLM */
    201   test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
    202 
    203   res = curl_easy_perform(curl);
    204 
    205 test_cleanup:
    206 
    207   curl_easy_cleanup(curl);
    208   curl_global_cleanup();
    209   return res;
    210 }