-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.cpp
196 lines (152 loc) · 6.4 KB
/
server.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
#include "server.h"
#include <signal.h>
#include <utility>
#include <string>
#include <iostream>
#include <cstddef>
#include "request_handler_default.h"
#include "request_handler_echo.h"
#include "request_handler_static.h"
#include "request_handler_status.h"
#include <thread>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
unsigned int MAX_NUM_THREADS = 32;
namespace http {
namespace server {
//Constructor
server::server(const std::string& address, NginxConfig config)
: io_service_(), signals_(io_service_), acceptor_(io_service_), socket_(io_service_)
{
// map from path -> (handler_name, root)
std::map <std::string, std::pair<std::string,NginxConfig>> path_info;
for (const auto& statement : config.statements_) {
for (unsigned int i = 0; i < statement->tokens_.size(); ++i) {
if (statement->tokens_.size() == 2 && statement->tokens_[i] == "port")
portNum_ = statement->tokens_[i+1];
else if (statement->tokens_.size() == 3 && statement->tokens_[i]=="path" && statement->tokens_[i+2]=="EchoHandler"){
std::string echo_handler = statement->tokens_[i+2];
NginxConfig echo_config;
path_info[statement->tokens_[i+1]] = std::make_pair(echo_handler, echo_config);
ServerMonitor::getInstance()->addHandler(statement->tokens_[i+2], statement->tokens_[i+1]);
}
else if (statement->tokens_.size() == 3 && statement->tokens_[i]=="path" && statement->tokens_[i+2]=="StatusHandler"){
std::string status_handler = statement->tokens_[i+2];
NginxConfig status_config;
path_info[statement->tokens_[i+1]] = std::make_pair(status_handler, status_config);
ServerMonitor::getInstance()->addHandler(statement->tokens_[i+2], statement->tokens_[i+1]);
}
else if (statement->tokens_.size() == 3 && statement->tokens_[i]=="path" && statement->tokens_[i+2]=="StaticHandler"){
std::string path = statement->tokens_[i+1];
std::string static_handler = statement->tokens_[i+2];
NginxConfig child = *(statement->child_block_);
path_info[path] = std::make_pair(static_handler, child);
ServerMonitor::getInstance()->addHandler(statement->tokens_[i+2], statement->tokens_[i+1]);
}
else if (statement->tokens_.size() == 3 && statement->tokens_[i]=="path" && statement->tokens_[i+2]=="ProxyHandler"){
std::string path = statement->tokens_[i+1];
std::string proxy_handler = statement->tokens_[i+2];
NginxConfig child = *(statement->child_block_);
path_info[path] = std::make_pair(proxy_handler, child);
ServerMonitor::getInstance()->addHandler(statement->tokens_[i+2], statement->tokens_[i+1]);
}
else if (statement->tokens_.size() == 2 && statement->tokens_[i]=="default"){
NginxConfig default_config;
path_info[statement->tokens_[i]]= std::make_pair(statement->tokens_[i+1], default_config);
ServerMonitor::getInstance()->addHandler(statement->tokens_[i+1], "");
}
}
}
std::cout << std::endl << "PORT: " << portNum_ << std::endl;
std::cout << std::endl << "HANDLERS: " << std::endl;
// for(auto iterator = path_info.begin(); iterator != path_info.end(); iterator++) {
// // iterator->first = key
// // iterator->second = value
// std::cout << iterator->first << "\t" <<
// iterator->second.first << "\t" <<
// iterator->second.second << std::endl;
// }
// std::cout << std::endl;
for(auto iterator = path_info.begin(); iterator != path_info.end(); iterator++) {
std::string path = iterator->first;
handlers[path] = RequestHandler::CreateByName(iterator->second.first.c_str());
handlers[path]->Init(path, iterator->second.second);
}
if (stoi(portNum_) < 0 || stoi(portNum_) > 65535) {
std::cerr << "Error: Port number not in range [0:65535].\n";
}
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve({address, portNum_});
acceptor_.open(endpoint.protocol());
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
address_ = address;
signals_.add(SIGINT);
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif // defined(SIGQUIT)
do_await_stop();
do_accept();
}
void server::run()
{
boost::thread threads[MAX_NUM_THREADS];
for (std::size_t i = 0; i < MAX_NUM_THREADS; ++i){
threads[i] = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
}
for (std::size_t i = 0; i < MAX_NUM_THREADS; ++i){
threads[i].join();
}
}
bool server::getStatus()
{
return isRunning;
}
std::string server::getPortNum(){
return portNum_;
}
std::string server::getAddress(){
return address_;
}
void server::do_await_stop()
{
signals_.async_wait(
[this](boost::system::error_code /*ec*/, int /*signo*/)
{
// The server is stopped by cancelling all outstanding asynchronous
// operations. Once all operations have finished the io_service::run()
// call will exit.
std::cout << " \n";
acceptor_.close();
io_service_.stop();
});
}
void server::do_accept()
{
acceptor_.async_accept(socket_,
[this](boost::system::error_code ec){
// error check, if the website name given is not valid then this will return
if (!acceptor_.is_open()) {
return;
}
// if accept handle did not detect any problems with creating then start the connection
if (!ec) {
// Creates a shared connection ptr and calls start on it
// std::move gets rid of the copy constructor delete error
auto my_connection = std::make_shared<connection> (std::move(socket_), handlers);
my_connection->start();
}
do_accept();
});
}
bool server::create_connection(boost::system::error_code ec){
isRunning = true;
if (ec == 0)
return true;
else
return false;
}
}
}