Path: blob/main/external/curl/tests/http/clients/ws-pingpong.c
2066 views
/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23/* <DESC>24* WebSockets pingpong25* </DESC>26*/27/* curl stuff */28#include "curl_setup.h"29#include <curl/curl.h>3031#include <stdio.h>32#include <stdlib.h>33#include <string.h>3435#ifdef _WIN3236#include <windows.h>37#else38#include <sys/time.h>39#endif4041#ifndef CURL_DISABLE_WEBSOCKETS4243static CURLcode ping(CURL *curl, const char *send_payload)44{45size_t sent;46CURLcode result =47curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,48CURLWS_PING);49fprintf(stderr,50"ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);5152return result;53}5455static CURLcode recv_pong(CURL *curl, const char *expected_payload)56{57size_t rlen;58const struct curl_ws_frame *meta;59char buffer[256];60CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);61if(result) {62fprintf(stderr, "ws: curl_ws_recv returned %u, received %ld\n",63(int)result, (long)rlen);64return result;65}6667if(!(meta->flags & CURLWS_PONG)) {68fprintf(stderr, "recv_pong: wrong frame, got %d bytes rflags %x\n",69(int)rlen, meta->flags);70return CURLE_RECV_ERROR;71}7273fprintf(stderr, "ws: got PONG back\n");74if(rlen == strlen(expected_payload) &&75!memcmp(expected_payload, buffer, rlen)) {76fprintf(stderr, "ws: got the same payload back\n");77return CURLE_OK;78}79fprintf(stderr, "ws: did NOT get the same payload back\n");80return CURLE_RECV_ERROR;81}8283/* just close the connection */84static void websocket_close(CURL *curl)85{86size_t sent;87CURLcode result =88curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);89fprintf(stderr,90"ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);91}9293#if defined(__TANDEM)94# include <cextdecs.h(PROCESS_DELAY_)>95#endif96static CURLcode pingpong(CURL *curl, const char *payload)97{98CURLcode res;99int i;100101res = ping(curl, payload);102if(res)103return res;104for(i = 0; i < 10; ++i) {105fprintf(stderr, "Receive pong\n");106res = recv_pong(curl, payload);107if(res == CURLE_AGAIN) {108#ifdef _WIN32109Sleep(100);110#elif defined(__TANDEM)111/* NonStop only defines usleep when building for a threading model */112# if defined(_PUT_MODEL_) || defined(_KLT_MODEL_)113usleep(100*1000);114# else115PROCESS_DELAY_(100*1000);116# endif117#else118usleep(100*1000);119#endif120continue;121}122websocket_close(curl);123return res;124}125websocket_close(curl);126return CURLE_RECV_ERROR;127}128129#endif130131int main(int argc, char *argv[])132{133#ifndef CURL_DISABLE_WEBSOCKETS134CURL *curl;135CURLcode res = CURLE_OK;136const char *url, *payload;137138if(argc != 3) {139fprintf(stderr, "usage: ws-pingpong url payload\n");140return 2;141}142url = argv[1];143payload = argv[2];144145curl_global_init(CURL_GLOBAL_ALL);146147curl = curl_easy_init();148if(curl) {149curl_easy_setopt(curl, CURLOPT_URL, url);150151/* use the callback style */152curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong");153curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);154curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */155res = curl_easy_perform(curl);156fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);157if(res == CURLE_OK)158res = pingpong(curl, payload);159160/* always cleanup */161curl_easy_cleanup(curl);162}163curl_global_cleanup();164return (int)res;165166#else /* !CURL_DISABLE_WEBSOCKETS */167(void)argc;168(void)argv;169fprintf(stderr, "WebSockets not enabled in libcurl\n");170return 1;171#endif /* CURL_DISABLE_WEBSOCKETS */172}173174175