async_client.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // async_client.cpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <iostream>
  11. #include <istream>
  12. #include <ostream>
  13. #include <string>
  14. #include <boost/asio.hpp>
  15. #include <boost/bind.hpp>
  16. using boost::asio::ip::tcp;
  17. class client
  18. {
  19. public:
  20. client(boost::asio::io_service& io_service, const std::string& server,
  21. const std::string& path) :
  22. resolver_(io_service), socket_(io_service)
  23. {
  24. // Form the request. We specify the "Connection: close" header so that the
  25. // server will close the socket after transmitting the response. This will
  26. // allow us to treat all data up until the EOF as the content.
  27. std::ostream request_stream(&request_);
  28. request_stream << "GET " << path << " HTTP/1.0\r\n";
  29. request_stream << "Host: " << server << "\r\n";
  30. request_stream << "Accept: */*\r\n";
  31. request_stream << "Connection: close\r\n\r\n";
  32. // Start an asynchronous resolve to translate the server and service names
  33. // into a list of endpoints.
  34. tcp::resolver::query query(server, "http");
  35. resolver_.async_resolve(query, boost::bind(&client::handle_resolve,
  36. this, boost::asio::placeholders::error,
  37. boost::asio::placeholders::iterator));
  38. }
  39. private:
  40. void handle_resolve(const boost::system::error_code& err,
  41. tcp::resolver::iterator endpoint_iterator)
  42. {
  43. if (!err)
  44. {
  45. // Attempt a connection to the first endpoint in the list. Each endpoint
  46. // will be tried until we successfully establish a connection.
  47. tcp::endpoint endpoint = *endpoint_iterator;
  48. socket_.async_connect(endpoint, boost::bind(
  49. &client::handle_connect, this,
  50. boost::asio::placeholders::error, ++endpoint_iterator));
  51. }
  52. else
  53. {
  54. std::cout << "Error: " << err.message() << "\n";
  55. }
  56. }
  57. void handle_connect(const boost::system::error_code& err,
  58. tcp::resolver::iterator endpoint_iterator)
  59. {
  60. if (!err)
  61. {
  62. // The connection was successful. Send the request.
  63. boost::asio::async_write(socket_, request_, boost::bind(
  64. &client::handle_write_request, this,
  65. boost::asio::placeholders::error));
  66. }
  67. else if (endpoint_iterator != tcp::resolver::iterator())
  68. {
  69. // The connection failed. Try the next endpoint in the list.
  70. socket_.close();
  71. tcp::endpoint endpoint = *endpoint_iterator;
  72. socket_.async_connect(endpoint, boost::bind(
  73. &client::handle_connect, this,
  74. boost::asio::placeholders::error, ++endpoint_iterator));
  75. }
  76. else
  77. {
  78. std::cout << "Error: " << err.message() << "\n";
  79. }
  80. }
  81. void handle_write_request(const boost::system::error_code& err)
  82. {
  83. if (!err)
  84. {
  85. // Read the response status line.
  86. boost::asio::async_read_until(socket_, response_, "\r\n",
  87. boost::bind(&client::handle_read_status_line, this,
  88. boost::asio::placeholders::error));
  89. }
  90. else
  91. {
  92. std::cout << "Error: " << err.message() << "\n";
  93. }
  94. }
  95. void handle_read_status_line(const boost::system::error_code& err)
  96. {
  97. if (!err)
  98. {
  99. // Check that response is OK.
  100. std::istream response_stream(&response_);
  101. std::string http_version;
  102. response_stream >> http_version;
  103. unsigned int status_code;
  104. response_stream >> status_code;
  105. std::string status_message;
  106. std::getline(response_stream, status_message);
  107. if (!response_stream || http_version.substr(0, 5) != "HTTP/")
  108. {
  109. std::cout << "Invalid response\n";
  110. return;
  111. }
  112. if (status_code != 200)
  113. {
  114. std::cout << "Response returned with status code ";
  115. std::cout << status_code << "\n";
  116. return;
  117. }
  118. // Read the response headers, which are terminated by a blank line.
  119. boost::asio::async_read_until(socket_, response_, "\r\n\r\n",
  120. boost::bind(&client::handle_read_headers, this,
  121. boost::asio::placeholders::error));
  122. }
  123. else
  124. {
  125. std::cout << "Error: " << err << "\n";
  126. }
  127. }
  128. void handle_read_headers(const boost::system::error_code& err)
  129. {
  130. if (!err)
  131. {
  132. // Process the response headers.
  133. std::istream response_stream(&response_);
  134. std::string header;
  135. while (std::getline(response_stream, header) && header != "\r")
  136. std::cout << header << "\n";
  137. std::cout << "\n";
  138. // Write whatever content we already have to output.
  139. if (response_.size() > 0)
  140. std::cout << &response_;
  141. // Start reading remaining data until EOF.
  142. boost::asio::async_read(socket_, response_,
  143. boost::asio::transfer_at_least(1), boost::bind(
  144. &client::handle_read_content, this,
  145. boost::asio::placeholders::error));
  146. }
  147. else
  148. {
  149. std::cout << "Error: " << err << "\n";
  150. }
  151. }
  152. void handle_read_content(const boost::system::error_code& err)
  153. {
  154. if (!err)
  155. {
  156. // Write all of the data that has been read so far.
  157. std::cout << &response_;
  158. // Continue reading remaining data until EOF.
  159. boost::asio::async_read(socket_, response_,
  160. boost::asio::transfer_at_least(1), boost::bind(
  161. &client::handle_read_content, this,
  162. boost::asio::placeholders::error));
  163. }
  164. else if (err != boost::asio::error::eof)
  165. {
  166. std::cout << "Error: " << err << "\n";
  167. }
  168. }
  169. tcp::resolver resolver_;
  170. tcp::socket socket_;
  171. boost::asio::streambuf request_;
  172. boost::asio::streambuf response_;
  173. };
  174. int main(int argc, char* argv[])
  175. {
  176. try
  177. {
  178. if (argc != 3)
  179. {
  180. std::cout << "Usage: async_client <server> <path>\n";
  181. std::cout << "Example:\n";
  182. std::cout << " async_client www.boost.org /LICENSE_1_0.txt\n";
  183. return 1;
  184. }
  185. boost::asio::io_service io_service;
  186. client c(io_service, argv[1], argv[2]);
  187. io_service.run();
  188. }
  189. catch (std::exception& e)
  190. {
  191. std::cout << "Exception: " << e.what() << "\n";
  192. }
  193. return 0;
  194. }