From f4facffae32f98ea2e8f53aa23d1e8a4df89986f Mon Sep 17 00:00:00 2001 From: Sorin Fetche Date: Wed, 25 Oct 2017 08:24:47 -0700 Subject: [PATCH] Version command line option for HTTP client examples: The examples HTTP clients allow an optional command line switch to choose the HTTP-version used ("1.0" or "1.1"). --- CHANGELOG.md | 1 + .../client/async-ssl/http_client_async_ssl.cpp | 15 +++++++++------ example/http/client/async/http_client_async.cpp | 15 +++++++++------ .../http/client/coro-ssl/http_client_coro_ssl.cpp | 12 ++++++++---- example/http/client/coro/http_client_coro.cpp | 12 ++++++++---- .../http/client/sync-ssl/http_client_sync_ssl.cpp | 10 ++++++---- example/http/client/sync/http_client_sync.cpp | 10 ++++++---- 7 files changed, 47 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a32194b6..c548aac6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 127: * Add BOOST_BEAST_NO_POSIX_FADVISE +* Version command line option for HTTP client examples -------------------------------------------------------------------------------- diff --git a/example/http/client/async-ssl/http_client_async_ssl.cpp b/example/http/client/async-ssl/http_client_async_ssl.cpp index 71a55cda..fc5791ee 100644 --- a/example/http/client/async-ssl/http_client_async_ssl.cpp +++ b/example/http/client/async-ssl/http_client_async_ssl.cpp @@ -63,10 +63,11 @@ public: run( char const* host, char const* port, - char const* target) + char const* target, + int version) { // Set up an HTTP GET request message - req_.version(11); + req_.version(version); req_.method(http::verb::get); req_.target(target); req_.set(http::field::host, host); @@ -193,17 +194,19 @@ public: int main(int argc, char** argv) { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-async-ssl \n" << + "Usage: http-client-async-ssl []\n" << "Example:\n" << - " http-client-async-ssl www.example.com 443 /\n"; + " http-client-async-ssl www.example.com 443 /\n" << + " http-client-async-ssl www.example.com 443 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; @@ -215,7 +218,7 @@ int main(int argc, char** argv) load_root_certificates(ctx); // Launch the asynchronous operation - std::make_shared(ioc, ctx)->run(host, port, target); + std::make_shared(ioc, ctx)->run(host, port, target, version); // Run the I/O service. The call will return when // the get operation is complete. diff --git a/example/http/client/async/http_client_async.cpp b/example/http/client/async/http_client_async.cpp index c7d46468..ec9fc09e 100644 --- a/example/http/client/async/http_client_async.cpp +++ b/example/http/client/async/http_client_async.cpp @@ -59,10 +59,11 @@ public: run( char const* host, char const* port, - char const* target) + char const* target, + int version) { // Set up an HTTP GET request message - req_.version(11); + req_.version(version); req_.method(http::verb::get); req_.target(target); req_.set(http::field::host, host); @@ -161,23 +162,25 @@ public: int main(int argc, char** argv) { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-async \n" << + "Usage: http-client-async []\n" << "Example:\n" << - " http-client-async www.example.com 80 /\n"; + " http-client-async www.example.com 80 /\n" << + " http-client-async www.example.com 80 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; // Launch the asynchronous operation - std::make_shared(ioc)->run(host, port, target); + std::make_shared(ioc)->run(host, port, target, version); // Run the I/O service. The call will return when // the get operation is complete. diff --git a/example/http/client/coro-ssl/http_client_coro_ssl.cpp b/example/http/client/coro-ssl/http_client_coro_ssl.cpp index 98b5fbdd..2e796704 100644 --- a/example/http/client/coro-ssl/http_client_coro_ssl.cpp +++ b/example/http/client/coro-ssl/http_client_coro_ssl.cpp @@ -46,6 +46,7 @@ do_session( std::string const& host, std::string const& port, std::string const& target, + int version, boost::asio::io_context& ioc, ssl::context& ctx, boost::asio::yield_context yield) @@ -72,7 +73,7 @@ do_session( return fail(ec, "handshake"); // Set up an HTTP GET request message - http::request req{http::verb::get, target, 11}; + http::request req{http::verb::get, target, version}; req.set(http::field::host, host); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); @@ -114,17 +115,19 @@ do_session( int main(int argc, char** argv) { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-coro-ssl \n" << + "Usage: http-client-coro-ssl []\n" << "Example:\n" << - " http-client-coro-ssl www.example.com 443 /\n"; + " http-client-coro-ssl www.example.com 443 /\n" << + " http-client-coro-ssl www.example.com 443 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; @@ -141,6 +144,7 @@ int main(int argc, char** argv) std::string(host), std::string(port), std::string(target), + version, std::ref(ioc), std::ref(ctx), std::placeholders::_1)); diff --git a/example/http/client/coro/http_client_coro.cpp b/example/http/client/coro/http_client_coro.cpp index b076b2f7..f07498e3 100644 --- a/example/http/client/coro/http_client_coro.cpp +++ b/example/http/client/coro/http_client_coro.cpp @@ -42,6 +42,7 @@ do_session( std::string const& host, std::string const& port, std::string const& target, + int version, boost::asio::io_context& ioc, boost::asio::yield_context yield) { @@ -62,7 +63,7 @@ do_session( return fail(ec, "connect"); // Set up an HTTP GET request message - http::request req{http::verb::get, target, 11}; + http::request req{http::verb::get, target, version}; req.set(http::field::host, host); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); @@ -102,17 +103,19 @@ do_session( int main(int argc, char** argv) { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-coro \n" << + "Usage: http-client-coro []\n" << "Example:\n" << - " http-client-coro www.example.com 80 /\n"; + " http-client-coro www.example.com 80 /\n" << + " http-client-coro www.example.com 80 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; @@ -123,6 +126,7 @@ int main(int argc, char** argv) std::string(host), std::string(port), std::string(target), + version, std::ref(ioc), std::placeholders::_1)); diff --git a/example/http/client/sync-ssl/http_client_sync_ssl.cpp b/example/http/client/sync-ssl/http_client_sync_ssl.cpp index 54bb6d67..86c9f9e8 100644 --- a/example/http/client/sync-ssl/http_client_sync_ssl.cpp +++ b/example/http/client/sync-ssl/http_client_sync_ssl.cpp @@ -35,17 +35,19 @@ int main(int argc, char** argv) try { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-sync-ssl \n" << + "Usage: http-client-sync-ssl []\n" << "Example:\n" << - " http-client-sync-ssl www.example.com 443 /\n"; + " http-client-sync-ssl www.example.com 443 /\n" << + " http-client-sync-ssl www.example.com 443 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; @@ -70,7 +72,7 @@ int main(int argc, char** argv) stream.handshake(ssl::stream_base::client); // Set up an HTTP GET request message - http::request req{http::verb::get, target, 11}; + http::request req{http::verb::get, target, version}; req.set(http::field::host, host); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); diff --git a/example/http/client/sync/http_client_sync.cpp b/example/http/client/sync/http_client_sync.cpp index 09a3e04f..9189f015 100644 --- a/example/http/client/sync/http_client_sync.cpp +++ b/example/http/client/sync/http_client_sync.cpp @@ -33,17 +33,19 @@ int main(int argc, char** argv) try { // Check command line arguments. - if(argc != 4) + if(argc != 4 && argc != 5) { std::cerr << - "Usage: http-client-sync \n" << + "Usage: http-client-sync []\n" << "Example:\n" << - " http-client-sync www.example.com 80 /\n"; + " http-client-sync www.example.com 80 /\n" << + " http-client-sync www.example.com 80 / 1.0\n"; return EXIT_FAILURE; } auto const host = argv[1]; auto const port = argv[2]; auto const target = argv[3]; + int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; // The io_context is required for all I/O boost::asio::io_context ioc; @@ -59,7 +61,7 @@ int main(int argc, char** argv) boost::asio::connect(socket, results.begin(), results.end()); // Set up an HTTP GET request message - http::request req{http::verb::get, target, 11}; + http::request req{http::verb::get, target, version}; req.set(http::field::host, host); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);