5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
15 #include <boost/asio.hpp>
17 using boost::asio::ip::tcp
;
19 int main(int argc
, char* argv
[])
25 std::cout
<< "Usage: sync_client <server> <path>\n";
26 std::cout
<< "Example:\n";
27 std::cout
<< " sync_client www.boost.org /LICENSE_1_0.txt\n";
31 boost::asio::io_service io_service
;
33 // Get a list of endpoints corresponding to the server name.
34 tcp::resolver
resolver(io_service
);
35 tcp::resolver::query
query(argv
[1], "http");
36 tcp::resolver::iterator endpoint_iterator
= resolver
.resolve(query
);
38 // Try each endpoint until we successfully establish a connection.
39 tcp::socket
socket(io_service
);
40 boost::asio::connect(socket
, endpoint_iterator
);
42 // Form the request. We specify the "Connection: close" header so that the
43 // server will close the socket after transmitting the response. This will
44 // allow us to treat all data up until the EOF as the content.
45 boost::asio::streambuf request
;
46 std::ostream
request_stream(&request
);
47 request_stream
<< "GET " << argv
[2] << " HTTP/1.0\r\n";
48 request_stream
<< "Host: " << argv
[1] << "\r\n";
49 request_stream
<< "Accept: */*\r\n";
50 request_stream
<< "Connection: close\r\n\r\n";
53 boost::asio::write(socket
, request
);
55 // Read the response status line. The response streambuf will automatically
56 // grow to accommodate the entire line. The growth may be limited by passing
57 // a maximum size to the streambuf constructor.
58 boost::asio::streambuf response
;
59 boost::asio::read_until(socket
, response
, "\r\n");
61 // Check that response is OK.
62 std::istream
response_stream(&response
);
63 std::string http_version
;
64 response_stream
>> http_version
;
65 unsigned int status_code
;
66 response_stream
>> status_code
;
67 std::string status_message
;
68 std::getline(response_stream
, status_message
);
69 if (!response_stream
|| http_version
.substr(0, 5) != "HTTP/")
71 std::cout
<< "Invalid response\n";
74 if (status_code
!= 200)
76 std::cout
<< "Response returned with status code " << status_code
<< "\n";
80 // Read the response headers, which are terminated by a blank line.
81 boost::asio::read_until(socket
, response
, "\r\n\r\n");
83 // Process the response headers.
85 while (std::getline(response_stream
, header
) && header
!= "\r")
86 std::cout
<< header
<< "\n";
89 // Write whatever content we already have to output.
90 if (response
.size() > 0)
91 std::cout
<< &response
;
93 // Read until EOF, writing data to output as we go.
94 boost::system::error_code error
;
95 while (boost::asio::read(socket
, response
,
96 boost::asio::transfer_at_least(1), error
))
97 std::cout
<< &response
;
98 if (error
!= boost::asio::error::eof
)
99 throw boost::system::system_error(error
);
101 catch (std::exception
& e
)
103 std::cout
<< "Exception: " << e
.what() << "\n";