| 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("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 | // on completion, spawn will call this function |
| 139 | [](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 | } |