forked from boostorg/beast
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:
@@ -0,0 +1,19 @@
|
||||
#
|
||||
# Copyright (c) 2013-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
|
||||
#
|
||||
|
||||
add_subdirectory (async)
|
||||
add_subdirectory (coro)
|
||||
add_subdirectory (crawl)
|
||||
add_subdirectory (sync)
|
||||
|
||||
if (OPENSSL_FOUND)
|
||||
add_subdirectory (async-ssl)
|
||||
add_subdirectory (coro-ssl)
|
||||
add_subdirectory (sync-ssl)
|
||||
endif()
|
||||
@@ -0,0 +1,18 @@
|
||||
#
|
||||
# Copyright (c) 2013-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
|
||||
#
|
||||
|
||||
build-project async ;
|
||||
build-project coro ;
|
||||
build-project crawl ;
|
||||
build-project sync ;
|
||||
|
||||
# VFALCO How do I make this work on Windows and if OpenSSL is not available?
|
||||
#build-project async-ssl ;
|
||||
#build-project coro-ssl ;
|
||||
#build-project sync-ssl ;
|
||||
@@ -0,0 +1,23 @@
|
||||
#
|
||||
# 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/common common)
|
||||
GroupSources(example/http/client/async-ssl "/")
|
||||
|
||||
add_executable (http-client-async-ssl
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
|
||||
Jamfile
|
||||
http_client_async_ssl.cpp
|
||||
)
|
||||
|
||||
target_link_libraries (http-client-async-ssl
|
||||
${OPENSSL_LIBRARIES}
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
project
|
||||
: requirements
|
||||
<library>ssl
|
||||
<library>crypto
|
||||
;
|
||||
|
||||
exe http-client-async-ssl :
|
||||
http_client_async_ssl.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,212 @@
|
||||
//
|
||||
// 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 SSL client, asynchronous
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "example/common/root_certificates.hpp"
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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
|
||||
class session : public std::enable_shared_from_this<session>
|
||||
{
|
||||
tcp::resolver resolver_;
|
||||
ssl::stream<tcp::socket> stream_;
|
||||
boost::beast::flat_buffer buffer_; // (Must persist between reads)
|
||||
http::request<http::empty_body> req_;
|
||||
http::response<http::string_body> res_;
|
||||
|
||||
public:
|
||||
// Resolver and stream require an io_service
|
||||
explicit
|
||||
session(boost::asio::io_service& ios, ssl::context& ctx)
|
||||
: resolver_(ios)
|
||||
, stream_(ios, ctx)
|
||||
{
|
||||
}
|
||||
|
||||
// Start the asynchronous operation
|
||||
void
|
||||
run(
|
||||
char const* host,
|
||||
char const* port,
|
||||
char const* target)
|
||||
{
|
||||
// Set up an HTTP GET request message
|
||||
req_.version = 11;
|
||||
req_.method(http::verb::get);
|
||||
req_.target(target);
|
||||
req_.set(http::field::host, host);
|
||||
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve({host, port},
|
||||
std::bind(
|
||||
&session::on_resolve,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(
|
||||
stream_.next_layer(),
|
||||
result,
|
||||
std::bind(
|
||||
&session::on_connect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_connect(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "connect");
|
||||
|
||||
// Perform the SSL handshake
|
||||
stream_.async_handshake(
|
||||
ssl::stream_base::client,
|
||||
std::bind(
|
||||
&session::on_handshake,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_handshake(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "handshake");
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::async_write(stream_, req_,
|
||||
std::bind(
|
||||
&session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "write");
|
||||
|
||||
// Receive the HTTP response
|
||||
http::async_read(stream_, buffer_, res_,
|
||||
std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Write the message to standard out
|
||||
std::cout << res_ << std::endl;
|
||||
|
||||
// Gracefully close the stream
|
||||
stream_.async_shutdown(
|
||||
std::bind(
|
||||
&session::on_shutdown,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_shutdown(boost::system::error_code ec)
|
||||
{
|
||||
if(ec == boost::asio::error::eof)
|
||||
{
|
||||
// Rationale:
|
||||
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
if(ec)
|
||||
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-async-ssl <host> <port> <target>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-client-async-ssl www.example.com 443 /\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;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
|
||||
// This holds the root certificate used for verification
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// Launch the asynchronous operation
|
||||
std::make_shared<session>(ios, ctx)->run(host, port, target);
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -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/async "/")
|
||||
|
||||
add_executable (http-client-async
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
Jamfile
|
||||
http_client_async.cpp
|
||||
)
|
||||
@@ -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-async :
|
||||
http_client_async.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,172 @@
|
||||
//
|
||||
// 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, asynchronous
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#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
|
||||
class session : public std::enable_shared_from_this<session>
|
||||
{
|
||||
tcp::resolver resolver_;
|
||||
tcp::socket socket_;
|
||||
boost::beast::flat_buffer buffer_; // (Must persist between reads)
|
||||
http::request<http::empty_body> req_;
|
||||
http::response<http::string_body> res_;
|
||||
|
||||
public:
|
||||
// Resolver and socket require an io_service
|
||||
explicit
|
||||
session(boost::asio::io_service& ios)
|
||||
: resolver_(ios)
|
||||
, socket_(ios)
|
||||
{
|
||||
}
|
||||
|
||||
// Start the asynchronous operation
|
||||
void
|
||||
run(
|
||||
char const* host,
|
||||
char const* port,
|
||||
char const* target)
|
||||
{
|
||||
// Set up an HTTP GET request message
|
||||
req_.version = 11;
|
||||
req_.method(http::verb::get);
|
||||
req_.target(target);
|
||||
req_.set(http::field::host, host);
|
||||
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve({host, port},
|
||||
std::bind(
|
||||
&session::on_resolve,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "resolve");
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(socket_, result,
|
||||
std::bind(
|
||||
&session::on_connect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_connect(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "connect");
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::async_write(socket_, req_,
|
||||
std::bind(
|
||||
&session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "write");
|
||||
|
||||
// Receive the HTTP response
|
||||
http::async_read(socket_, buffer_, res_,
|
||||
std::bind(
|
||||
&session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code 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-async <host> <port> <target>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-client-async 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
|
||||
std::make_shared<session>(ios)->run(host, port, target);
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#
|
||||
# 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/common common)
|
||||
GroupSources(example/http/client/coro-ssl "/")
|
||||
|
||||
add_executable (http-client-coro-ssl
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
|
||||
Jamfile
|
||||
http_client_coro_ssl.cpp
|
||||
)
|
||||
|
||||
target_link_libraries (http-client-coro-ssl
|
||||
${OPENSSL_LIBRARIES}
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
project
|
||||
: requirements
|
||||
<library>ssl
|
||||
<library>crypto
|
||||
;
|
||||
|
||||
exe http-client-coro-ssl :
|
||||
http_client_coro_ssl.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,153 @@
|
||||
//
|
||||
// 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 SSL client, coroutine
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "example/common/root_certificates.hpp"
|
||||
|
||||
#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 <boost/asio/ssl/stream.hpp>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.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,
|
||||
ssl::context& ctx,
|
||||
boost::asio::yield_context yield)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
ssl::stream<tcp::socket> stream{ios, ctx};
|
||||
|
||||
// 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(stream.next_layer(), lookup, yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "connect");
|
||||
|
||||
// Perform the SSL handshake
|
||||
stream.async_handshake(ssl::stream_base::client, yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "handshake");
|
||||
|
||||
// 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(stream, 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(stream, b, res, yield[ec]);
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Write the message to standard out
|
||||
std::cout << res << std::endl;
|
||||
|
||||
// Gracefully close the stream
|
||||
stream.async_shutdown(yield[ec]);
|
||||
if(ec == boost::asio::error::eof)
|
||||
{
|
||||
// Rationale:
|
||||
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
if(ec)
|
||||
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-ssl <host> <port> <target>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-client-coro-ssl www.example.com 443 /\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;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
|
||||
// This holds the root certificate used for verification
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// 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::ref(ctx),
|
||||
std::placeholders::_1));
|
||||
|
||||
// Run the I/O service. The call will return when
|
||||
// the get operation is complete.
|
||||
ios.run();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#
|
||||
# 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/crawl "/")
|
||||
|
||||
add_executable (http-crawl
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
Jamfile
|
||||
urls_large_data.hpp
|
||||
urls_large_data.cpp
|
||||
http_crawl.cpp
|
||||
)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# 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-crawl :
|
||||
http_crawl.cpp
|
||||
urls_large_data.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,450 @@
|
||||
//
|
||||
// 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 crawl (asynchronous)
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "urls_large_data.hpp"
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
namespace chrono = std::chrono; // from <chrono>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// This structure aggregates statistics on all the sites
|
||||
class crawl_report
|
||||
{
|
||||
boost::asio::io_service& ios_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
std::atomic<std::size_t> index_;
|
||||
std::vector<char const*> const& hosts_;
|
||||
std::size_t count_ = 0;
|
||||
|
||||
public:
|
||||
crawl_report(boost::asio::io_service& ios)
|
||||
: ios_(ios)
|
||||
, strand_(ios_)
|
||||
, index_(0)
|
||||
, hosts_(urls_large_data())
|
||||
{
|
||||
}
|
||||
|
||||
// Run an aggregation function on the strand.
|
||||
// This allows synchronization without a mutex.
|
||||
template<class F>
|
||||
void
|
||||
aggregate(F const& f)
|
||||
{
|
||||
ios_.post(strand_.wrap(
|
||||
[&, f]
|
||||
{
|
||||
f(*this);
|
||||
if(count_ % 100 == 0)
|
||||
{
|
||||
std::cerr <<
|
||||
"Progress: " << count_ << " of " << hosts_.size() << "\n";
|
||||
//std::cerr << *this;
|
||||
}
|
||||
++count_;
|
||||
}));
|
||||
}
|
||||
|
||||
// Returns the next host to check
|
||||
char const*
|
||||
get_host()
|
||||
{
|
||||
auto const n = index_++;
|
||||
if(n >= hosts_.size())
|
||||
return nullptr;
|
||||
return hosts_[n];
|
||||
}
|
||||
|
||||
// Counts the number of timer failures
|
||||
std::size_t timer_failures = 0;
|
||||
|
||||
// Counts the number of name resolution failures
|
||||
std::size_t resolve_failures = 0;
|
||||
|
||||
// Counts the number of connection failures
|
||||
std::size_t connect_failures = 0;
|
||||
|
||||
// Counts the number of write failures
|
||||
std::size_t write_failures = 0;
|
||||
|
||||
// Counts the number of read failures
|
||||
std::size_t read_failures = 0;
|
||||
|
||||
// Counts the number of success reads
|
||||
std::size_t success = 0;
|
||||
|
||||
// Counts the number received of each status code
|
||||
std::map<unsigned, std::size_t> status_codes;
|
||||
};
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, crawl_report const& report)
|
||||
{
|
||||
// Print the report
|
||||
os <<
|
||||
"Crawl report\n" <<
|
||||
" Failure counts\n" <<
|
||||
" Timer : " << report.timer_failures << "\n" <<
|
||||
" Resolve : " << report.resolve_failures << "\n" <<
|
||||
" Connect : " << report.connect_failures << "\n" <<
|
||||
" Write : " << report.write_failures << "\n" <<
|
||||
" Read : " << report.read_failures << "\n" <<
|
||||
" Success : " << report.success << "\n" <<
|
||||
" Status codes\n"
|
||||
;
|
||||
for(auto const& result : report.status_codes)
|
||||
os <<
|
||||
" " << std::setw(3) << result.first << ": " << result.second <<
|
||||
" (" << http::obsolete_reason(static_cast<http::status>(result.first)) << ")\n";
|
||||
os.flush();
|
||||
return os;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Performs HTTP GET requests and aggregates the results into a report
|
||||
class worker : public std::enable_shared_from_this<worker>
|
||||
{
|
||||
enum
|
||||
{
|
||||
// Use a small timeout to keep things lively
|
||||
timeout = 5
|
||||
};
|
||||
|
||||
crawl_report& report_;
|
||||
tcp::resolver resolver_;
|
||||
tcp::socket socket_;
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::asio::io_service::strand strand_;
|
||||
boost::beast::flat_buffer buffer_; // (Must persist between reads)
|
||||
http::request<http::empty_body> req_;
|
||||
http::response<http::string_body> res_;
|
||||
|
||||
public:
|
||||
worker(worker&&) = default;
|
||||
|
||||
// Resolver and socket require an io_service
|
||||
worker(
|
||||
crawl_report& report,
|
||||
boost::asio::io_service& ios)
|
||||
: report_(report)
|
||||
, resolver_(ios)
|
||||
, socket_(ios)
|
||||
, timer_(ios,
|
||||
(chrono::steady_clock::time_point::max)())
|
||||
, strand_(ios)
|
||||
{
|
||||
// Set up the common fields of the request
|
||||
req_.version = 11;
|
||||
req_.method(http::verb::get);
|
||||
req_.target("/");
|
||||
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
}
|
||||
|
||||
// Start the asynchronous operation
|
||||
void
|
||||
run()
|
||||
{
|
||||
// Run the timer. The timer is operated
|
||||
// continuously, this simplifies the code.
|
||||
on_timer({});
|
||||
|
||||
do_get_host();
|
||||
}
|
||||
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
{
|
||||
if(ec && ec != boost::asio::error::operation_aborted)
|
||||
{
|
||||
// Should never happen
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.timer_failures;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify that the timer really expired since the deadline may have moved.
|
||||
if(timer_.expires_at() <= chrono::steady_clock::now())
|
||||
{
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_timer,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
do_get_host()
|
||||
{
|
||||
// Grab another host
|
||||
auto const host = report_.get_host();
|
||||
|
||||
// nullptr means no more work
|
||||
if(! host)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
timer_.cancel(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
// The Host HTTP field is required
|
||||
req_.set(http::field::host, host);
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
|
||||
// Set up an HTTP GET request message
|
||||
// Look up the domain name
|
||||
resolver_.async_resolve(
|
||||
tcp::resolver::query{host, "http"},
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_resolve,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
}
|
||||
|
||||
void
|
||||
on_resolve(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::iterator result)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.resolve_failures;
|
||||
});
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::async_connect(
|
||||
socket_,
|
||||
result,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_connect,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_connect(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.connect_failures;
|
||||
});
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::async_write(
|
||||
socket_,
|
||||
req_,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.write_failures;
|
||||
});
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
// Set the timer
|
||||
timer_.expires_from_now(chrono::seconds(timeout));
|
||||
|
||||
// Receive the HTTP response
|
||||
http::async_read(
|
||||
socket_,
|
||||
buffer_,
|
||||
res_,
|
||||
strand_.wrap(std::bind(
|
||||
&worker::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
report_.aggregate(
|
||||
[](crawl_report& rep)
|
||||
{
|
||||
++rep.read_failures;
|
||||
});
|
||||
return do_get_host();
|
||||
}
|
||||
|
||||
auto const code = res_.result_int();
|
||||
report_.aggregate(
|
||||
[code](crawl_report& rep)
|
||||
{
|
||||
++rep.success;
|
||||
++rep.status_codes[code];
|
||||
});
|
||||
|
||||
// Gracefully close the socket
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
|
||||
do_get_host();
|
||||
}
|
||||
};
|
||||
|
||||
class timer
|
||||
{
|
||||
using clock_type = chrono::system_clock;
|
||||
|
||||
clock_type::time_point when_;
|
||||
|
||||
public:
|
||||
using duration = clock_type::duration;
|
||||
|
||||
timer()
|
||||
: when_(clock_type::now())
|
||||
{
|
||||
}
|
||||
|
||||
duration
|
||||
elapsed() const
|
||||
{
|
||||
return clock_type::now() - when_;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// Check command line arguments.
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr <<
|
||||
"Usage: http-crawl <threads>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-crawl 100 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const threads = std::max<std::size_t>(1, std::atoi(argv[1]));
|
||||
|
||||
// The io_service is required for all I/O
|
||||
boost::asio::io_service ios{1};
|
||||
|
||||
// The work keeps io_service::run from returning
|
||||
boost::optional<boost::asio::io_service::work> work{ios};
|
||||
|
||||
// The report holds the aggregated statistics
|
||||
crawl_report report{ios};
|
||||
|
||||
timer t;
|
||||
|
||||
// Create and launch the worker threads.
|
||||
std::vector<std::thread> workers;
|
||||
workers.reserve(threads + 1);
|
||||
for(std::size_t i = 0; i < threads; ++i)
|
||||
workers.emplace_back(
|
||||
[&report]
|
||||
{
|
||||
// We use a separate io_service for each worker because
|
||||
// the asio resolver simulates asynchronous operation using
|
||||
// a dedicated worker thread per io_service, and we want to
|
||||
// do a lot of name resolutions in parallel.
|
||||
boost::asio::io_service ios{1};
|
||||
std::make_shared<worker>(report, ios)->run();
|
||||
ios.run();
|
||||
});
|
||||
|
||||
// Add another thread to run the main io_service which
|
||||
// is used to aggregate the statistics
|
||||
workers.emplace_back(
|
||||
[&ios]
|
||||
{
|
||||
ios.run();
|
||||
});
|
||||
|
||||
// Now block until all threads exit
|
||||
for(std::size_t i = 0; i < workers.size(); ++i)
|
||||
{
|
||||
auto& thread = workers[i];
|
||||
|
||||
// If this is the last thread, destroy the
|
||||
// work object so that it can return from run.
|
||||
//if(&thread == &workers.back())
|
||||
if(i == workers.size() - 1)
|
||||
work = boost::none;
|
||||
|
||||
// Wait for the thread to exit
|
||||
thread.join();
|
||||
}
|
||||
|
||||
std::cout <<
|
||||
"Elapsed time: " << chrono::duration_cast<chrono::seconds>(t.elapsed()).count() << " seconds\n";
|
||||
std::cout << report;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_EXAMPLE_HTTP_CLIENT_CRAWL_URLS_LARGE_DATA_HPP
|
||||
#define BOOST_BEAST_EXAMPLE_HTTP_CLIENT_CRAWL_URLS_LARGE_DATA_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<char const*> const&
|
||||
urls_large_data();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#
|
||||
# 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/common common)
|
||||
GroupSources(example/http/client/sync-ssl "/")
|
||||
|
||||
add_executable (http-client-sync-ssl
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${PROJECT_SOURCE_DIR}/example/common/root_certificates.hpp
|
||||
Jamfile
|
||||
http_client_sync_ssl.cpp
|
||||
)
|
||||
|
||||
target_link_libraries (http-client-sync-ssl
|
||||
${OPENSSL_LIBRARIES}
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
project
|
||||
: requirements
|
||||
<library>ssl
|
||||
<library>crypto
|
||||
;
|
||||
|
||||
exe http-client-sync-ssl :
|
||||
http_client_sync_ssl.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// 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 SSL client, synchronous
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "example/common/root_certificates.hpp"
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
|
||||
// Performs an HTTP GET and prints the response
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check command line arguments.
|
||||
if(argc != 4)
|
||||
{
|
||||
std::cerr <<
|
||||
"Usage: http-client-sync-ssl <host> <port> <target>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-client-sync-ssl www.example.com 443 /\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;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23_client};
|
||||
|
||||
// This holds the root certificate used for verification
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
ssl::stream<tcp::socket> stream{ios, ctx};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.resolve({host, port});
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(stream.next_layer(), lookup);
|
||||
|
||||
// Perform the SSL handshake
|
||||
stream.handshake(ssl::stream_base::client);
|
||||
|
||||
// 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::write(stream, req);
|
||||
|
||||
// This buffer is used for reading and must be persisted
|
||||
boost::beast::flat_buffer buffer;
|
||||
|
||||
// Declare a container to hold the response
|
||||
http::response<http::dynamic_body> res;
|
||||
|
||||
// Receive the HTTP response
|
||||
http::read(stream, buffer, res);
|
||||
|
||||
// Write the message to standard out
|
||||
std::cout << res << std::endl;
|
||||
|
||||
// Gracefully close the stream
|
||||
boost::system::error_code ec;
|
||||
stream.shutdown(ec);
|
||||
if(ec == boost::asio::error::eof)
|
||||
{
|
||||
// Rationale:
|
||||
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
if(ec)
|
||||
throw boost::system::system_error{ec};
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -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/sync "/")
|
||||
|
||||
add_executable (http-client-sync
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
Jamfile
|
||||
http_client_sync.cpp
|
||||
)
|
||||
@@ -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-sync :
|
||||
http_client_sync.cpp
|
||||
:
|
||||
<variant>coverage:<build>no
|
||||
<variant>ubasan:<build>no
|
||||
;
|
||||
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// 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, synchronous
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//[example_http_client
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <cstdlib>
|
||||
#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>
|
||||
|
||||
// Performs an HTTP GET and prints the response
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check command line arguments.
|
||||
if(argc != 4)
|
||||
{
|
||||
std::cerr <<
|
||||
"Usage: http-client-sync <host> <port> <target>\n" <<
|
||||
"Example:\n" <<
|
||||
" http-client-sync 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;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ios};
|
||||
tcp::socket socket{ios};
|
||||
|
||||
// Look up the domain name
|
||||
auto const lookup = resolver.resolve({host, port});
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(socket, lookup);
|
||||
|
||||
// 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::write(socket, req);
|
||||
|
||||
// This buffer is used for reading and must be persisted
|
||||
boost::beast::flat_buffer buffer;
|
||||
|
||||
// Declare a container to hold the response
|
||||
http::response<http::dynamic_body> res;
|
||||
|
||||
// Receive the HTTP response
|
||||
http::read(socket, buffer, res);
|
||||
|
||||
// Write the message to standard out
|
||||
std::cout << res << std::endl;
|
||||
|
||||
// Gracefully close the socket
|
||||
boost::system::error_code ec;
|
||||
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)
|
||||
throw boost::system::system_error{ec};
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//]
|
||||
Reference in New Issue
Block a user