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").
This commit is contained in:
Sorin Fetche
2017-10-25 08:24:47 -07:00
committed by Vinnie Falco
parent bea7d6e019
commit f4facffae3
7 changed files with 47 additions and 28 deletions
@@ -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<http::string_body> req{http::verb::get, target, 11};
http::request<http::string_body> 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 <host> <port> <target>\n" <<
"Usage: http-client-coro <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\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));