|
| 1 | +// |
| 2 | +// Copyright (c) 2025 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/cppalliance/http_io |
| 8 | +// |
| 9 | + |
| 10 | +#include <cstdlib> |
| 11 | +#include <boost/asio/basic_stream_socket.hpp> |
| 12 | +#include <boost/asio/connect.hpp> |
| 13 | +#include <boost/asio/io_context.hpp> |
| 14 | +#include <boost/asio/ip/tcp.hpp> |
| 15 | +#include <boost/core/detail/string_view.hpp> |
| 16 | +#include <boost/http_proto/context.hpp> |
| 17 | +#include <boost/http_proto/request.hpp> |
| 18 | +#include <boost/http_proto/response_parser.hpp> |
| 19 | +#include <boost/url/url_view.hpp> |
| 20 | + |
| 21 | +struct worker |
| 22 | +{ |
| 23 | + using executor_type = |
| 24 | + boost::asio::io_context::executor_type; |
| 25 | + using resolver_type = |
| 26 | + boost::asio::ip::basic_resolver< |
| 27 | + boost::asio::ip::tcp, executor_type>; |
| 28 | + using socket_type = boost::asio::basic_stream_socket< |
| 29 | + boost::asio::ip::tcp, executor_type>; |
| 30 | + |
| 31 | + socket_type sock; |
| 32 | + resolver_type resolver; |
| 33 | + boost::http_proto::response_parser pr; |
| 34 | + boost::urls::url_view url; |
| 35 | + |
| 36 | + explicit |
| 37 | + worker( |
| 38 | + executor_type ex, |
| 39 | + boost::http_proto::context& ctx) |
| 40 | + : sock(ex) |
| 41 | + , resolver(ex) |
| 42 | + , pr(ctx) |
| 43 | + { |
| 44 | + sock.open(boost::asio::ip::tcp::v4()); |
| 45 | + } |
| 46 | + |
| 47 | + void |
| 48 | + do_next() |
| 49 | + { |
| 50 | + do_visit("http://www.boost.org"); |
| 51 | + } |
| 52 | + |
| 53 | + void |
| 54 | + do_visit(boost::urls::url_view url_) |
| 55 | + { |
| 56 | + url = url_; |
| 57 | + do_resolve(); |
| 58 | + } |
| 59 | + |
| 60 | + void |
| 61 | + do_resolve() |
| 62 | + { |
| 63 | + resolver.async_resolve( |
| 64 | + url.encoded_host(), |
| 65 | + url.scheme(), |
| 66 | + [&]( |
| 67 | + boost::system::error_code ec, |
| 68 | + resolver_type::results_type results) |
| 69 | + { |
| 70 | + if(ec.failed()) |
| 71 | + { |
| 72 | + // log (target, ec.message()) |
| 73 | + auto s = ec.message(); |
| 74 | + return do_next(); |
| 75 | + } |
| 76 | + do_connect(results); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + void |
| 81 | + do_connect( |
| 82 | + resolver_type::results_type results) |
| 83 | + { |
| 84 | + boost::asio::async_connect( |
| 85 | + sock, |
| 86 | + results.begin(), |
| 87 | + results.end(), |
| 88 | + [&]( |
| 89 | + boost::system::error_code ec, |
| 90 | + resolver_type::results_type::const_iterator it) |
| 91 | + { |
| 92 | + if(ec.failed()) |
| 93 | + { |
| 94 | + // log (target, ec.message()) |
| 95 | + return do_next(); |
| 96 | + } |
| 97 | + do_request(); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + void |
| 102 | + do_request() |
| 103 | + { |
| 104 | + boost::http_proto::request req; |
| 105 | + auto path = url.encoded_path(); |
| 106 | + req.set_start_line( |
| 107 | + boost::http_proto::method::get, |
| 108 | + path.empty() ? "/" : path, |
| 109 | + boost::http_proto::version::http_1_1); |
| 110 | + |
| 111 | + do_shutdown(); |
| 112 | + } |
| 113 | + |
| 114 | + void |
| 115 | + do_shutdown() |
| 116 | + { |
| 117 | + boost::system::error_code ec; |
| 118 | + sock.shutdown(socket_type::shutdown_both, ec); |
| 119 | + if(ec.failed()) |
| 120 | + { |
| 121 | + // log(ec.message()) |
| 122 | + return do_next(); |
| 123 | + } |
| 124 | + do_next(); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +int |
| 129 | +main(int argc, char* argv[]) |
| 130 | +{ |
| 131 | + boost::http_proto::context ctx; |
| 132 | + boost::http_proto::parser::config_base cfg; |
| 133 | + boost::http_proto::install_parser_service(ctx, cfg); |
| 134 | + |
| 135 | + boost::asio::io_context ioc; |
| 136 | + |
| 137 | + worker w(ioc.get_executor(), ctx); |
| 138 | + |
| 139 | + w.do_next(); |
| 140 | + |
| 141 | + ioc.run(); |
| 142 | + |
| 143 | + return EXIT_SUCCESS; |
| 144 | +} |
0 commit comments