sync_client.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // sync_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. using boost::asio::ip::tcp;
  16. int main(int argc, char* argv[])
  17. {
  18. try
  19. {
  20. if (argc != 3)
  21. {
  22. std::cout << "Usage: sync_client <server> <path>\n";
  23. std::cout << "Example:\n";
  24. std::cout << " sync_client www.boost.org /LICENSE_1_0.txt\n";
  25. return 1;
  26. }
  27. boost::asio::io_service io_service;
  28. // Get a list of endpoints corresponding to the server name.
  29. tcp::resolver resolver(io_service);
  30. tcp::resolver::query query(argv[1], "http");
  31. tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  32. tcp::resolver::iterator end;
  33. // Try each endpoint until we successfully establish a connection.
  34. tcp::socket socket(io_service);
  35. boost::system::error_code error = boost::asio::error::host_not_found;
  36. while (error && endpoint_iterator != end)
  37. {
  38. socket.close();
  39. socket.connect(*endpoint_iterator++, error);
  40. }
  41. if (error)
  42. throw boost::system::system_error(error);
  43. // Form the request. We specify the "Connection: close" header so that the
  44. // server will close the socket after transmitting the response. This will
  45. // allow us to treat all data up until the EOF as the content.
  46. boost::asio::streambuf request;
  47. std::ostream request_stream(&request);
  48. request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
  49. request_stream << "Host: " << argv[1] << "\r\n";
  50. request_stream << "Accept: */*\r\n";
  51. request_stream << "Connection: close\r\n\r\n";
  52. // Send the request.
  53. boost::asio::write(socket, request);
  54. // Read the response status line.
  55. boost::asio::streambuf response;
  56. boost::asio::read_until(socket, response, "\r\n");
  57. // Check that response is OK.
  58. std::istream response_stream(&response);
  59. std::string http_version;
  60. response_stream >> http_version;
  61. unsigned int status_code;
  62. response_stream >> status_code;
  63. std::string status_message;
  64. std::getline(response_stream, status_message);
  65. if (!response_stream || http_version.substr(0, 5) != "HTTP/")
  66. {
  67. std::cout << "Invalid response\n";
  68. return 1;
  69. }
  70. if (status_code != 200)
  71. {
  72. std::cout << "Response returned with status code " << status_code
  73. << "\n";
  74. return 1;
  75. }
  76. // Read the response headers, which are terminated by a blank line.
  77. boost::asio::read_until(socket, response, "\r\n\r\n");
  78. // Process the response headers.
  79. std::string header;
  80. while (std::getline(response_stream, header) && header != "\r")
  81. std::cout << header << "\n";
  82. std::cout << "\n";
  83. // Write whatever content we already have to output.
  84. if (response.size() > 0)
  85. std::cout << &response;
  86. // Read until EOF, writing data to output as we go.
  87. while (boost::asio::read(socket, response,
  88. boost::asio::transfer_at_least(1), error))
  89. std::cout << &response;
  90. if (error != boost::asio::error::eof)
  91. throw boost::system::system_error(error);
  92. }
  93. catch (std::exception& e)
  94. {
  95. std::cout << "Exception: " << e.what() << "\n";
  96. }
  97. return 0;
  98. }