quickjs-tart

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

lib2304.c (4179B)


      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 
     28 static CURLcode t2304_send_ping(CURL *curl, const char *send_payload)
     29 {
     30   size_t sent;
     31   CURLcode result =
     32     curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
     33                  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 t2304_recv_pong(CURL *curl, const char *expected_payload)
     41 {
     42   size_t rlen;
     43   const struct curl_ws_frame *meta;
     44   char buffer[256];
     45   CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
     46   if(!result) {
     47     if(meta->flags & 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", (int)rlen,
     61                     meta->flags);
     62     }
     63   }
     64   curl_mfprintf(stderr, "ws: curl_ws_recv returned %d, received %d\n", result,
     65                 (int)rlen);
     66   return result;
     67 }
     68 
     69 static CURLcode recv_any(CURL *curl)
     70 {
     71   size_t rlen;
     72   const struct curl_ws_frame *meta;
     73   char buffer[256];
     74   CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
     75   if(result)
     76     return result;
     77 
     78   curl_mfprintf(stderr, "recv_any: got %u bytes rflags %x\n", (int)rlen,
     79                 meta->flags);
     80   return CURLE_OK;
     81 }
     82 
     83 /* just close the connection */
     84 static void t2304_websocket_close(CURL *curl)
     85 {
     86   size_t sent;
     87   CURLcode result =
     88     curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
     89   curl_mfprintf(stderr,
     90                 "ws: curl_ws_send returned %d, sent %u\n", result, (int)sent);
     91 }
     92 
     93 static void t2304_websocket(CURL *curl)
     94 {
     95   int i = 0;
     96   curl_mfprintf(stderr, "ws: websocket() starts\n");
     97   do {
     98     recv_any(curl);
     99     curl_mfprintf(stderr, "Send ping\n");
    100     if(t2304_send_ping(curl, "foobar"))
    101       return;
    102     curl_mfprintf(stderr, "Receive pong\n");
    103     if(t2304_recv_pong(curl, "foobar")) {
    104       curl_mprintf("Connection closed\n");
    105       return;
    106     }
    107     curlx_wait_ms(2000);
    108   } while(i++ < 10);
    109   t2304_websocket_close(curl);
    110 }
    111 #endif
    112 
    113 static CURLcode test_lib2304(char *URL)
    114 {
    115 #ifndef CURL_DISABLE_WEBSOCKETS
    116   CURL *curl;
    117   CURLcode res = CURLE_OK;
    118 
    119   global_init(CURL_GLOBAL_ALL);
    120 
    121   curl = curl_easy_init();
    122   if(curl) {
    123     curl_easy_setopt(curl, CURLOPT_URL, URL);
    124 
    125     /* use the callback style */
    126     curl_easy_setopt(curl, CURLOPT_USERAGENT, "websocket/2304");
    127     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    128     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
    129     res = curl_easy_perform(curl);
    130     curl_mfprintf(stderr, "curl_easy_perform() returned %d\n", res);
    131     if(res == CURLE_OK)
    132       t2304_websocket(curl);
    133 
    134     /* always cleanup */
    135     curl_easy_cleanup(curl);
    136   }
    137   curl_global_cleanup();
    138   return res;
    139 #else
    140   NO_SUPPORT_BUILT_IN
    141 #endif
    142 }