-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cc
164 lines (141 loc) · 5.8 KB
/
server.cc
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
/* Source:
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/echo/blocking_tcp_echo_server.cpp
*/
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <utility>
#include <thread>
#include "server.h"
#include "status_counter.h"
using boost::asio::ip::tcp;
Server::Server(boost::asio::io_service& io_service, NginxConfig* config)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), std::stoi(config->getConfigValue("port")))),
config_(config) {
create_handler_map(config_);
}
Server::~Server() {}
// Start accepting requests
void Server::run() {
while (true) {
tcp::socket* socket = new tcp::socket(io_service_);
acceptor_.accept(*socket);
std::thread child([this, socket] { this->handle_request(socket); });
child.detach();
}
}
void Server::handle_request(tcp::socket* socket) {
char data[MAX_LENGTH];
memset(data, 0, MAX_LENGTH);
boost::system::error_code error;
socket->read_some(boost::asio::buffer(data), error);
if (error) {
std::cout << "Error reading request" << std::endl;
} else {
std::string message = handle_read(data);
boost::asio::write(*socket, boost::asio::buffer(message, message.size()));
}
socket->close();
delete socket;
}
std::string Server::handle_read(const char* data) {
std::string data_string = std::string(data);
auto request = Request::Parse(data_string);
Response response;
std::string longest_prefix;
// check if request has a referer field
using Headers = std::vector<std::pair<std::string, std::string>>;
Headers headers = request->headers();
std::string referer_url;
for (std::size_t i = 0; i < headers.size(); i++) {
if (headers[i].first == "Referer") {
referer_url = headers[i].second;
}
}
// if the referer field exists, we use that for longest prefix
if (referer_url != "") {
std::size_t slash_pos = referer_url.find('/');
slash_pos = referer_url.find('/', slash_pos+1);
slash_pos = referer_url.find('/', slash_pos+1); // find third slash
std::string referer_uri = referer_url.substr(slash_pos);
longest_prefix = find_uri_prefix(referer_uri);
if (longest_prefix == "default") {
for (auto it = handler_map_.begin(); it != handler_map_.end(); it++) {
if (it->second->GetName() == "ReverseProxyHandler") {
RequestHandler::Status status = it->second->HandleRequest(*request, &response);
if (status == RequestHandler::OK) {
return response.ToString();
}
}
}
longest_prefix = find_uri_prefix(request->uri());
}
} else {
longest_prefix = find_uri_prefix(request->uri());
}
RequestHandler::Status status = handler_map_[longest_prefix]->HandleRequest(*request, &response);
// update status counter
StatusCounter::get_instance().request_count_++;
StatusCounter::get_instance().status_code_map_[request->uri()][response.GetStatus()]++;
if (status == RequestHandler::OK) {
std::cout << "Handle request returned status OK." << std::endl;
} else if (status == RequestHandler::FILE_NOT_FOUND) {
std::cout << "Handle request returned status FILE_NOT_FOUND." << std::endl;
if (handler_map_[longest_prefix]->GetName() != "NotFoundHandler")
handler_map_["default"]->HandleRequest(*request, &response);
}
return response.ToString();
}
void Server::create_handler_map(NginxConfig* config) {
std::vector<std::shared_ptr<NginxConfigStatement>> statements = config->statements_;
for (size_t i = 0; i < statements.size(); i++) {
if (statements[i]->tokens_[0] == "path") {
std::string uri_prefix = statements[i]->tokens_[1];
handler_map_[uri_prefix] = RequestHandler::CreateByName(statements[i]->tokens_[2]);
RequestHandler::Status status = handler_map_[uri_prefix]->Init(uri_prefix, *(statements[i]->child_block_));
if (status == RequestHandler::Status::INTERNAL_ERROR) {
std::cout << "Bad config file. Server exiting." << std::endl;
exit(1);
}
} else if (statements[i]->tokens_[0] == "default") {
handler_map_["default"] = RequestHandler::CreateByName("NotFoundHandler");
}
}
init_status_counter();
}
void Server::init_status_counter() {
if (handler_map_.find("/status") != handler_map_.end()) {
for (auto it = handler_map_.begin(); it != handler_map_.end(); it++) {
std::string uri_prefix = it->first;
std::string handler_name = it->second->GetName();
StatusCounter::get_instance().handler_name_map_[uri_prefix] = handler_name;
}
}
}
// Finds the correct handler using longest uri prefix matching
std::string Server::find_uri_prefix(std::string request_uri) {
std::string longest_prefix_match = "";
for (auto it = handler_map_.begin(); it != handler_map_.end(); it++) {
std::string prefix = it->first;
if (is_uri_prefix(prefix, request_uri) &&
prefix.size() > longest_prefix_match.size()) {
longest_prefix_match = prefix;
}
}
if (longest_prefix_match == "")
longest_prefix_match = "default";
return longest_prefix_match;
}
// Returns true if short_str is a uri prefix of long_str
// e.g. /foo is a uri prefix of /foo/boo, but not of /fooo
bool Server::is_uri_prefix(std::string short_str, std::string long_str) {
if (short_str.size() > long_str.size()) return false;
if ((long_str.substr(0, short_str.size()) == short_str) &&
(short_str == long_str || long_str[short_str.size()] == '/')) {
return true;
} else {
return false;
}
}