Set SNI hostname in example SSL clients

fix #853
This commit is contained in:
Vinnie Falco
2017-10-29 06:52:39 -07:00
parent c5a1615301
commit b75d4e5e14
4 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,7 @@
Version 131:
* basic_fields returns const values
* Set SNI hostname in example SSL clients
--------------------------------------------------------------------------------

View File

@ -20,6 +20,7 @@
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <functional>
@ -66,6 +67,14 @@ public:
char const* target,
int version)
{
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream_.native_handle(), host))
{
boost::system::error_code ec{static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
std::cerr << ec.message() << "\n";
return;
}
// Set up an HTTP GET request message
req_.version(version);
req_.method(http::verb::get);

View File

@ -21,6 +21,7 @@
#include <boost/asio/connect.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <functional>
@ -57,6 +58,14 @@ do_session(
tcp::resolver resolver{ioc};
ssl::stream<tcp::socket> stream{ioc, ctx};
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
{
ec.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
std::cerr << ec.message() << "\n";
return;
}
// Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]);
if(ec)

View File

@ -20,6 +20,7 @@
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <iostream>
@ -62,6 +63,13 @@ int main(int argc, char** argv)
tcp::resolver resolver{ioc};
ssl::stream<tcp::socket> stream{ioc, ctx};
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host))
{
boost::system::error_code ec{static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
throw boost::system::system_error{ec};
}
// Look up the domain name
auto const results = resolver.resolve(host, port);