quickjs-tart

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

lib554.c (6327B)


      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 #include "first.h"
     25 
     26 #include "memdebug.h"
     27 
     28 struct t554_WriteThis {
     29   const char *readptr;
     30   size_t sizeleft;
     31 };
     32 
     33 static size_t t554_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
     34 {
     35   struct t554_WriteThis *pooh = (struct t554_WriteThis *)userp;
     36 
     37   if(size*nmemb < 1)
     38     return 0;
     39 
     40   if(pooh->sizeleft) {
     41     *ptr = pooh->readptr[0];  /* copy one single byte */
     42     pooh->readptr++;          /* advance pointer */
     43     pooh->sizeleft--;         /* less data left */
     44     return 1;                 /* we return 1 byte at a time! */
     45   }
     46 
     47   return 0;                   /* no more data left to deliver */
     48 }
     49 
     50 static size_t t587_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
     51 {
     52   (void)ptr;
     53   (void)size;
     54   (void)nmemb;
     55   (void)userp;
     56   return CURL_READFUNC_ABORT;
     57 }
     58 
     59 static CURLcode t554_test_once(char *URL, bool oldstyle)
     60 {
     61   static const char testdata[] =
     62     "this is what we post to the silly web server\n";
     63 
     64   CURL *curl;
     65   CURLcode res = CURLE_OK;
     66   CURLFORMcode formrc;
     67 
     68   struct curl_httppost *formpost = NULL;
     69   struct curl_httppost *lastptr = NULL;
     70   struct t554_WriteThis pooh;
     71   struct t554_WriteThis pooh2;
     72 
     73   pooh.readptr = testdata;
     74   pooh.sizeleft = strlen(testdata);
     75 
     76   /* Fill in the file upload field */
     77   if(oldstyle) {
     78     formrc = curl_formadd(&formpost,
     79                           &lastptr,
     80                           CURLFORM_COPYNAME, "sendfile",
     81                           CURLFORM_STREAM, &pooh,
     82                           CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
     83                           CURLFORM_FILENAME, "postit2.c",
     84                           CURLFORM_END);
     85   }
     86   else {
     87     /* new style */
     88     formrc = curl_formadd(&formpost,
     89                           &lastptr,
     90                           CURLFORM_COPYNAME, "sendfile alternative",
     91                           CURLFORM_STREAM, &pooh,
     92                           CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
     93                           CURLFORM_FILENAME, "file name 2",
     94                           CURLFORM_END);
     95   }
     96 
     97   if(formrc)
     98     curl_mprintf("curl_formadd(1) = %d\n", (int)formrc);
     99 
    100   /* Now add the same data with another name and make it not look like
    101      a file upload but still using the callback */
    102 
    103   pooh2.readptr = testdata;
    104   pooh2.sizeleft = strlen(testdata);
    105 
    106   /* Fill in the file upload field */
    107   formrc = curl_formadd(&formpost,
    108                         &lastptr,
    109                         CURLFORM_COPYNAME, "callbackdata",
    110                         CURLFORM_STREAM, &pooh2,
    111                         CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
    112                         CURLFORM_END);
    113 
    114   if(formrc)
    115     curl_mprintf("curl_formadd(2) = %d\n", (int)formrc);
    116 
    117   /* Fill in the filename field */
    118   formrc = curl_formadd(&formpost,
    119                         &lastptr,
    120                         CURLFORM_COPYNAME, "filename",
    121                         CURLFORM_COPYCONTENTS, "postit2.c",
    122                         CURLFORM_END);
    123   if(formrc)
    124     curl_mprintf("curl_formadd(3) = %d\n", (int)formrc);
    125 
    126   /* Fill in a submit field too */
    127   formrc = curl_formadd(&formpost,
    128                         &lastptr,
    129                         CURLFORM_COPYNAME, "submit",
    130                         CURLFORM_COPYCONTENTS, "send",
    131                         CURLFORM_CONTENTTYPE, "text/plain",
    132                         CURLFORM_END);
    133 
    134   if(formrc)
    135     curl_mprintf("curl_formadd(4) = %d\n", (int)formrc);
    136 
    137   formrc = curl_formadd(&formpost, &lastptr,
    138                         CURLFORM_COPYNAME, "somename",
    139                         CURLFORM_BUFFER, "somefile.txt",
    140                         CURLFORM_BUFFERPTR, "blah blah",
    141                         CURLFORM_BUFFERLENGTH, (long)9,
    142                         CURLFORM_END);
    143 
    144   if(formrc)
    145     curl_mprintf("curl_formadd(5) = %d\n", (int)formrc);
    146 
    147   curl = curl_easy_init();
    148   if(!curl) {
    149     curl_mfprintf(stderr, "curl_easy_init() failed\n");
    150     curl_formfree(formpost);
    151     curl_global_cleanup();
    152     return TEST_ERR_MAJOR_BAD;
    153   }
    154 
    155   /* First set the URL that is about to receive our POST. */
    156   test_setopt(curl, CURLOPT_URL, URL);
    157 
    158   /* Now specify we want to POST data */
    159   test_setopt(curl, CURLOPT_POST, 1L);
    160 
    161   /* Set the expected POST size */
    162   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
    163 
    164   /* we want to use our own read function */
    165   if(testnum == 587) {
    166     test_setopt(curl, CURLOPT_READFUNCTION, t587_read_cb);
    167   }
    168   else {
    169     test_setopt(curl, CURLOPT_READFUNCTION, t554_read_cb);
    170   }
    171 
    172   /* send a multi-part formpost */
    173   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
    174 
    175   /* get verbose debug output please */
    176   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    177 
    178   /* include headers in the output */
    179   test_setopt(curl, CURLOPT_HEADER, 1L);
    180 
    181   /* Perform the request, res will get the return code */
    182   res = curl_easy_perform(curl);
    183 
    184 test_cleanup:
    185 
    186   /* always cleanup */
    187   curl_easy_cleanup(curl);
    188 
    189   /* now cleanup the formpost chain */
    190   curl_formfree(formpost);
    191 
    192   return res;
    193 }
    194 
    195 static CURLcode test_lib554(char *URL)
    196 {
    197   CURLcode res;
    198 
    199   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    200     curl_mfprintf(stderr, "curl_global_init() failed\n");
    201     return TEST_ERR_MAJOR_BAD;
    202   }
    203 
    204   res = t554_test_once(URL, TRUE); /* old */
    205   if(!res)
    206     res = t554_test_once(URL, FALSE); /* new */
    207 
    208   curl_global_cleanup();
    209 
    210   return res;
    211 }