-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (191 loc) · 6 KB
/
main.cpp
File metadata and controls
218 lines (191 loc) · 6 KB
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
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <vector>
#include "http_parser.h"
#include <string.h>
#include <sstream>
#include <utility>
#include <string>
//compile with -no-pie
int on_url(http_parser* parser, const char* at, size_t length) {
http_parser_url url_data;
http_parser_url_init(&url_data);
http_parser_parse_url(at, length, 0, &url_data);
std::string url(at);
uint16_t size = url_data.field_data[UF_PATH].len;
uint16_t from = url_data.field_data[UF_PATH].off;
std::string path = url.substr(from, size);
strncpy((char*)parser->data, path.c_str(), path.size());
return 0;
}
using boost::asio::ip::tcp;
class Connection
{
public:
Connection(boost::asio::io_service& service, std::string& dir) :m_socket(service), m_buffer(1024), m_dir(dir) {}
tcp::socket& socket()
{
return m_socket;
}
void operator()()
{
std::size_t read = m_socket.read_some(boost::asio::buffer(&m_buffer[0], 1024));
readHandler(read);
boost::system::error_code erro;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, erro);
m_socket.close();
}
private:
void readHandler(std::size_t bytes_transferred)
{
std::cout << "input:" << std::endl;
for (int i = 0; i < bytes_transferred; ++i)
std::cout << m_buffer[i];
std::cout << std::endl;
std::string path = getPath(bytes_transferred);
std::cout << "Path: " << path << std::endl;
bool pathOk = path.size() > 1;
std::ifstream file(m_dir + path);
if (pathOk && file.is_open())
{
std::stringstream ss;
std::stringstream fileStream;
fileStream << file.rdbuf();
std::string content(fileStream.str());
ss << "HTTP/1.0 200 OK\r\n";
ss << "Content-Type: text/html\r\n";
ss << "Content-length: " << content.size() << "\r\n";
ss << "Connection: close\r\n";
ss << "\r\n";
ss << content;
std::string str(ss.str());
file.close();
std::cout << "Response: " << str << std::endl;
m_socket.write_some(boost::asio::buffer(&str[0], str.size()));
}
else
{
std::string not_found("HTTP/1.0 404 NOT FOUND\r\nContent-length: 0\r\nContent-Type: text/html\r\n\r\n");
std::cout << "Responce: " << not_found << std::endl;
m_socket.write_some(boost::asio::buffer(¬_found[0], not_found.size()));
}
m_buffer.clear();
}
private:
std::string getPath(std::size_t bytes_transferred)
{
http_parser_settings in_settings;
http_parser_settings_init(&in_settings);
in_settings.on_url = on_url;
char* in_parser_buffer = new char[255];
memset(in_parser_buffer, 0, 255);
http_parser in_parser;
in_parser.data = in_parser_buffer;
http_parser_init(&in_parser, HTTP_REQUEST);
http_parser_execute(&in_parser, &in_settings, &m_buffer[0], bytes_transferred);
return std::string(in_parser_buffer);
}
private:
tcp::socket m_socket;
std::vector<char> m_buffer;
std::string& m_dir;
};
class server
{
public:
server(boost::asio::io_service& io_service, const std::string& host, short port, const std::string& dir)
: io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), port_(port), host_(host), dir_(dir)
{
start_accept();
}
private:
void start_accept()
{
Connection* connection = new Connection(io_service_, dir_);
acceptor_.async_accept(connection->socket(), boost::bind(&server::handle_accept, this, connection, boost::asio::placeholders::error));
}
void handle_accept(Connection* connection, const boost::system::error_code& error)
{
//std::cout << "New connection!" << std::endl;
boost::thread thread(std::move(*connection));
thread.detach();
start_accept();
}
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
int port_;
std::string host_;
std::string dir_;
};
int main(int argc, char* argv[])
{
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log any failure here */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log any failure here */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
auto fd = open("/tmp/webserver.log", O_WRONLY|O_CREAT|O_APPEND, 0777);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(STDIN_FILENO);
if (fd > 2) close(fd);
std::cout << "===================[ server start ]===================\n";
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
int port;
std::string dir;
std::string host;
int c;
while ((c = getopt (argc, argv, "h:p:d:")) != -1)
switch (c)
{
case 'h':
host = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
dir = optarg;
break;
default:
abort ();
}
try
{
boost::asio::io_service io_service;
server s(io_service, host, port, dir);
io_service.run();
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << "\n";
}
return 0;
}