Files
boost_beast/test/doc/core_snippets.cpp
T

140 lines
3.6 KiB
C++
Raw Normal View History

2017-06-07 18:18:50 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-06-07 18:18:50 -07:00
//
// 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)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-06-07 18:18:50 -07:00
//[snippet_core_1a
2017-07-20 13:40:34 -07:00
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
2017-06-07 18:18:50 -07:00
#include <boost/asio.hpp>
2018-12-01 13:22:21 -08:00
#include <boost/asio/ssl.hpp>
2017-06-07 18:18:50 -07:00
#include <iostream>
#include <thread>
//]
2017-07-20 13:40:34 -07:00
using namespace boost::beast;
2017-06-07 18:18:50 -07:00
namespace doc_core_snippets {
void fxx()
{
//[snippet_core_1b
//
2017-07-20 13:40:34 -07:00
using namespace boost::beast;
2018-12-01 13:22:21 -08:00
namespace net = boost::asio;
namespace ssl = boost::asio::ssl;
using tcp = net::ip::tcp;
2017-06-07 18:18:50 -07:00
2018-11-30 14:58:38 -08:00
net::io_context ioc;
2020-07-04 07:33:22 +02:00
net::any_io_executor work =
2020-07-04 08:22:39 +02:00
net::require(ioc.get_executor(),
2020-07-04 07:33:22 +02:00
net::execution::outstanding_work.tracked);
2017-09-07 07:39:52 -07:00
std::thread t{[&](){ ioc.run(); }};
2017-06-07 18:18:50 -07:00
2017-06-07 16:30:49 -07:00
error_code ec;
2018-12-01 13:22:21 -08:00
tcp::socket sock{ioc};
2017-06-07 18:18:50 -07:00
//]
2018-11-22 20:49:30 -08:00
boost::ignore_unused(ec);
2017-06-07 18:18:50 -07:00
2018-11-22 20:49:30 -08:00
{
2017-06-07 18:18:50 -07:00
//[snippet_core_2
2018-12-01 13:22:21 -08:00
// The resolver is used to look up IP addresses and port numbers from a domain and service name pair
tcp::resolver r{ioc};
// A socket represents the local end of a connection between two peers
tcp::socket stream{ioc};
// Establish a connection before sending and receiving data
net::connect(stream, r.resolve("www.example.com", "http"));
2017-06-07 18:18:50 -07:00
// At this point `stream` is a connected to a remote
// host and may be used to perform stream operations.
//]
2018-11-22 20:49:30 -08:00
}
2017-06-07 18:18:50 -07:00
} // fxx()
2018-11-22 20:49:30 -08:00
//------------------------------------------------------------------------------
2017-06-07 18:18:50 -07:00
//[snippet_core_3
template<class SyncWriteStream>
void write_string(SyncWriteStream& stream, string_view s)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
2019-02-20 19:19:59 -08:00
"SyncWriteStream type requirements not met");
2018-11-30 14:58:38 -08:00
net::write(stream, net::const_buffer(s.data(), s.size()));
2017-06-07 18:18:50 -07:00
}
//]
void ssl_tls_shutdown()
{
net::io_context ioc;
net::ssl::context ctx(net::ssl::context::tlsv12);
2024-05-30 11:39:48 +00:00
net::ssl::stream<tcp_stream> stream(ioc, ctx);
flat_buffer buffer;
http::response<http::dynamic_body> res;
auto log = [](error_code){};
{
//[snippet_core_4
// Receive the HTTP response
http::read(stream, buffer, res);
// Gracefully shutdown the SSL/TLS connection
error_code ec;
stream.shutdown(ec);
// Non-compliant servers don't participate in the SSL/TLS shutdown process and
// close the underlying transport layer. This causes the shutdown operation to
// complete with a `stream_truncated` error. One might decide not to log such
// errors as there are many non-compliant servers in the wild.
if(ec != net::ssl::error::stream_truncated)
log(ec);
//]
}
{
//[snippet_core_5
// Use an HTTP response parser to have more control
http::response_parser<http::dynamic_body> parser;
error_code ec;
// Receive the HTTP response until the end or until an error occurs
http::read(stream, buffer, parser, ec);
// Try to manually commit the EOF, the resulting message body would be
// vulnerable to truncation attacks.
if(parser.need_eof() && ec == net::ssl::error::stream_truncated)
parser.put_eof(ec); // Override the error_code
if(ec)
throw system_error{ec};
// Access the HTTP response inside the parser
std::cout << parser.get() << std::endl;
// Gracefully shutdown the SSL/TLS connection
stream.shutdown(ec); // Override the error_code
// Non-compliant servers don't participate in the SSL/TLS shutdown process and
// close the underlying transport layer. This causes the shutdown operation to
// complete with a `stream_truncated` error. One might decide not to log such
// errors as there are many non-compliant servers in the wild.
if(ec != net::ssl::error::stream_truncated)
log(ec);
//]
}
}
2017-06-07 18:18:50 -07:00
} // doc_core_snippets