]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/http/client/sync_client.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / http / client / sync_client.cpp
1 //
2 // sync_client.cpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2016 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
11 #include <iostream>
12 #include <istream>
13 #include <ostream>
14 #include <string>
15 #include <boost/asio.hpp>
16
17 using boost::asio::ip::tcp;
18
19 int main(int argc, char* argv[])
20 {
21 try
22 {
23 if (argc != 3)
24 {
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";
28 return 1;
29 }
30
31 boost::asio::io_service io_service;
32
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);
37
38 // Try each endpoint until we successfully establish a connection.
39 tcp::socket socket(io_service);
40 boost::asio::connect(socket, endpoint_iterator);
41
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";
51
52 // Send the request.
53 boost::asio::write(socket, request);
54
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");
60
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/")
70 {
71 std::cout << "Invalid response\n";
72 return 1;
73 }
74 if (status_code != 200)
75 {
76 std::cout << "Response returned with status code " << status_code << "\n";
77 return 1;
78 }
79
80 // Read the response headers, which are terminated by a blank line.
81 boost::asio::read_until(socket, response, "\r\n\r\n");
82
83 // Process the response headers.
84 std::string header;
85 while (std::getline(response_stream, header) && header != "\r")
86 std::cout << header << "\n";
87 std::cout << "\n";
88
89 // Write whatever content we already have to output.
90 if (response.size() > 0)
91 std::cout << &response;
92
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);
100 }
101 catch (std::exception& e)
102 {
103 std::cout << "Exception: " << e.what() << "\n";
104 }
105
106 return 0;
107 }