-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
268 lines (248 loc) · 11 KB
/
server.cpp
File metadata and controls
268 lines (248 loc) · 11 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
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
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include "parser.h"
#include "utils.h"
#include <map>
#include <sstream>
#include <string>
volatile sig_atomic_t Stop = 0;
const int buffer_size = 4096;
const int MaxEvents = 10000;
void stop(int blank)
{
Stop = 1;
}
int write_headers(int fd, int status, const char* comment, std::vector<http_header_t> headers) {
std::ostringstream sstr;
sstr << "HTTP/1.1 " << status << " " << comment << "\r\n";
for (size_t i = 0; i < headers.size(); ++i) {
sstr << headers[i].name << ": " << headers[i].value << "\r\n";
}
sstr << "\r\n";
return write(fd, sstr.str().c_str(), sstr.str().size());
}
int main(int argc, char **argv)
{
read_conf();
check_args(argc, argv);
slog((char*)"Program started.\n");
socklen_t addrlen;
int bufsize = 1024*1024;
char *buffer = (char*)malloc(bufsize);
char proxy_site[1024];
char proxy_service[1024];
int socket_fd = create_and_bind();
make_non_blocking(socket_fd);
int epoll_fd = epoll_create1(0);
struct epoll_event *events = (struct epoll_event*)calloc(MaxEvents, sizeof(events));
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.data.fd = socket_fd;
event.events = EPOLLIN;
struct addrinfo *proxy_addrinfo;
if (global_args.proxy_mode) {
sscanf(global_args.site_address, "%1023[^:/]://%1023s", proxy_service, proxy_site);
struct addrinfo hints = {};
if (int errcode = getaddrinfo(proxy_site, proxy_service, &hints, &proxy_addrinfo)) {
slog("Error resolving host \"%s\" with service \"%s\": %s\n",
proxy_site, proxy_service, gai_strerror(errcode));
exit(1);
}
}
if (listen(socket_fd, SOMAXCONN) < 0) {
perror("listen");
close(socket_fd);
exit(1);
}
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &event) < 0) {
perror("epoll_ctl");
close(socket_fd);
exit(1);
}
signal(SIGINT, stop);
signal(SIGTERM, stop);
std::map<int, int> fdpairs;
std::map<int, http_request_t> requests;
std::map<int, std::string> requests_raw;
std::map<int, short> fd_ready; // 0 - not ready, 1 - read, 2 - write
while (!Stop) {
int number_of_events = epoll_wait(epoll_fd, events, MaxEvents, -1);
if (number_of_events < 0) {
perror("epoll_wait");
close(socket_fd);
exit(1);
}
for (int i = 0; i < number_of_events; ++i) {
const uint32_t emask = events[i].events;
const bool e_error = emask & EPOLLERR;
const bool e_hup = emask & EPOLLHUP;
const bool e_rdhup = emask & EPOLLRDHUP;
const bool e_out = emask & EPOLLOUT;
const bool e_in = emask & EPOLLIN;
const int fd = events[i].data.fd;
if (e_error || e_hup) {
if (e_error || e_hup) {
slog((char*)"Error with some socket\n");
}
close(fd);
if (fdpairs.count(fd)) {
close(fdpairs[fd]);
fdpairs.erase(fd);
}
continue;
}
if (fd == socket_fd) {
while(1) {
struct sockaddr_in in_addr;
socklen_t in_addr_size = sizeof(in_addr);
int incoming_fd = accept(socket_fd, (sockaddr*)&in_addr, &in_addr_size);
if (-1 == incoming_fd) {
if (EAGAIN == errno || EWOULDBLOCK == errno) {
break;
}
else {
perror("accept");
requests.erase(incoming_fd);
close(socket_fd);
exit(1);
}
}
else {
make_non_blocking(incoming_fd);
event.data.fd = incoming_fd;
event.events = EPOLLIN | EPOLLOUT;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, incoming_fd, &event) < 0) {
perror("epoll_ctl");
close(socket_fd);
exit(1);
}
http_request_t r;
r.ready = 0;
requests.insert({incoming_fd, r});
requests_raw.insert({incoming_fd, ""});
fdpairs.insert({incoming_fd, -1});
slog("A client has connected...\n");
}
}
} else if (e_out && requests[fd].ready) {
if (fdpairs[fd] < 0) {
std::vector<http_header_t> headers{{"Content-Type", "text/html"}, {"Content-Length", "49"}};
write_headers(fd, 404, "Not Found", headers);
write(fd, "<html><body><h1>404: Not Found</h1></body></html>", 49);
fdpairs.erase(fd);
requests.erase(fd);
requests_raw.erase(fd);
close(fd);
} else {
if (requests[fd].ready == 1) {
requests[fd].ready = 2;
if (!global_args.proxy_mode) {
struct stat stats;
if (stat(requests[fd].path, &stats) < 0) {
perror("fstat");
close(fdpairs[fd]);
fdpairs.erase(fd);
requests.erase(fd);
requests_raw.erase(fd);
close(fd);
continue;
}
char size[15];
snprintf(size, 15, "%d", stats.st_size);
std::vector<http_header_t> headers{{"Content-Type", "text/html"}, {"Content-Length", size}};
write_headers(fd, 200, "OK", headers);
}
}
char buf[buffer_size];
int read_size = read(fdpairs[fd], buf, buffer_size);
if (read_size < 0) {
perror("read");
close(fdpairs[fd]);
fdpairs.erase(fd);
requests.erase(fd);
requests_raw.erase(fd);
close(fd);
continue;
} else if (read_size > 0) {
write(fd, buf, read_size);
} else {
close(fdpairs[fd]);
fdpairs.erase(fd);
requests.erase(fd);
requests_raw.erase(fd);
close(fd);
}
}
} else if (e_in) {
while (1) {
int recv_size = recv(fd, buffer, bufsize, 0);
if (recv_size == 0 || (recv_size < 0 && errno == EAGAIN)) {
if (http_parse_request(requests_raw[fd].c_str(), &requests[fd]) < 0) {
slog("Bad request:\n");
slog("<<%s>>\n", requests_raw[fd].c_str());
std::vector<http_header_t> headers{{"Content-Type", "text/html"}, {"Content-Length", "51"}};
write_headers(fd, 500, "Bad Request", headers);
write(fd, "<html><body><h1>500: Bad Request</h1></body></html>", 51);
close(fdpairs[fd]);
fdpairs.erase(fd);
requests.erase(fd);
requests_raw.erase(fd);
close(fd);
slog("%s\n", buffer);
break;
}
if (global_args.proxy_mode) {
fdpairs[fd] = socket(PF_INET, SOCK_STREAM, 0);
connect(fdpairs[fd], proxy_addrinfo->ai_addr, proxy_addrinfo->ai_addrlen);
size_t hbegin = requests_raw[fd].find("Host");
size_t hlen = requests_raw[fd].substr(hbegin).find("\n");
requests_raw[fd].replace(hbegin + 6, hlen - 6, std::string(proxy_site)); //len("Host: ") == 6
slog("<<%s>>\n", requests_raw[fd].c_str());
write(fdpairs[fd], requests_raw[fd].c_str(), requests_raw[fd].length());
} else {
slog("<<%s>>\n", requests_raw[fd].c_str());
fdpairs[fd] = open(requests[fd].path, O_RDONLY);
if (open >= 0) {
struct stat buf;
fstat(fdpairs[fd], &buf);
if (S_ISDIR(buf.st_mode)) {
fdpairs[fd] = -1;
}
}
}
break;
}
if (recv_size < 0 ) {
perror("recv");
close(fd);
}
requests_raw[fd].append(buffer);
}
}
}
}
free(events);
close(socket_fd);
if (global_args.site_path != NULL) {
free(global_args.site_path);
}
if (global_args.site_address != NULL) {
free(global_args.site_address);
}
printf("\nClosing...\n");
return 0;
}