-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
115 lines (94 loc) · 3.23 KB
/
main.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
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-err34-c"
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <fcntl.h>
#include <algorithm>
#include <thread>
#include "arg_parser.h"
std::string get_full_path(const std::string &request) {
std::string full_path = request.substr(
request.find('/'),
request.size() - request.find('/'));
full_path = "." + full_path;
full_path.erase(
std::remove(full_path.begin(), full_path.end(), ' '), full_path.end());
if (full_path == "/") full_path = "index.html";
return full_path;
}
bool handle_get_request(int socket, std::string request) {
if (request.find("HTTP") != std::string::npos) // NOLINT(abseil-string-find-str-contains)
request = request.substr(0, request.find("HTTP"));
if (request.find('?') != std::string::npos)
request = request.substr(0, request.find('?'));
std::string fullpath = get_full_path(request);
std::string response;
int requested_fd = open(fullpath.c_str(), O_RDONLY);
if (requested_fd == -1) {
response = "HTTP/1.0 404 NOT FOUND\r\nContent-Length: 0\r\nContent-Type: text/html\r\n";
send(socket, response.c_str(), response.size(), 0);
return false;
}
response = "HTTP/1.0 200 OK\r\n\r\n";
send(socket, response.c_str(), response.size(), 0);
char readBuf[1024];
while (int cntRead = read(requested_fd, readBuf, 1024)) {
send(socket, readBuf, cntRead, 0);
}
close(requested_fd);
return true;
}
bool worker(int socket) {
char buf[2048];
int read_size = read(socket, buf, 2048);
if (read_size == -1 || read_size == 0) {
shutdown(socket, SHUT_RDWR);
close(socket);
return false;
}
std::string requestStr(buf, (unsigned long) read_size);
if (requestStr.size() > 3 && "GET" == requestStr.substr(0, 3)) {
handle_get_request(socket, requestStr);
} else {
std::string response = "HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\nContent-Type: text/html\r\n\r\n";
send(socket, response.c_str(), response.size(), 0);
}
shutdown(socket, SHUT_RDWR);
close(socket);
return true;
}
int main(int argc, char **argv) {
auto port = "12345";
auto ip = "127.0.0.1";
auto directory = "tmp";
parse_args(argc, argv, port, ip, directory);
struct sockaddr_in host_addr{};
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(atoi(port));
inet_aton(ip, &host_addr.sin_addr);
int masterSocket = socket(AF_INET, SOCK_STREAM, 0);
if (bind(masterSocket, (struct sockaddr *) &host_addr, sizeof(host_addr)))
return 1;
if (!fork()) {
setsid();
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
chdir(directory);
if (listen(masterSocket, SOMAXCONN))
return 2;
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
while (true) {
int slaveSocket = accept(masterSocket, nullptr, nullptr);
std::thread thread(worker, slaveSocket);
thread.detach();
}
#pragma clang diagnostic pop
}
return 0;
}