-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cpp
190 lines (162 loc) · 5.21 KB
/
Worker.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
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <signal.h>
#include <sys/sendfile.h>
#include "Worker.h"
void Worker::run(int listener) {
signal(SIGPIPE, SIG_IGN);
int threads_num = 3;
std::vector<std::thread> threads;
for (int i = 0; i < threads_num; i++) {
std::thread thread([this](int l) {
this->run_thread(l);
}, listener);
threads.push_back(std::move(thread));
}
for (auto &thr: threads) {
if (thr.joinable()) {
thr.join();
}
}
}
void Worker::run_thread(int listener) {
HttpParser parser;
while(true) {
auto sock = accept(listener, nullptr, nullptr);
if(sock < 0) {
perror("accept");
exit(3);
}
auto req = read_request(sock);
try {
auto http_req = parser.parse_header(req);
complete_tusk(http_req, sock);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
close(sock);
}
}
void Worker::write_start_line(std::string &version, int status, int sock) {
auto start_str = version + " " + std::to_string(status) + " " + statuses.at(status) + "\r\n";
send(sock, start_str.c_str(), start_str.length(), 0);
}
std::string Worker::read_request(int sock) {
const int buf_size = 1024;
char buf[buf_size]; // TODO move to some config
std::string req;
auto n = recv(sock, buf, buf_size, 0);
if (n > 0) {
req.append(buf, n);
} else {
if (n == -1) {
throw Exception("read failed");
}
}
return req;
}
void Worker::complete_tusk(HttpRequest &request, int socket) {
int status;
auto method = request.start_line.method;
std::cout << "url: " << request.start_line.uri << std::endl
<< "method: " << request.start_line.method << std::endl
<< "http version: " << request.start_line.http_version << std::endl;
if (method == "GET" || method == "HEAD") {
status = 200;
bool is_subdir;
try {
is_subdir = make_path(request.start_line.uri);
if (!fs::exists(request.start_line.uri)) {
if (is_subdir) {
status = 403;
} else {
status = 404;
}
}
} catch (Exception &e) {
status = 403;
std::cout << "exc" << std::endl;
}
// TODO check if its directory
write_start_line(request.start_line.http_version, status, socket);
int size = 0;
if (status == 200) {
size = fs::file_size(request.start_line.uri);
}
std::cout << "status: " << status << "; size: " << size << std::endl;
if (status == 200) {
write_headers(socket, true, request.start_line.uri, size);
if (method == "GET") {
write_file(socket, request.start_line.uri);
}
} else {
write_headers(socket, false, request.start_line.uri);
}
} else {
status = 405;
write_start_line(request.start_line.http_version, status, socket);
write_headers(socket, false, request.start_line.uri);
}
}
void Worker::write_headers(int sock, bool is_ok, std::string &uri, int length) {
std::string headers = "Server: Kotyarich Server C++\r\n"
"Connection: close\r\n"
"Date: " + get_rfc7231_time() + "\r\n";
if (is_ok) {
std::string content_type;
try {
auto ext = get_extension(uri);
content_type = contnet_types.at(ext);
} catch (std::exception &e) {
content_type = "text/plain";
}
headers += "Content-Type: " + content_type + "\r\n"
+ "Content-Length: " + std::to_string(length) + "\r\n\r\n";
}
std::cout << headers;
send(sock, headers.c_str(), headers.length(), 0);
}
bool Worker::make_path(std::string &path) {
if (path.find("/..") != std::string::npos) {
throw Exception("bad path .."); // TODO add custom exception
}
auto arguments_pos = path.find('?');
if (arguments_pos != std::string::npos) {
path = path.substr(0, arguments_pos);
}
HttpParser parser;
auto decoded_path = parser.decode_uri(path);
auto subdir = false;
if (decoded_path[decoded_path.length() - 1] != '/') {
path = _docs_root + decoded_path;
} else {
subdir = true;
path = _docs_root + decoded_path + "index.html";
}
std::cout << "path: " << path << std::endl;
return subdir;
}
void Worker::write_file(int sock, std::string &uri) {
auto size = fs::file_size(uri);
auto fd = open(uri.c_str(), O_RDONLY);
// TODO add check if file opened
while (size != 0) {
auto written = sendfile(sock, fd, nullptr, size);
if (written != -1) {
size -= written;
} else {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
// TODO add something useful here mb
}
close(fd);
return;
}
}
close(fd);
}
Worker::Worker(std::string &docs_root): _docs_root(docs_root) {}