2016-10-02 16:33:42 -04:00
|
|
|
//
|
2017-02-06 20:07:03 -05:00
|
|
|
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2016-10-02 16:33:42 -04: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-06-10 06:43:23 -07:00
|
|
|
#include <beast/core.hpp>
|
2016-10-02 16:33:42 -04:00
|
|
|
#include <beast/http.hpp>
|
2017-06-21 04:38:42 -07:00
|
|
|
#include <beast/version.hpp>
|
2016-10-02 16:33:42 -04:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2017-06-19 14:41:28 -07:00
|
|
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
|
|
|
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
|
|
|
namespace http = beast::http; // from <beast/http.hpp>
|
|
|
|
|
2016-10-02 16:33:42 -04:00
|
|
|
int main()
|
|
|
|
{
|
2017-06-17 20:17:01 -07:00
|
|
|
// A helper for reporting errors
|
|
|
|
auto const fail =
|
|
|
|
[](std::string what, beast::error_code ec)
|
|
|
|
{
|
|
|
|
std::cerr << what << ": " << ec.message() << std::endl;
|
|
|
|
std::cerr.flush();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::system::error_code ec;
|
2016-10-02 16:33:42 -04:00
|
|
|
|
|
|
|
// Normal boost::asio setup
|
2017-06-17 20:17:01 -07:00
|
|
|
boost::asio::io_service ios;
|
2017-06-19 14:41:28 -07:00
|
|
|
tcp::resolver r{ios};
|
|
|
|
tcp::socket sock{ios};
|
2017-06-17 20:17:01 -07:00
|
|
|
|
|
|
|
// Look up the domain name
|
|
|
|
std::string const host = "www.example.com";
|
2017-06-19 07:28:20 -07:00
|
|
|
auto const lookup = r.resolve({host, "https"}, ec);
|
2017-06-17 20:17:01 -07:00
|
|
|
if(ec)
|
|
|
|
return fail("resolve", ec);
|
|
|
|
|
|
|
|
// Make the connection on the IP address we get from a lookup
|
|
|
|
boost::asio::connect(sock, lookup, ec);
|
|
|
|
if(ec)
|
|
|
|
return fail("connect", ec);
|
|
|
|
|
|
|
|
// Wrap the now-connected socket in an SSL stream
|
2017-06-19 14:41:28 -07:00
|
|
|
ssl::context ctx{ssl::context::sslv23};
|
|
|
|
ssl::stream<tcp::socket&> stream{sock, ctx};
|
|
|
|
stream.set_verify_mode(ssl::verify_none);
|
2016-10-02 16:33:42 -04:00
|
|
|
|
|
|
|
// Perform SSL handshaking
|
2017-06-19 14:41:28 -07:00
|
|
|
stream.handshake(ssl::stream_base::client, ec);
|
2017-06-17 20:17:01 -07:00
|
|
|
if(ec)
|
|
|
|
return fail("handshake", ec);
|
2016-10-02 16:33:42 -04:00
|
|
|
|
2017-06-17 20:17:01 -07:00
|
|
|
// Set up an HTTP GET request message
|
2017-06-19 14:41:28 -07:00
|
|
|
http::request<http::string_body> req;
|
|
|
|
req.method(http::verb::get);
|
2017-05-02 15:49:22 -07:00
|
|
|
req.target("/");
|
2016-10-02 16:33:42 -04:00
|
|
|
req.version = 11;
|
2017-06-19 14:41:28 -07:00
|
|
|
req.set(http::field::host, host + ":" +
|
2017-06-20 15:09:53 -07:00
|
|
|
std::to_string(sock.remote_endpoint().port()));
|
2017-06-21 04:38:42 -07:00
|
|
|
req.set(http::field::user_agent, BEAST_VERSION_STRING);
|
2017-06-19 15:37:39 -07:00
|
|
|
req.prepare_payload();
|
2016-10-02 16:33:42 -04:00
|
|
|
|
2017-06-17 20:17:01 -07:00
|
|
|
// Write the HTTP request to the remote host
|
2017-06-19 14:41:28 -07:00
|
|
|
http::write(stream, req, ec);
|
2017-06-17 20:17:01 -07:00
|
|
|
if(ec)
|
|
|
|
return fail("write", ec);
|
|
|
|
|
|
|
|
// This buffer is used for reading and must be persisted
|
2017-06-10 06:43:23 -07:00
|
|
|
beast::flat_buffer b;
|
2017-06-17 20:17:01 -07:00
|
|
|
|
|
|
|
// Declare a container to hold the response
|
2017-06-19 14:41:28 -07:00
|
|
|
http::response<http::dynamic_body> res;
|
2017-06-17 20:17:01 -07:00
|
|
|
|
|
|
|
// Read the response
|
2017-06-19 14:41:28 -07:00
|
|
|
http::read(stream, b, res, ec);
|
2017-06-17 20:17:01 -07:00
|
|
|
if(ec)
|
|
|
|
return fail("read", ec);
|
|
|
|
|
|
|
|
// Write the message to standard out
|
|
|
|
std::cout << res << std::endl;
|
2016-10-02 16:33:42 -04:00
|
|
|
|
|
|
|
// Shut down SSL on the stream
|
|
|
|
stream.shutdown(ec);
|
|
|
|
if(ec && ec != boost::asio::error::eof)
|
2017-06-19 19:42:06 -07:00
|
|
|
fail("ssl_shutdown ", ec);
|
2017-06-17 18:33:39 -07:00
|
|
|
|
2017-06-17 20:17:01 -07:00
|
|
|
// If we get here then the connection is closed gracefully
|
|
|
|
return EXIT_SUCCESS;
|
2016-10-02 16:33:42 -04:00
|
|
|
}
|