quickjs-tart

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

lib1901.c (2566B)


      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 static size_t t1901_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
     29 {
     30   static const char *chunks[] = {
     31     "one",
     32     "two",
     33     "three",
     34     "four",
     35     NULL
     36   };
     37   static int ix = 0;
     38   (void)size;
     39   (void)nmemb;
     40   (void)stream;
     41   if(chunks[ix]) {
     42     size_t len = strlen(chunks[ix]);
     43     strcpy(ptr, chunks[ix]);
     44     ix++;
     45     return len;
     46   }
     47   return 0;
     48 }
     49 
     50 static CURLcode test_lib1901(char *URL)
     51 {
     52   CURL *curl;
     53   CURLcode res = CURLE_OK;
     54   struct curl_slist *chunk = NULL;
     55 
     56   curl_global_init(CURL_GLOBAL_ALL);
     57 
     58   curl = curl_easy_init();
     59   if(curl) {
     60     /* deliberately setting the size - to a wrong value to make sure libcurl
     61        ignores it */
     62     easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 4L);
     63     easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
     64     easy_setopt(curl, CURLOPT_READFUNCTION, t1901_read_cb);
     65     easy_setopt(curl, CURLOPT_POST, 1L);
     66     easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     67     easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     68     easy_setopt(curl, CURLOPT_URL, URL);
     69     easy_setopt(curl, CURLOPT_READDATA, NULL);
     70 
     71     chunk = curl_slist_append(chunk, "Expect:");
     72     if(chunk) {
     73       struct curl_slist *n =
     74         curl_slist_append(chunk, "Transfer-Encoding: chunked");
     75       if(n)
     76         chunk = n;
     77       if(n)
     78         easy_setopt(curl, CURLOPT_HTTPHEADER, n);
     79     }
     80 
     81     res = curl_easy_perform(curl);
     82   }
     83 test_cleanup:
     84   curl_easy_cleanup(curl);
     85   curl_slist_free_all(chunk);
     86 
     87   curl_global_cleanup();
     88   return res;
     89 }