Refactor all examples:

fix #575, fix #604, fix #608, fix #634, fix #712

All examples are rewritten:

* Using Best Practices
* Mostly self-contained
* New examples to complete the feature matrix
* The server-framework example is removed
This commit is contained in:
Vinnie Falco committed 2017-08-07 20:53:11 -07:00
1 parent 20a8f7d75b
commit dd71b0f94d
187 files changed
+11759 -7098

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
#
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/beast
#
GroupSources(include/boost/beast beast)
GroupSources(example/http/client/coro "/")
add_executable (http-client-coro
${BOOST_BEAST_INCLUDES}
Jamfile
http_client_coro.cpp
)
+15
View File
@@ -0,0 +1,15 @@
#
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/beast
#
exe http-client-coro :
http_client_coro.cpp
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
;
@@ -0,0 +1,134 @@
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
//------------------------------------------------------------------------------
//
// Example: HTTP client, coroutine
//
//------------------------------------------------------------------------------
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
//------------------------------------------------------------------------------
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Performs an HTTP GET and prints the response
void
do_session(
std::string const& host,
std::string const& port,
std::string const& target,
boost::asio::io_service& ios,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ios};
tcp::socket socket{ios};
// Look up the domain name
auto const lookup = resolver.async_resolve({host, port}, yield[ec]);
if(ec)
return fail(ec, "resolve");
// Make the connection on the IP address we get from a lookup
boost::asio::async_connect(socket, lookup, yield[ec]);
if(ec)
return fail(ec, "connect");
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, 11};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::async_write(socket, req, yield[ec]);
if(ec)
return fail(ec, "write");
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer b;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
// Receive the HTTP response
http::async_read(socket, b, res, yield[ec]);
if(ec)
return fail(ec, "read");
// Write the message to standard out
std::cout << res << std::endl;
// Gracefully close the socket
socket.shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes
// so don't bother reporting it.
//
if(ec && ec != boost::system::errc::not_connected)
return fail(ec, "shutdown");
// If we get here then the connection is closed gracefully
}
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: http-client-coro <host> <port> <target>\n" <<
"Example:\n" <<
" http-client-coro www.example.com 80 /\n";
return EXIT_FAILURE;
}
auto const host = argv[1];
auto const port = argv[2];
auto const target = argv[3];
// The io_service is required for all I/O
boost::asio::io_service ios;
// Launch the asynchronous operation
boost::asio::spawn(ios, std::bind(
&do_session,
std::string(host),
std::string(port),
std::string(target),
std::ref(ios),
std::placeholders::_1));
// Run the I/O service. The call will return when
// the get operation is complete.
ios.run();
return EXIT_SUCCESS;
}