quickjs-tart

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

lib547.c (3571B)


      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
     26  * argv3 = proxyuser:password
     27  */
     28 
     29 #include "first.h"
     30 
     31 #include "memdebug.h"
     32 
     33 #define UPLOADTHIS "this is the blurb we want to upload\n"
     34 
     35 static size_t t547_read_cb(char *ptr, size_t size, size_t nmemb, void *clientp)
     36 {
     37   int *counter = (int *)clientp;
     38 
     39   if(*counter) {
     40     /* only do this once and then require a clearing of this */
     41     curl_mfprintf(stderr, "READ ALREADY DONE!\n");
     42     return 0;
     43   }
     44   (*counter)++; /* bump */
     45 
     46   if(size * nmemb >= strlen(UPLOADTHIS)) {
     47     curl_mfprintf(stderr, "READ!\n");
     48     strcpy(ptr, UPLOADTHIS);
     49     return strlen(UPLOADTHIS);
     50   }
     51   curl_mfprintf(stderr, "READ NOT FINE!\n");
     52   return 0;
     53 }
     54 
     55 static curlioerr t547_ioctl_callback(CURL *handle, int cmd, void *clientp)
     56 {
     57   int *counter = (int *)clientp;
     58   (void)handle; /* unused */
     59   if(cmd == CURLIOCMD_RESTARTREAD) {
     60     curl_mfprintf(stderr, "REWIND!\n");
     61     *counter = 0; /* clear counter to make the read callback restart */
     62   }
     63   return CURLIOE_OK;
     64 }
     65 
     66 static CURLcode test_lib547(char *URL)
     67 {
     68   CURLcode res;
     69   CURL *curl;
     70   int counter = 0;
     71 
     72   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     73     curl_mfprintf(stderr, "curl_global_init() failed\n");
     74     return TEST_ERR_MAJOR_BAD;
     75   }
     76 
     77   curl = curl_easy_init();
     78   if(!curl) {
     79     curl_mfprintf(stderr, "curl_easy_init() failed\n");
     80     curl_global_cleanup();
     81     return TEST_ERR_MAJOR_BAD;
     82   }
     83 
     84   test_setopt(curl, CURLOPT_URL, URL);
     85   test_setopt(curl, CURLOPT_VERBOSE, 1L);
     86   test_setopt(curl, CURLOPT_HEADER, 1L);
     87   if(testnum == 548) {
     88     /* set the data to POST with a mere pointer to a null-terminated string */
     89     test_setopt(curl, CURLOPT_POSTFIELDS, UPLOADTHIS);
     90   }
     91   else {
     92     /* 547 style, which means reading the POST data from a callback */
     93     test_setopt(curl, CURLOPT_IOCTLFUNCTION, t547_ioctl_callback);
     94     test_setopt(curl, CURLOPT_IOCTLDATA, &counter);
     95 
     96     test_setopt(curl, CURLOPT_READFUNCTION, t547_read_cb);
     97     test_setopt(curl, CURLOPT_READDATA, &counter);
     98     /* We CANNOT do the POST fine without setting the size (or choose
     99        chunked)! */
    100     test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(UPLOADTHIS));
    101   }
    102   test_setopt(curl, CURLOPT_POST, 1L);
    103   test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
    104   test_setopt(curl, CURLOPT_PROXYUSERPWD, libtest_arg3);
    105   test_setopt(curl, CURLOPT_PROXYAUTH,
    106                    (long) (CURLAUTH_NTLM | CURLAUTH_DIGEST | CURLAUTH_BASIC) );
    107 
    108   res = curl_easy_perform(curl);
    109 
    110 test_cleanup:
    111 
    112   curl_easy_cleanup(curl);
    113   curl_global_cleanup();
    114 
    115   return res;
    116 }