]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/http/client/sync_client.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / http / client / sync_client.cpp
CommitLineData
7c673cae
FG
1//
2// sync_client.cpp
3// ~~~~~~~~~~~~~~~
4//
b32b8144 5// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7c673cae
FG
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
17using boost::asio::ip::tcp;
18
19int 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
b32b8144 31 boost::asio::io_context io_context;
7c673cae
FG
32
33 // Get a list of endpoints corresponding to the server name.
b32b8144
FG
34 tcp::resolver resolver(io_context);
35 tcp::resolver::results_type endpoints = resolver.resolve(argv[1], "http");
7c673cae
FG
36
37 // Try each endpoint until we successfully establish a connection.
b32b8144
FG
38 tcp::socket socket(io_context);
39 boost::asio::connect(socket, endpoints);
7c673cae
FG
40
41 // Form the request. We specify the "Connection: close" header so that the
42 // server will close the socket after transmitting the response. This will
43 // allow us to treat all data up until the EOF as the content.
44 boost::asio::streambuf request;
45 std::ostream request_stream(&request);
46 request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
47 request_stream << "Host: " << argv[1] << "\r\n";
48 request_stream << "Accept: */*\r\n";
49 request_stream << "Connection: close\r\n\r\n";
50
51 // Send the request.
52 boost::asio::write(socket, request);
53
54 // Read the response status line. The response streambuf will automatically
55 // grow to accommodate the entire line. The growth may be limited by passing
56 // a maximum size to the streambuf constructor.
57 boost::asio::streambuf response;
58 boost::asio::read_until(socket, response, "\r\n");
59
60 // Check that response is OK.
61 std::istream response_stream(&response);
62 std::string http_version;
63 response_stream >> http_version;
64 unsigned int status_code;
65 response_stream >> status_code;
66 std::string status_message;
67 std::getline(response_stream, status_message);
68 if (!response_stream || http_version.substr(0, 5) != "HTTP/")
69 {
70 std::cout << "Invalid response\n";
71 return 1;
72 }
73 if (status_code != 200)
74 {
75 std::cout << "Response returned with status code " << status_code << "\n";
76 return 1;
77 }
78
79 // Read the response headers, which are terminated by a blank line.
80 boost::asio::read_until(socket, response, "\r\n\r\n");
81
82 // Process the response headers.
83 std::string header;
84 while (std::getline(response_stream, header) && header != "\r")
85 std::cout << header << "\n";
86 std::cout << "\n";
87
88 // Write whatever content we already have to output.
89 if (response.size() > 0)
90 std::cout << &response;
91
92 // Read until EOF, writing data to output as we go.
93 boost::system::error_code error;
94 while (boost::asio::read(socket, response,
95 boost::asio::transfer_at_least(1), error))
96 std::cout << &response;
97 if (error != boost::asio::error::eof)
98 throw boost::system::system_error(error);
99 }
100 catch (std::exception& e)
101 {
102 std::cout << "Exception: " << e.what() << "\n";
103 }
104
105 return 0;
106}