http_client_coro.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Example: HTTP client, coroutine
  12. //
  13. //------------------------------------------------------------------------------
  14. #include <boost/beast/core.hpp>
  15. #include <boost/beast/http.hpp>
  16. #include <boost/beast/version.hpp>
  17. #include <boost/asio/spawn.hpp>
  18. #include <cstdlib>
  19. #include <functional>
  20. #include <iostream>
  21. #include <string>
  22. namespace beast = boost::beast; // from <boost/beast.hpp>
  23. namespace http = beast::http; // from <boost/beast/http.hpp>
  24. namespace net = boost::asio; // from <boost/asio.hpp>
  25. using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
  26. //------------------------------------------------------------------------------
  27. // Report a failure
  28. void
  29. fail(beast::error_code ec, char const* what)
  30. {
  31. std::cerr << what << ": " << ec.message() << "\n";
  32. }
  33. // Performs an HTTP GET and prints the response
  34. void
  35. do_session(
  36. std::string const& host,
  37. std::string const& port,
  38. std::string const& target,
  39. int version,
  40. net::io_context& ioc,
  41. net::yield_context yield)
  42. {
  43. beast::error_code ec;
  44. // These objects perform our I/O
  45. tcp::resolver resolver(ioc);
  46. beast::tcp_stream stream(ioc);
  47. // Look up the domain name
  48. auto const results = resolver.async_resolve(host, port, yield[ec]);
  49. if(ec)
  50. return fail(ec, "resolve");
  51. // Set the timeout.
  52. stream.expires_after(std::chrono::seconds(30));
  53. // Make the connection on the IP address we get from a lookup
  54. stream.async_connect(results, yield[ec]);
  55. if(ec)
  56. return fail(ec, "connect");
  57. // Set up an HTTP GET request message
  58. http::request<http::string_body> req{http::verb::get, target, version};
  59. req.set(http::field::host, host);
  60. req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
  61. // Set the timeout.
  62. stream.expires_after(std::chrono::seconds(30));
  63. // Send the HTTP request to the remote host
  64. http::async_write(stream, req, yield[ec]);
  65. if(ec)
  66. return fail(ec, "write");
  67. // This buffer is used for reading and must be persisted
  68. beast::flat_buffer b;
  69. // Declare a container to hold the response
  70. http::response<http::dynamic_body> res;
  71. // Receive the HTTP response
  72. http::async_read(stream, b, res, yield[ec]);
  73. if(ec)
  74. return fail(ec, "read");
  75. // Write the message to standard out
  76. std::cout << res << std::endl;
  77. // Gracefully close the socket
  78. stream.socket().shutdown(tcp::socket::shutdown_both, ec);
  79. // not_connected happens sometimes
  80. // so don't bother reporting it.
  81. //
  82. if(ec && ec != beast::errc::not_connected)
  83. return fail(ec, "shutdown");
  84. // If we get here then the connection is closed gracefully
  85. }
  86. //------------------------------------------------------------------------------
  87. int main(int argc, char** argv)
  88. {
  89. // Check command line arguments.
  90. if(argc != 4 && argc != 5)
  91. {
  92. std::cerr <<
  93. "Usage: http-client-coro <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
  94. "Example:\n" <<
  95. " http-client-coro www.example.com 80 /\n" <<
  96. " http-client-coro www.example.com 80 / 1.0\n";
  97. return EXIT_FAILURE;
  98. }
  99. auto const host = argv[1];
  100. auto const port = argv[2];
  101. auto const target = argv[3];
  102. int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
  103. // The io_context is required for all I/O
  104. net::io_context ioc;
  105. // Launch the asynchronous operation
  106. boost::asio::spawn(ioc, std::bind(
  107. &do_session,
  108. std::string(host),
  109. std::string(port),
  110. std::string(target),
  111. version,
  112. std::ref(ioc),
  113. std::placeholders::_1));
  114. // Run the I/O service. The call will return when
  115. // the get operation is complete.
  116. ioc.run();
  117. return EXIT_SUCCESS;
  118. }