http_client_coro_ssl.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. //------------------------------------------------------------------------------
  10. //
  11. // Example: HTTP SSL client, coroutine
  12. //
  13. //------------------------------------------------------------------------------
  14. #include "example/common/root_certificates.hpp"
  15. #include <boost/beast/core.hpp>
  16. #include <boost/beast/http.hpp>
  17. #include <boost/beast/ssl.hpp>
  18. #include <boost/beast/version.hpp>
  19. #include <boost/asio/spawn.hpp>
  20. #include <cstdlib>
  21. #include <functional>
  22. #include <iostream>
  23. #include <string>
  24. namespace beast = boost::beast; // from <boost/beast.hpp>
  25. namespace http = beast::http; // from <boost/beast/http.hpp>
  26. namespace net = boost::asio; // from <boost/asio.hpp>
  27. namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
  28. using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
  29. //------------------------------------------------------------------------------
  30. // Report a failure
  31. void
  32. fail(beast::error_code ec, char const* what)
  33. {
  34. std::cerr << what << ": " << ec.message() << "\n";
  35. }
  36. // Performs an HTTP GET and prints the response
  37. void
  38. do_session(
  39. std::string const& host,
  40. std::string const& port,
  41. std::string const& target,
  42. int version,
  43. net::io_context& ioc,
  44. ssl::context& ctx,
  45. net::yield_context yield)
  46. {
  47. beast::error_code ec;
  48. // These objects perform our I/O
  49. tcp::resolver resolver(ioc);
  50. beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
  51. // Set SNI Hostname (many hosts need this to handshake successfully)
  52. if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
  53. {
  54. ec.assign(static_cast<int>(::ERR_get_error()), net::error::get_ssl_category());
  55. std::cerr << ec.message() << "\n";
  56. return;
  57. }
  58. // Look up the domain name
  59. auto const results = resolver.async_resolve(host, port, yield[ec]);
  60. if(ec)
  61. return fail(ec, "resolve");
  62. // Set the timeout.
  63. beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
  64. // Make the connection on the IP address we get from a lookup
  65. get_lowest_layer(stream).async_connect(results, yield[ec]);
  66. if(ec)
  67. return fail(ec, "connect");
  68. // Set the timeout.
  69. beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
  70. // Perform the SSL handshake
  71. stream.async_handshake(ssl::stream_base::client, yield[ec]);
  72. if(ec)
  73. return fail(ec, "handshake");
  74. // Set up an HTTP GET request message
  75. http::request<http::string_body> req{http::verb::get, target, version};
  76. req.set(http::field::host, host);
  77. req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
  78. // Set the timeout.
  79. beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
  80. // Send the HTTP request to the remote host
  81. http::async_write(stream, req, yield[ec]);
  82. if(ec)
  83. return fail(ec, "write");
  84. // This buffer is used for reading and must be persisted
  85. beast::flat_buffer b;
  86. // Declare a container to hold the response
  87. http::response<http::dynamic_body> res;
  88. // Receive the HTTP response
  89. http::async_read(stream, b, res, yield[ec]);
  90. if(ec)
  91. return fail(ec, "read");
  92. // Write the message to standard out
  93. std::cout << res << std::endl;
  94. // Set the timeout.
  95. beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
  96. // Gracefully close the stream
  97. stream.async_shutdown(yield[ec]);
  98. if(ec == net::error::eof)
  99. {
  100. // Rationale:
  101. // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
  102. ec = {};
  103. }
  104. if(ec)
  105. return fail(ec, "shutdown");
  106. // If we get here then the connection is closed gracefully
  107. }
  108. //------------------------------------------------------------------------------
  109. int main(int argc, char** argv)
  110. {
  111. // Check command line arguments.
  112. if(argc != 4 && argc != 5)
  113. {
  114. std::cerr <<
  115. "Usage: http-client-coro-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
  116. "Example:\n" <<
  117. " http-client-coro-ssl www.example.com 443 /\n" <<
  118. " http-client-coro-ssl www.example.com 443 / 1.0\n";
  119. return EXIT_FAILURE;
  120. }
  121. auto const host = argv[1];
  122. auto const port = argv[2];
  123. auto const target = argv[3];
  124. int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
  125. // The io_context is required for all I/O
  126. net::io_context ioc;
  127. // The SSL context is required, and holds certificates
  128. ssl::context ctx{ssl::context::tlsv12_client};
  129. // This holds the root certificate used for verification
  130. load_root_certificates(ctx);
  131. // Verify the remote server's certificate
  132. ctx.set_verify_mode(ssl::verify_peer);
  133. // Launch the asynchronous operation
  134. boost::asio::spawn(ioc, std::bind(
  135. &do_session,
  136. std::string(host),
  137. std::string(port),
  138. std::string(target),
  139. version,
  140. std::ref(ioc),
  141. std::ref(ctx),
  142. std::placeholders::_1));
  143. // Run the I/O service. The call will return when
  144. // the get operation is complete.
  145. ioc.run();
  146. return EXIT_SUCCESS;
  147. }