| 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 | // |
| 12 | // Example: HTTP client, coroutine |
| 13 | // |
| 14 | //------------------------------------------------------------------------------ |
| 15 | |
| 16 | #include <boost/beast/core.hpp> |
| 17 | #include <boost/beast/http.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 | |
| 25 | namespace beast = boost::beast; // from <boost/beast.hpp> |
| 26 | namespace http = beast::http; // from <boost/beast/http.hpp> |
| 27 | namespace net = boost::asio; // from <boost/asio.hpp> |
| 28 | using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> |
| 29 | |
| 30 | //------------------------------------------------------------------------------ |
| 31 | |
| 32 | // Report a failure |
| 33 | void |
| 34 | fail(beast::error_code ec, char const* what) |
| 35 | { |
| 36 | std::cerr << what << ": " << ec.message() << "\n" ; |
| 37 | } |
| 38 | |
| 39 | // Performs an HTTP GET and prints the response |
| 40 | void |
| 41 | do_session( |
| 42 | std::string const& host, |
| 43 | std::string const& port, |
| 44 | std::string const& target, |
| 45 | int version, |
| 46 | net::io_context& ioc, |
| 47 | net::yield_context yield) |
| 48 | { |
| 49 | beast::error_code ec; |
| 50 | |
| 51 | // These objects perform our I/O |
| 52 | tcp::resolver resolver(ioc); |
| 53 | beast::tcp_stream stream(ioc); |
| 54 | |
| 55 | // Look up the domain name |
| 56 | auto const results = resolver.async_resolve(host, service: port, token: yield[ec]); |
| 57 | if(ec) |
| 58 | return fail(ec, what: "resolve" ); |
| 59 | |
| 60 | // Set the timeout. |
| 61 | stream.expires_after(expiry_time: std::chrono::seconds(30)); |
| 62 | |
| 63 | // Make the connection on the IP address we get from a lookup |
| 64 | stream.async_connect(endpoints: results, handler: yield[ec]); |
| 65 | if(ec) |
| 66 | return fail(ec, what: "connect" ); |
| 67 | |
| 68 | // Set up an HTTP GET request message |
| 69 | http::request<http::string_body> req{http::verb::get, target, version}; |
| 70 | req.set(name: http::field::host, value: host); |
| 71 | req.set(name: http::field::user_agent, BOOST_BEAST_VERSION_STRING); |
| 72 | |
| 73 | // Set the timeout. |
| 74 | stream.expires_after(expiry_time: std::chrono::seconds(30)); |
| 75 | |
| 76 | // Send the HTTP request to the remote host |
| 77 | http::async_write(stream, msg: req, handler: yield[ec]); |
| 78 | if(ec) |
| 79 | return fail(ec, what: "write" ); |
| 80 | |
| 81 | // This buffer is used for reading and must be persisted |
| 82 | beast::flat_buffer b; |
| 83 | |
| 84 | // Declare a container to hold the response |
| 85 | http::response<http::dynamic_body> res; |
| 86 | |
| 87 | // Receive the HTTP response |
| 88 | http::async_read(stream, buffer&: b, msg&: res, handler: yield[ec]); |
| 89 | if(ec) |
| 90 | return fail(ec, what: "read" ); |
| 91 | |
| 92 | // Write the message to standard out |
| 93 | std::cout << res << std::endl; |
| 94 | |
| 95 | // Gracefully close the socket |
| 96 | stream.socket().shutdown(what: tcp::socket::shutdown_both, ec); |
| 97 | |
| 98 | // not_connected happens sometimes |
| 99 | // so don't bother reporting it. |
| 100 | // |
| 101 | if(ec && ec != beast::errc::not_connected) |
| 102 | return fail(ec, what: "shutdown" ); |
| 103 | |
| 104 | // If we get here then the connection is closed gracefully |
| 105 | } |
| 106 | |
| 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 <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" << |
| 116 | "Example:\n" << |
| 117 | " http-client-coro www.example.com 80 /\n" << |
| 118 | " http-client-coro www.example.com 80 / 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(s1: "1.0" , s2: argv[4]) ? 10 : 11; |
| 125 | |
| 126 | // The io_context is required for all I/O |
| 127 | net::io_context ioc; |
| 128 | |
| 129 | // Launch the asynchronous operation |
| 130 | boost::asio::spawn(ctx&: ioc, function: std::bind( |
| 131 | f: &do_session, |
| 132 | args: std::string(host), |
| 133 | args: std::string(port), |
| 134 | args: std::string(target), |
| 135 | args&: version, |
| 136 | args: std::ref(t&: ioc), |
| 137 | args: std::placeholders::_1), |
| 138 | // on completion, spawn will call this function |
| 139 | token: [](std::exception_ptr ex) |
| 140 | { |
| 141 | // if an exception occurred in the coroutine, |
| 142 | // it's something critical, e.g. out of memory |
| 143 | // we capture normal errors in the ec |
| 144 | // so we just rethrow the exception here, |
| 145 | // which will cause `ioc.run()` to throw |
| 146 | if (ex) |
| 147 | std::rethrow_exception(ex); |
| 148 | }); |
| 149 | |
| 150 | // Run the I/O service. The call will return when |
| 151 | // the get operation is complete. |
| 152 | ioc.run(); |
| 153 | |
| 154 | return EXIT_SUCCESS; |
| 155 | } |
| 156 | |