move http_client to a ws sub-command

This commit is contained in:
Benjamin Sergeant 2019-02-23 09:52:19 -08:00
parent 846f0c680a
commit c04bc3cdfc
4 changed files with 46 additions and 50 deletions

View File

@ -1,50 +0,0 @@
/*
* http_client.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <ixwebsocket/IXHttpClient.h>
using namespace ix;
void run(const std::string& url)
{
HttpClient httpClient;
bool verbose = true;
auto out = httpClient.get(url, verbose);
auto errorCode = std::get<0>(out);
auto headers = std::get<1>(out);
auto payload = std::get<2>(out);
auto errorMsg = std::get<3>(out);
for (auto it : headers)
{
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "error code: " << errorCode << std::endl;
if (!errorMsg.empty())
{
std::cout << "error message: " << errorMsg << std::endl;
}
std::cout << "payload: " << payload << std::endl;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Usage: ws_connect <url>" << std::endl;
return 1;
}
std::string url = argv[1];
Socket::init();
run(url);
return 0;
}

View File

@ -23,6 +23,7 @@ add_executable(ws
ixcrypto/IXHash.cpp ixcrypto/IXHash.cpp
ixcrypto/IXUuid.cpp ixcrypto/IXUuid.cpp
ws_http_client.cpp
ws_ping_pong.cpp ws_ping_pong.cpp
ws_broadcast_server.cpp ws_broadcast_server.cpp
ws_echo_server.cpp ws_echo_server.cpp

View File

@ -17,6 +17,8 @@
namespace ix namespace ix
{ {
int ws_http_client_main(const std::string& url);
int ws_ping_pong_main(const std::string& url); int ws_ping_pong_main(const std::string& url);
int ws_echo_server_main(int port); int ws_echo_server_main(int port);
@ -73,6 +75,9 @@ int main(int argc, char** argv)
CLI::App* pingPongApp = app.add_subcommand("ping", "Ping pong"); CLI::App* pingPongApp = app.add_subcommand("ping", "Ping pong");
pingPongApp->add_option("url", url, "Connection url")->required(); pingPongApp->add_option("url", url, "Connection url")->required();
CLI::App* httpClientApp = app.add_subcommand("http_client", "HTTP Client");
httpClientApp->add_option("url", url, "Connection url")->required();
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
ix::Socket::init(); ix::Socket::init();
@ -110,6 +115,10 @@ int main(int argc, char** argv)
{ {
return ix::ws_ping_pong_main(url); return ix::ws_ping_pong_main(url);
} }
else if (app.got_subcommand("http_client"))
{
return ix::ws_http_client_main(url);
}
return 1; return 1;
} }

36
ws/ws_http_client.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* http_client.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <ixwebsocket/IXHttpClient.h>
namespace ix
{
void ws_http_client_main(const std::string& url)
{
HttpClient httpClient;
bool verbose = true;
auto out = httpClient.get(url, verbose);
auto errorCode = std::get<0>(out);
auto headers = std::get<1>(out);
auto payload = std::get<2>(out);
auto errorMsg = std::get<3>(out);
for (auto it : headers)
{
std::cout << it.first << ": " << it.second << std::endl;
}
std::cout << "error code: " << errorCode << std::endl;
if (!errorMsg.empty())
{
std::cout << "error message: " << errorMsg << std::endl;
}
std::cout << "payload: " << payload << std::endl;
}
}