-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProxyHandler.cpp
282 lines (257 loc) · 9.22 KB
/
ProxyHandler.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "ProxyHandler.h"
#include "mime_types.hpp"
#include <iterator>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/log/trivial.hpp>
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <stdlib.h>
#include <ctype.h>
namespace Team15 {
namespace server {
ProxyHandler::ProxyHandler():
RequestHandler(),
io_service_()
{
}
RequestHandler::Status
ProxyHandler::Init(const std::string& uri_prefix, NginxConfig config)
{
uri_prefix_ = uri_prefix;
for (auto statement: config.statements_) {
std::string start_token = statement->tokens_[0];
if (start_token == "server_host") {
if (statement->tokens_.size() != 2) return RequestHandler::INVALID_INPUT;
server_host = statement->tokens_[1];
}
else if (start_token == "server_port") {
if (statement->tokens_.size() != 2) return RequestHandler::INVALID_INPUT;
server_port = statement->tokens_[1];
}
}
return RequestHandler::OK;
}
// sync handleRequest
RequestHandler::Status
ProxyHandler::HandleRequest(const Request& request,
Response* response)
{
// construct the request
std::unique_ptr<Request> request_ = CreateProxyRequest(request);
return IOHandle(server_host, server_port, request_.get(), response);
}
RequestHandler::Status
ProxyHandler::IOHandle(const std::string host,
const std::string port,
Request* request,
Response* response)
{
std::string raw_request = request->ToString();
BOOST_LOG_TRIVIAL(trace) << raw_request;
// resolve the server endpoint
boost::system::error_code ec;
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve({host, port}, ec);
if (ec) return HandleError("resolve", ec);
// connect the remmote server
boost::asio::ip::tcp::socket socket_(io_service_);
boost::asio::connect(socket_, endpoint_iterator, ec);
if (ec) return HandleError("connect", ec);
// send the request
boost::asio::streambuf request_stream_buf;
std::ostream request_stream(&request_stream_buf);
request_stream << raw_request;
boost::asio::write(socket_, request_stream_buf, ec);
if (ec) return HandleError("write", ec);
// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf cFonstructor.
boost::asio::streambuf response_stream_buf;
boost::asio::read_until(socket_, response_stream_buf, "\r\n", ec);
if (ec) return HandleError("read_until", ec);
std::string raw_headers = "";
// Check that response is OK.
std::istream response_stream(&response_stream_buf);
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); // contains a space in front of it!
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
BOOST_LOG_TRIVIAL(error) << "Invalid response\n";
// error handler
return RequestHandler::OK;
}
raw_headers += http_version + " " + std::to_string(status_code) + status_message + "\n";
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket_, response_stream_buf, "\r\n\r\n", ec);
if (ec) return HandleError("read_until", ec);
// Process the 302 code
std::string header;
if (status_code == 302) {
BOOST_LOG_TRIVIAL(trace) << "302 found!";
while (std::getline(response_stream, header) && header != "\r") {
if (header.find("Location") != std::string::npos) {
// delete '/r'
header.pop_back();
return HandleRedirect(header, request, response);
}
}
BOOST_LOG_TRIVIAL(error) << "Invalid response\n";
// error handler
return RequestHandler::OK;
}
// process the headers
std::string content_length_str = "";
while (std::getline(response_stream, header) && header != "\r") {
if (header.find("Content-Length") != std::string::npos) {
for (auto c: header) {
if (isdigit(c)) content_length_str += c;
}
}
raw_headers += header + '\n';
}
raw_headers += "\r\n";
response->SetRawHeader(raw_headers);
if (content_length_str == "") {
BOOST_LOG_TRIVIAL(error) << "Invalid 200 responses -- without Content-Length Header!";
// error handler
return RequestHandler::OK;
}
BOOST_LOG_TRIVIAL(trace) << raw_headers;
// some contents may have been received by read_until
// according the read_until doc, it may read pass the delimeter
std::size_t received_content_length = response_stream_buf.size();
std::size_t left_content_length = boost::lexical_cast<std::size_t>(content_length_str) - received_content_length;
boost::asio::read(socket_, response_stream_buf,
boost::asio::transfer_exactly(left_content_length), ec);
if (ec) return HandleError("read", ec);
std::string body;
std::ostringstream ss;
ss << &response_stream_buf;
body = ss.str();
//HandleContent(body);
response->SetBody(body);
return RequestHandler::OK;
}
void
ProxyHandler::HandleContent(const std::string& body)
{/*
// regex = (?:href|src)=(["'])(?!mailto|https).*?\1
boost::cmatch ref_mat;
boost::regex ref_expression ("(href|src)=\"(/.*)\"");
if (!boost::regex_match(raw_request.c_str(), request_mat, request_expression)) {
BOOST_LOG_TRIVIAL(info) << "Invalid request format\n";
return nullptr;
}*/
}
std::unique_ptr<Request>
ProxyHandler::CreateProxyRequest(const Request& request)
{
std::unique_ptr<Request> request_ = std::unique_ptr<Request>(new Request(request));
std::string new_uri = request.uri().substr(uri_prefix_.size());
if (new_uri == "" || new_uri[0] != '/') new_uri = "/" + new_uri;
request_->Seturi(new_uri);
std::map<std::string, std::string> headers = request.GetHeaders();
for (auto header: headers) {
if (header.first == "Host") {
request_->AddHeader("Host", server_host + ":" + server_port);
break;
}
}
return request_;
}
RequestHandler::Status
ProxyHandler::HandleError(const std::string& error_info, const boost::system::error_code& ec)
{
BOOST_LOG_TRIVIAL(trace) << "ProxyHandler: " << error_info << " " << "error_code=" << ec;
return RequestHandler::OK;
}
RequestHandler::Status
ProxyHandler::HandleRedirect(const std::string& location_header,
Request* request,
Response *response)
{
std::string location;
// absolute url
if (location_header.find("http://") != std::string::npos) {
std::string host, port, path, query;
std::size_t host_start = location_header.find("http://") + 7;
std::size_t path_start = location_header.find("/", host_start);
std::string host_and_port;
// process host and port
if (path_start == std::string::npos) host_and_port = location_header.substr(host_start);
else host_and_port = location_header.substr(host_start, path_start - host_start);
std::size_t delimeter_pos = host_and_port.find(":");
if (delimeter_pos == std::string::npos
|| delimeter_pos == host_and_port.size() - 1) {
host = host_and_port;
port = "80";
}
else {
host = host_and_port.substr(0, delimeter_pos);
port = host_and_port.substr(delimeter_pos + 1);
}
// process relative_url
if (path_start == std::string::npos) {
path = "/";
}
else {
std::string relative_url = location_header.substr(path_start);
ParsePathAndQuery(relative_url, path, query);
}
request->Seturi(path);
request->SetBody(query);
// modify Host header
std::map<std::string, std::string> headers = request->GetHeaders();
for (auto header: headers) {
if (header.first == "Host") {
// because headers is a map, so AddHeader is like SetHeader
request->AddHeader("Host", host + ":" + port);
break;
}
}
//std::cout << "host=" << host << " port=" << port << " path=" << path << std::endl;
return IOHandle(host, port, request, response);
} // relative url
else {
// TODO: maybe multiple spaces after url??
std::string path;
std::string query;
std::size_t path_start = location_header.find_last_of(" ");
if (path_start == std::string::npos ||
path_start == location_header.size() - 1) {
path = "/";
}
else {
std::string relative_url;
if (location_header[path_start + 1] != '/')
relative_url = "/" + location_header.substr(path_start + 1);
else relative_url = location_header.substr(path_start + 1);
ParsePathAndQuery(relative_url, path, query);
}
request->Seturi(path);
request->SetBody(query);
return IOHandle(server_host, server_port, request, response);
}
return RequestHandler::OK;
}
void
ProxyHandler::ParsePathAndQuery(const std::string& relative_url,
std::string& path,
std::string& query)
{
std::size_t query_pos = relative_url.find("?");
if (query_pos != std::string::npos) {
path.append(relative_url.substr(0, query_pos));
query.append(relative_url.substr(query_pos + 1));
}
else path.append(relative_url);
}
} // server
} // Team15