quickjs-tart

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

lib2301.c (4503B)


      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 #ifndef CURL_DISABLE_WEBSOCKETS
     27 #if 0
     28 
     29 static CURLcode t2301_send_ping(CURL *curl, const char *send_payload)
     30 {
     31   size_t sent;
     32   CURLcode result =
     33     curl_ws_send(curl, send_payload, strlen(send_payload), &sent, CURLWS_PING);
     34   curl_mfprintf(stderr,
     35                 "ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
     36 
     37   return result;
     38 }
     39 
     40 static CURLcode t2301_recv_pong(CURL *curl, const char *expected_payload)
     41 {
     42   size_t rlen;
     43   unsigned int rflags;
     44   char buffer[256];
     45   CURLcode result =
     46     curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &rflags);
     47   if(rflags & CURLWS_PONG) {
     48     int same = 0;
     49     curl_mfprintf(stderr, "ws: got PONG back\n");
     50     if(rlen == strlen(expected_payload)) {
     51       if(!memcmp(expected_payload, buffer, rlen)) {
     52         curl_mfprintf(stderr, "ws: got the same payload back\n");
     53         same = 1;
     54       }
     55     }
     56     if(!same)
     57       curl_mfprintf(stderr, "ws: did NOT get the same payload back\n");
     58   }
     59   else {
     60     curl_mfprintf(stderr, "recv_pong: got %d bytes rflags %x\n",
     61                   (int)rlen, rflags);
     62   }
     63   curl_mfprintf(stderr, "ws: curl_ws_recv returned %d, received %d\n", result,
     64                 (int)rlen);
     65   return result;
     66 }
     67 
     68 /* just close the connection */
     69 static void t2301_websocket_close(CURL *curl)
     70 {
     71   size_t sent;
     72   CURLcode result =
     73     curl_ws_send(curl, "", 0, &sent, CURLWS_CLOSE);
     74   curl_mfprintf(stderr,
     75                 "ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
     76 }
     77 
     78 static void t2301_websocket(CURL *curl)
     79 {
     80   int i = 0;
     81   curl_mfprintf(stderr, "ws: websocket() starts\n");
     82   do {
     83     if(t2301_send_ping(curl, "foobar"))
     84       return;
     85     if(t2301_recv_pong(curl, "foobar"))
     86       return;
     87     curlx_wait_ms(2000);
     88   } while(i++ < 10);
     89   t2301_websocket_close(curl);
     90 }
     91 
     92 #endif
     93 
     94 static size_t t2301_write_cb(char *b, size_t size, size_t nitems, void *p)
     95 {
     96   CURL *easy = p;
     97   unsigned char *buffer = (unsigned char *)b;
     98   size_t i;
     99   size_t sent;
    100   unsigned char pong[] = {
    101     0x8a, 0x0
    102   };
    103   size_t incoming = nitems;
    104   curl_mfprintf(stderr, "Called CURLOPT_WRITEFUNCTION with %d bytes: ",
    105                 (int)nitems);
    106   for(i = 0; i < nitems; i++)
    107     curl_mfprintf(stderr, "%02x ", (unsigned char)buffer[i]);
    108   curl_mfprintf(stderr, "\n");
    109   (void)size;
    110   if(buffer[0] == 0x89) {
    111     CURLcode result;
    112     curl_mfprintf(stderr, "send back a simple PONG\n");
    113     result = curl_ws_send(easy, pong, 2, &sent, 0, 0);
    114     if(result)
    115       nitems = 0;
    116   }
    117   if(nitems != incoming)
    118     curl_mfprintf(stderr, "returns error from callback\n");
    119   return nitems;
    120 }
    121 #endif
    122 
    123 static CURLcode test_lib2301(char *URL)
    124 {
    125 #ifndef CURL_DISABLE_WEBSOCKETS
    126   CURL *curl;
    127   CURLcode res = CURLE_OK;
    128 
    129   global_init(CURL_GLOBAL_ALL);
    130 
    131   curl = curl_easy_init();
    132   if(curl) {
    133     curl_easy_setopt(curl, CURLOPT_URL, URL);
    134 
    135     /* use the callback style */
    136     curl_easy_setopt(curl, CURLOPT_USERAGENT, "webbie-sox/3");
    137     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    138     curl_easy_setopt(curl, CURLOPT_WS_OPTIONS, (long)CURLWS_RAW_MODE);
    139     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, t2301_write_cb);
    140     curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
    141     res = curl_easy_perform(curl);
    142     curl_mfprintf(stderr, "curl_easy_perform() returned %d\n", res);
    143 #if 0
    144     if(res == CURLE_OK)
    145       t2301_websocket(curl);
    146 #endif
    147     /* always cleanup */
    148     curl_easy_cleanup(curl);
    149   }
    150   curl_global_cleanup();
    151   return res;
    152 #else
    153   NO_SUPPORT_BUILT_IN
    154 #endif
    155 }