-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.cpp
More file actions
210 lines (178 loc) · 5.03 KB
/
Copy pathhttp_server.cpp
File metadata and controls
210 lines (178 loc) · 5.03 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
#include "http_server.h"
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
using namespace std;
#define MAX_CONN 10000
#define MAX_EVENTS 32
#define BUF_SIZE 30000
#define MAX_LINE 256
static void epoll_ctl_add(int epfd, int fd, uint32_t events) {
struct epoll_event ev;
ev.events = events;
ev.data.fd = fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
perror("epoll_ctl()\n");
exit(1);
}
}
static void set_sockaddr(struct sockaddr_in *addr, int port) {
bzero((char *)addr, sizeof(struct sockaddr_in));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = INADDR_ANY;
addr->sin_port = htons(port);
}
static int setnonblocking(int sockfd) {
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK) == -1) {
return -1;
}
return 0;
}
void http_server::run(int port) {
int i;
int n;
int epfd;
int nfds;
int listen_sock;
int conn_sock;
int socklen;
char buf[BUF_SIZE];
struct sockaddr_in srv_addr;
struct sockaddr_in cli_addr;
struct epoll_event events[MAX_EVENTS];
this->port = port;
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
set_sockaddr(&srv_addr, port);
bind(listen_sock, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
setnonblocking(listen_sock);
listen(listen_sock, MAX_CONN);
epfd = epoll_create(1);
epoll_ctl_add(epfd, listen_sock, EPOLLIN | EPOLLOUT | EPOLLET);
socklen = sizeof(cli_addr);
for (;;) {
nfds = epoll_wait(epfd, events, MAX_EVENTS, -1);
for (i = 0; i < nfds; i++) {
if (events[i].data.fd == listen_sock) {
/* handle new connection */
conn_sock =
accept(listen_sock,
(struct sockaddr *)&cli_addr,
(socklen_t*)&socklen);
inet_ntop(AF_INET, (char *)&(cli_addr.sin_addr),
buf, sizeof(cli_addr));
printf("[+] connected with %s:%d\n", buf,
ntohs(cli_addr.sin_port));
setnonblocking(conn_sock);
epoll_ctl_add(epfd, conn_sock,
EPOLLIN | EPOLLET | EPOLLRDHUP |
EPOLLHUP);
} else if (events[i].events & EPOLLIN) {
/* handle EPOLLIN event */
string data = "";
for (;;) {
bzero(buf, sizeof(buf));
n = read(events[i].data.fd, buf,
sizeof(buf));
if (n <= 0 /* || errno == EAGAIN */ ) {
break;
} else {
data.append(buf);
printf("[+] data: %s\n", buf);
}
}
string res = process_request(data);
write(events[i].data.fd, res.c_str(), res.size());
} else {
printf("[+] unexpected\n");
}
/* check if the connection is closing */
if (events[i].events & (EPOLLRDHUP | EPOLLHUP)) {
printf("[+] connection closed\n");
epoll_ctl(epfd, EPOLL_CTL_DEL,
events[i].data.fd, NULL);
close(events[i].data.fd);
continue;
}
}
}
}
const std::unordered_map<std::string, std::string> mime_type = {
{".html", "text/html"},
{".css", "text/css"},
{".xml", "text/xml"},
{".xhtml", "application/xhtml+xml"},
{".txt", "text/plain"},
{".png", "image/png"},
{".gif", "image/gif"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".txt", "text/plain"},
{".rtf", "application/rtf"},
{".pdf", "application/pdf"},
{".word", "application/nsword"},
{".au", "audio/basic"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".avi", "video/x-msvideo"},
{".gz", "application/x-gzip"},
{".tar", "application/x-tar"},
};
string http_server::process_request(const string &request_raw_string) {
http_request request(request_raw_string);
const auto& method = request.get_method();
const auto& path = request.get_path();
auto ite = this->routes.find({method, path});
if (ite != this->routes.end()) {
return ite->second(request).message;
}
if (request.get_method() == "GET") {
string file_name = "";
if (request.get_path() == "/") {
file_name = "index.html";
} else {
file_name = request.get_path().substr(1);
}
struct stat sbuf;
if (stat(file_name.c_str(), &sbuf) < 0) {
return http_response(404, "NOT FOUND!").message;
}
auto dot_pos = file_name.find_last_of(".");
string file_type = "";
if (dot_pos == std::string::npos) {
file_type = "text/html";
} else {
std::string suffix = file_name.substr(dot_pos);
auto it = mime_type.find(suffix);
if (it == mime_type.cend()) {
file_type = "text/html";
} else {
file_type = it->second;
}
}
int src_fd = open(file_name.c_str(), O_RDONLY, 0);
if (src_fd < 0) {
return http_response(404, "NOT FOUND!").message;
}
void *mmapRet = mmap(nullptr, sbuf.st_size, PROT_READ, MAP_PRIVATE, src_fd, 0);
close(src_fd);
if (mmapRet == (void *) (-1)) {
munmap(mmapRet, sbuf.st_size);
return http_response(404, "NOT FOUND!").message;
}
char *src_addr = static_cast<char *>(mmapRet);
string body = string(src_addr, src_addr + sbuf.st_size);
munmap(mmapRet, sbuf.st_size);
return http_response(200, body, file_type).message;
}
return http_response(404, "NOT FOUND!").message;
}