-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPClient.cpp
213 lines (171 loc) · 4.5 KB
/
HTTPClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "HTTPClient.h"
HTTPClient::HTTPClient(boost::asio::io_service& service,
const std::string& remoteHost, unsigned int port)
: io_service_()
, socket_(io_service_)
, remote_host_(remoteHost)
{
/*
boost::asio::socket_base::debug option(true);
socket_.set_option(option);
*/
boost::asio::ip::tcp::resolver r(io_service_);
boost::asio::ip::tcp::resolver::query q(remoteHost,
boost::lexical_cast<std::string>(port));
boost::system::error_code ec;
boost::asio::connect(socket_, r.resolve(q), ec);
BOOST_LOG_TRIVIAL(debug) << "open connection to " << remote_host_;
if(ec)
{
//an error occured
BOOST_LOG_TRIVIAL(error) << ec.message();
}
else
{
BOOST_LOG_TRIVIAL(info) << ec.message();
}
}
HTTPClient::~HTTPClient()
{
this->closeConnection();
}
bool HTTPClient::isOpen()
{
return socket_.is_open();
}
void HTTPClient::closeConnection()
{
if(socket_.is_open())
{
socket_.close();
boost::system::error_code ec;
BOOST_LOG_TRIVIAL(debug) << "closing connection to " << remote_host_;
if(ec)
BOOST_LOG_TRIVIAL(error) << ec.message();
else
BOOST_LOG_TRIVIAL(info) << ec.message();
}
}
#include <iostream>
void HTTPClient::sendRequest(const std::string& data)
{
if(socket_.is_open())
{
BOOST_LOG_TRIVIAL(debug) << "send request to " << remote_host_;
boost::asio::async_write(socket_,
boost::asio::buffer(data, data.size()),
boost::bind(
&HTTPClient::writeHandler,
this,
boost::asio::placeholders::error));
socket_.get_io_service().poll_one();
}
}
void HTTPClient::writeHandler(const boost::system::error_code& ec)
{
BOOST_LOG_TRIVIAL(debug) << "handling write request from " << remote_host_;
if(!ec)
{
BOOST_LOG_TRIVIAL(info) << ec.message();
boost::asio::async_read_until(socket_, response_, "\r\n",
boost::bind(
&HTTPClient::readStatusLineHandler,
this,
boost::asio::placeholders::error));
socket_.get_io_service().run_one();
}
else
{
BOOST_LOG_TRIVIAL(error) << ec.message();
}
}
void HTTPClient::readStatusLineHandler(const boost::system::error_code& ec)
{
BOOST_LOG_TRIVIAL(debug) << "handling read status line from " << remote_host_;
if(!ec)
{
BOOST_LOG_TRIVIAL(info) << ec.message();
std::istream response_stream(&response_);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if(!response_stream || http_version.substr(0, 5) != "HTTP/")
{
BOOST_LOG_TRIVIAL(error) << "Invalid http response from " << remote_host_;
return;
}
if(status_code != 200)
{
BOOST_LOG_TRIVIAL(error) << "Response status code is " << status_code;
return;
}
boost::asio::async_read_until(socket_, response_, "\r\n\r\n",
boost::bind(&HTTPClient::readHeadersHandler,
this,
boost::asio::placeholders::error));
socket_.get_io_service().run_one();
}
else
{
BOOST_LOG_TRIVIAL(error) << ec.message();
}
}
void HTTPClient::readHeadersHandler(const boost::system::error_code& ec)
{
BOOST_LOG_TRIVIAL(debug) << "handling read status line from " << remote_host_;
if(!ec)
{
BOOST_LOG_TRIVIAL(info) << ec.message();
std::istream response_stream(&response_);
std::string header;
while (std::getline(response_stream, header) && header != "\r")
//std::cout << header << "\n";
response_.consume(header.length());
//std::cout << "\n";
/*
// Write whatever content we already have to output.
if (response_.size() > 0)
std::cout << &response_;
*/
boost::asio::async_read(socket_, response_,
boost::asio::transfer_at_least(1),
boost::bind(&HTTPClient::readContentHandler,
this,
boost::asio::placeholders::error));
socket_.get_io_service().run_one();
}
else
{
BOOST_LOG_TRIVIAL(error) << ec.message();
}
}
void HTTPClient::readContentHandler(const boost::system::error_code& ec)
{
BOOST_LOG_TRIVIAL(debug) << "handling read content from " << remote_host_;
if(!ec)
{
BOOST_LOG_TRIVIAL(info) << ec.message();
boost::asio::async_read(socket_, response_,
boost::asio::transfer_at_least(1),
boost::bind(&HTTPClient::readContentHandler,
this,
boost::asio::placeholders::error));
socket_.get_io_service().run_one();
}
else if(ec != boost::asio::error::eof)
{
BOOST_LOG_TRIVIAL(debug) << ec.message();
}
else
{
BOOST_LOG_TRIVIAL(error) << ec.message();
}
}
boost::asio::streambuf& HTTPClient::getBuffer()
{
BOOST_LOG_TRIVIAL(trace) << "returned buffer from " << remote_host_;
return response_;
}