quickjs-tart

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

lib556.c (3069B)


      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 CURLcode test_lib556(char *URL)
     29 {
     30   CURLcode res;
     31   CURL *curl;
     32   int transfers = 0;
     33 
     34   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     35     curl_mfprintf(stderr, "curl_global_init() failed\n");
     36     return TEST_ERR_MAJOR_BAD;
     37   }
     38 
     39   curl = curl_easy_init();
     40   if(!curl) {
     41     curl_mfprintf(stderr, "curl_easy_init() failed\n");
     42     curl_global_cleanup();
     43     return TEST_ERR_MAJOR_BAD;
     44   }
     45 
     46   test_setopt(curl, CURLOPT_URL, URL);
     47   test_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
     48   test_setopt(curl, CURLOPT_VERBOSE, 1L);
     49 
     50 again:
     51 
     52   res = curl_easy_perform(curl);
     53 
     54   if(!res) {
     55     /* we are connected, now get an HTTP document the raw way */
     56     static const char *request =
     57       "GET /556 HTTP/1.1\r\n"
     58       "Host: ninja\r\n\r\n";
     59     const char *sbuf = request;
     60     size_t sblen = strlen(request);
     61     size_t nwritten = 0, nread = 0;
     62 
     63     do {
     64       char buf[1024];
     65 
     66       if(sblen) {
     67         res = curl_easy_send(curl, sbuf, sblen, &nwritten);
     68         if(res && res != CURLE_AGAIN)
     69           break;
     70         if(nwritten > 0) {
     71           sbuf += nwritten;
     72           sblen -= nwritten;
     73         }
     74       }
     75 
     76       /* busy-read like crazy */
     77       res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
     78 
     79       if(nread) {
     80         /* send received stuff to stdout */
     81 #ifdef UNDER_CE
     82         if((size_t)fwrite(buf, sizeof(buf[0]), nread, stdout) != nread) {
     83 #else
     84         if((size_t)write(STDOUT_FILENO, buf, nread) != nread) {
     85 #endif
     86           curl_mfprintf(stderr, "write() failed: errno %d (%s)\n",
     87                         errno, strerror(errno));
     88           res = TEST_ERR_FAILURE;
     89           break;
     90         }
     91       }
     92 
     93     } while((res == CURLE_OK && nread) || (res == CURLE_AGAIN));
     94 
     95     if(res && res != CURLE_AGAIN)
     96       res = TEST_ERR_FAILURE;
     97   }
     98 
     99   if(testnum == 696) {
    100     ++transfers;
    101     /* perform the transfer a second time */
    102     if(!res && transfers == 1)
    103       goto again;
    104   }
    105 
    106 test_cleanup:
    107 
    108   curl_easy_cleanup(curl);
    109   curl_global_cleanup();
    110 
    111   return res;
    112 }