quickjs-tart

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

lib1948.c (2416B)


      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 typedef struct
     27 {
     28   char *buf;
     29   size_t len;
     30 } put_buffer;
     31 
     32 static size_t put_callback(char *ptr, size_t size, size_t nmemb, void *stream)
     33 {
     34   put_buffer *putdata = (put_buffer *)stream;
     35   size_t totalsize = size * nmemb;
     36   size_t tocopy = (putdata->len < totalsize) ? putdata->len : totalsize;
     37   memcpy(ptr, putdata->buf, tocopy);
     38   putdata->len -= tocopy;
     39   putdata->buf += tocopy;
     40   return tocopy;
     41 }
     42 
     43 static CURLcode test_lib1948(char *URL)
     44 {
     45   CURL *curl;
     46   CURLcode res = CURLE_OK;
     47   static const char *testput = "This is test PUT data\n";
     48   put_buffer pbuf;
     49 
     50   curl_global_init(CURL_GLOBAL_DEFAULT);
     51 
     52   easy_init(curl);
     53 
     54   /* PUT */
     55   easy_setopt(curl, CURLOPT_UPLOAD, 1L);
     56   easy_setopt(curl, CURLOPT_HEADER, 1L);
     57   easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
     58   pbuf.buf = (char *)CURL_UNCONST(testput);
     59   pbuf.len = strlen(testput);
     60   easy_setopt(curl, CURLOPT_READDATA, &pbuf);
     61   easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));
     62   easy_setopt(curl, CURLOPT_URL, URL);
     63   res = curl_easy_perform(curl);
     64   if(res)
     65     goto test_cleanup;
     66 
     67   /* POST */
     68   easy_setopt(curl, CURLOPT_POST, 1L);
     69   easy_setopt(curl, CURLOPT_POSTFIELDS, testput);
     70   easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(testput));
     71   res = curl_easy_perform(curl);
     72 
     73 test_cleanup:
     74   curl_easy_cleanup(curl);
     75   curl_global_cleanup();
     76   return res;
     77 }