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
fail(beast::error_code ec,char const * what)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
do_session(std::string const & host,std::string const & port,std::string const & target,int version,net::io_context & ioc,net::yield_context yield)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, port, yield[ec]);
57 if(ec)
58 return fail(ec, "resolve");
59
60 // Set the timeout.
61 stream.expires_after(std::chrono::seconds(30));
62
63 // Make the connection on the IP address we get from a lookup
64 stream.async_connect(results, yield[ec]);
65 if(ec)
66 return fail(ec, "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(http::field::host, host);
71 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
72
73 // Set the timeout.
74 stream.expires_after(std::chrono::seconds(30));
75
76 // Send the HTTP request to the remote host
77 http::async_write(stream, req, yield[ec]);
78 if(ec)
79 return fail(ec, "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, b, res, yield[ec]);
89 if(ec)
90 return fail(ec, "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(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, "shutdown");
103
104 // If we get here then the connection is closed gracefully
105 }
106
107 //------------------------------------------------------------------------------
108
main(int argc,char ** argv)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("1.0", 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(ioc, std::bind(
131 &do_session,
132 std::string(host),
133 std::string(port),
134 std::string(target),
135 version,
136 std::ref(ioc),
137 std::placeholders::_1));
138
139 // Run the I/O service. The call will return when
140 // the get operation is complete.
141 ioc.run();
142
143 return EXIT_SUCCESS;
144 }
145