-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.cpp
186 lines (161 loc) · 6.28 KB
/
webserver.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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <ctime>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
const int port = 80;
const int maxConnectionRequests = 500;
const bool cacheIndexHTML = true;
std::string logTime(){
std::time_t result = std::time(nullptr);
return '[' + static_cast<std::string>(std::ctime(&result)).substr(4, 20) + "] ";
}
std::string findContentType(std::string filepath){
while(filepath.find("?") != std::string::npos){
filepath.erase(filepath.find("?"), filepath.length() );
}
if(filepath.substr(filepath.find(".") + 1, filepath.length() - 1) == "png" ){
return "Content-Type: image/png;";
}
else if(filepath.substr(filepath.find(".") + 1, filepath.length() - 1) == "jpg"){
return "Content-Type: image/jpeg;";
}
else if(filepath.substr(filepath.find(".") + 1, filepath.length() - 1) == "mp4"){
return "Content-Type: video/mp4;";
}
else if(filepath.substr(filepath.find(".") + 1, filepath.length() - 1) == "html" || filepath == "/" ){
return "text/html; charset=UTF-8";
}
else if(filepath.substr(filepath.find(".") + 1, filepath.length() - 1) == "txt" ){
return "text/plain; charset=UTF-8";
}
return "application/octet-stream;";
}
std::string readFile(std::string filename){
std::stringstream filedata;
if(filename == "/"){
filename = "webserver/index.html"; //index.html is used as the homepage
std::ifstream file(filename.c_str());
filedata << file.rdbuf();
}
else{
if(filename.at(0) == '/'){
filename.erase(0, 1);
}
while(filename.find("%20") != std::string::npos){
filename.replace(filename.find("%20"), 3, " ");
}
while(filename.find("%5F") != std::string::npos){
filename.replace(filename.find("%5F"), 3, "_");
}
while(filename.find("%2D") != std::string::npos){
filename.replace(filename.find("%2D"), 3, "-");
}
while(filename.find("%28") != std::string::npos){
filename.replace(filename.find("%28"), 3, "(");
}
while(filename.find("%29") != std::string::npos){
filename.replace(filename.find("%29"), 3, ")");
}
while(filename.find("?") != std::string::npos){
filename.erase(filename.find("?"), filename.length() );
}
filename = "webserver/" + filename; //all public files should be inside the /webserver folder
std::ifstream file(filename.c_str());
filedata << file.rdbuf();
if(filedata.str().empty()){
std::ifstream file("404.html");
filedata << file.rdbuf();
}
}
return filedata.str();
}
std::string respondToGET(std::string filepath, bool cacheIndexHTML, std::string* index){
std::string response = "HTTP/1.1 ";
std::string file;
if(cacheIndexHTML && filepath == "/"){
file = *index;
}
else {
file = readFile(filepath);
}
if (file.empty()){
std::cout << "404 Not Found" << std::endl;
response += "404 Not Found";
}
else{
std::cout << "200 OK" << std::endl;
response += "200 OK\n";
response += "Content-Type: ";
response += findContentType(filepath);
response += "\r\n";
int length = file.size();
response += "Content-Length: ";
response += std::to_string(length);
response += "\n\n";
response += file;
}
return response;
}
int main(){
std::cout << logTime() << "Starting server\n";
int server_fd = 0;
int newSocket = 0;
struct sockaddr_in address;
int addrlen = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // htonl converts a long integer (e.g. address) to a network representation INADDR_ANY chooses any IP, WiFi or Ethernet.
address.sin_port = htons(port); /* htons converts a short integer (e.g. port) to a network representation */
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ //AF_INET = IPv4
std::cerr << logTime() << "Cannot create socket\n";
return 1;
}
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0){
std::cerr << logTime() << "Cannot bind socket\n";
return 2;
}
if(listen(server_fd, maxConnectionRequests) != 0){
std::cerr << logTime() << "Cannot listen to socket\n";
return 3;
}
std::string* index = new std::string;
if(cacheIndexHTML){
std::string temp = readFile("/"); //read index
*index = temp;
}
else{
delete index;
}
while (true){
if ((newSocket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0){
std::cerr << logTime() << "Connection refused\n";
return 4;
}
const size_t buffersize = 524288; //somehow larger requests stop at 524288 bytes
char* buffer = new char[buffersize] {0};
int usedbuffersize = recv(newSocket, buffer, buffersize, 0);
std::string request = std::string(buffer);
std::string response = "HTTP/1.1 ";
std::cout << "incoming request: " << request << " with size: " << usedbuffersize << std::endl;
if (request.length() > 4){ //make sure that the request is long enough
std::string filepath = request.substr(4, request.find(" ", 4) - 4);
std::cout << logTime() << "Requested: " << filepath << " Responding: ";
if(request.substr(0,3) == "GET"){
std::string response = respondToGET(filepath, cacheIndexHTML, index);
send(newSocket, response.c_str(), response.length(), 0);
}
else {
std::cout << "501 Unsupported" << std::endl;
response += "501 Unsupported";
send(newSocket, response.c_str(), response.length(), 0);
}
delete buffer;
close (newSocket);
}
}
}