Performs an HTTP GET and prints the response
| 32 | |
| 33 | // Performs an HTTP GET and prints the response |
| 34 | int main(int argc, char** argv) |
| 35 | { |
| 36 | try |
| 37 | { |
| 38 | // Check command line arguments. |
| 39 | if(argc != 4 && argc != 5) |
| 40 | { |
| 41 | std::cerr << |
| 42 | "Usage: http-client-sync-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" << |
| 43 | "Example:\n" << |
| 44 | " http-client-sync-ssl www.example.com 443 /\n" << |
| 45 | " http-client-sync-ssl www.example.com 443 / 1.0\n"; |
| 46 | return EXIT_FAILURE; |
| 47 | } |
| 48 | auto const host = argv[1]; |
| 49 | auto const port = argv[2]; |
| 50 | auto const target = argv[3]; |
| 51 | int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11; |
| 52 | |
| 53 | // The io_context is required for all I/O |
| 54 | net::io_context ioc; |
| 55 | |
| 56 | // The SSL context is required, and holds certificates |
| 57 | ssl::context ctx(ssl::context::tlsv12_client); |
| 58 | |
| 59 | // This holds the root certificate used for verification |
| 60 | load_root_certificates(ctx); |
| 61 | |
| 62 | // Verify the remote server's certificate |
| 63 | ctx.set_verify_mode(ssl::verify_peer); |
| 64 | |
| 65 | // These objects perform our I/O |
| 66 | tcp::resolver resolver(ioc); |
| 67 | ssl::stream<beast::tcp_stream> stream(ioc, ctx); |
| 68 | |
| 69 | // Set SNI Hostname (many hosts need this to handshake successfully) |
| 70 | if(! SSL_set_tlsext_host_name(stream.native_handle(), host)) |
| 71 | { |
| 72 | throw beast::system_error( |
| 73 | static_cast<int>(::ERR_get_error()), |
| 74 | net::error::get_ssl_category()); |
| 75 | } |
| 76 | |
| 77 | // Set the expected hostname in the peer certificate for verification |
| 78 | stream.set_verify_callback(ssl::host_name_verification(host)); |
| 79 | |
| 80 | // Look up the domain name |
| 81 | auto const results = resolver.resolve(host, port); |
| 82 | |
| 83 | // Make the connection on the IP address we get from a lookup |
| 84 | beast::get_lowest_layer(stream).connect(results); |
| 85 | |
| 86 | // Perform the SSL handshake |
| 87 | stream.handshake(ssl::stream_base::client); |
| 88 | |
| 89 | // Set up an HTTP GET request message |
| 90 | http::request<http::string_body> req{http::verb::get, target, version}; |
| 91 | req.set(http::field::host, host); |
nothing calls this directly
no test coverage detected