quickjs-tart

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

lib1514.c (2556B)


      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 /*
     25  * Make sure libcurl does not send a `Content-Length: -1` header when HTTP POST
     26  * size is unknown.
     27  */
     28 
     29 #include "first.h"
     30 
     31 #include "memdebug.h"
     32 
     33 struct t1514_WriteThis {
     34   char *readptr;
     35   size_t sizeleft;
     36 };
     37 
     38 static size_t t1514_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
     39 {
     40   struct t1514_WriteThis *pooh = (struct t1514_WriteThis *)userp;
     41 
     42   if(size*nmemb < 1)
     43     return 0;
     44 
     45   if(pooh->sizeleft) {
     46     *ptr = pooh->readptr[0]; /* copy one single byte */
     47     pooh->readptr++;                 /* advance pointer */
     48     pooh->sizeleft--;                /* less data left */
     49     return 1;                        /* we return 1 byte at a time! */
     50   }
     51 
     52   return 0;                         /* no more data left to deliver */
     53 }
     54 
     55 static CURLcode test_lib1514(char *URL)
     56 {
     57   CURL *curl;
     58   CURLcode result = CURLE_OK;
     59   CURLcode res = CURLE_OK;
     60 
     61   static char testdata[] = "dummy";
     62 
     63   struct t1514_WriteThis pooh = { testdata, sizeof(testdata)-1 };
     64 
     65   global_init(CURL_GLOBAL_ALL);
     66 
     67   easy_init(curl);
     68 
     69   easy_setopt(curl, CURLOPT_URL, URL);
     70   easy_setopt(curl, CURLOPT_POST, 1L);
     71   /* Purposely omit to set CURLOPT_POSTFIELDSIZE */
     72   easy_setopt(curl, CURLOPT_READFUNCTION, t1514_read_cb);
     73   easy_setopt(curl, CURLOPT_READDATA, &pooh);
     74 
     75   if(testnum == 1539) {
     76     /* speak HTTP 1.0 - no chunked! */
     77     easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     78   }
     79 
     80   result = curl_easy_perform(curl);
     81 
     82 test_cleanup:
     83 
     84   curl_easy_cleanup(curl);
     85   curl_global_cleanup();
     86 
     87   return result;
     88 }