diff --git a/examples/http_client/http_client.cpp b/examples/http_client/http_client.cpp deleted file mode 100644 index 2dd0cb54..00000000 --- a/examples/http_client/http_client.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * http_client.cpp - * Author: Benjamin Sergeant - * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. - */ - -#include -#include -#include - -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 " << std::endl; - return 1; - } - std::string url = argv[1]; - - Socket::init(); - run(url); - return 0; -} diff --git a/ws/CMakeLists.txt b/ws/CMakeLists.txt index 60a0ee34..2a2b4ee2 100644 --- a/ws/CMakeLists.txt +++ b/ws/CMakeLists.txt @@ -23,6 +23,7 @@ add_executable(ws ixcrypto/IXHash.cpp ixcrypto/IXUuid.cpp + ws_http_client.cpp ws_ping_pong.cpp ws_broadcast_server.cpp ws_echo_server.cpp diff --git a/ws/ws.cpp b/ws/ws.cpp index 18168082..b4ab21c2 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -17,6 +17,8 @@ namespace ix { + int ws_http_client_main(const std::string& url); + int ws_ping_pong_main(const std::string& url); 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"); 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); ix::Socket::init(); @@ -110,6 +115,10 @@ int main(int argc, char** argv) { return ix::ws_ping_pong_main(url); } + else if (app.got_subcommand("http_client")) + { + return ix::ws_http_client_main(url); + } return 1; } diff --git a/ws/ws_http_client.cpp b/ws/ws_http_client.cpp new file mode 100644 index 00000000..8a7b14b9 --- /dev/null +++ b/ws/ws_http_client.cpp @@ -0,0 +1,36 @@ +/* + * http_client.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. + */ + +#include +#include +#include + +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; + } +}