-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebServer.cpp
More file actions
251 lines (197 loc) · 6.23 KB
/
WebServer.cpp
File metadata and controls
251 lines (197 loc) · 6.23 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
#include "WebServer.h"
#include "Client.h"
#include "Socket.h"
#include "File.h"
typedef struct {
WebServer* web_server;
int client;
} Thread_Params;
#ifdef _WIN32
#define THREAD_FUNC static DWORD WINAPI
#else
#include "pthread.h"
#define THREAD_FUNC static void*
#endif
THREAD_FUNC Worker(void* args);
WebServer::WebServer(){
// Init Supported Mime Types HashTable.
supported_mime_types.set(".png","image/png");
supported_mime_types.set(".jpg","image/jpeg");
supported_mime_types.set(".jpeg","image/jpeg");
supported_mime_types.set(".jfif","image/jpeg");
supported_mime_types.set(".pjpeg","image/jpeg");
supported_mime_types.set(".pjp","image/jpeg");
supported_mime_types.set(".html","text/html");
supported_mime_types.set(".js","text/javascript");
supported_mime_types.set(".json","application/json");
supported_mime_types.set(".mp4","video/mp4");
}
void WebServer::listen(const char port[]){
Socket my_socket(port);
// Create a SOCKET for the server to listen for client connections
my_socket.socket();
// Setup the TCP listening socket
my_socket.bind();
// Listen
my_socket.listen();
while(true){
// Accept connection
int _client = my_socket.accept();
// Create thread for each connection
Thread_Params* th_pr = new Thread_Params;
th_pr->web_server = this;
th_pr->client = _client;
#ifdef _WIN32
CreateThread( NULL, 0, Worker, th_pr,0, &WORKER_ID);
#else
pthread_create(&WORKER_ID, nullptr, Worker, th_pr);
#endif
}
return;
}
WebServer::~WebServer(){
return;
}
void WebServer::use_static_path(const char* path){
static_paths.append(String(path));
}
void WebServer::register_callback(const char* path,const char* method,callback call){
handlers.append(new PathHandler(String(path), String(method), call));
}
void WebServer::get(const char* path,callback call){
register_callback(path, "GET", call);
}
void WebServer::head(const char *path, callback call){
register_callback(path, "HEAD", call);
}
void WebServer::post(const char *path, callback call){
register_callback(path, "POST", call);
}
void WebServer::put(const char *path, callback call){
register_callback(path, "PUT", call);
}
void WebServer::del(const char *path, callback call){
register_callback(path, "DELETE", call);
}
bool WebServer::is_static_path(String path){
bool found = false;
LinkedListIterator<LinkedList<String>::Bucket> iterator(static_paths.get_head());
while(!iterator.is_end()){
if(path.startWith(iterator.node()->value)){
found = true;
break;
}
iterator.increment();
}
return found;
}
void WebServer::handle_static_path(String path, HttpRequest* req, HttpResponse* res){
// readfiles and send response
// Open a handle to the file
File my_file(path.get());
#ifdef _WIN32
if(my_file.getFileHandle() == INVALID_HANDLE_VALUE) {
printf("\nINVALID_HANDLE_VALUE\n");
// An error occurred
res->status(404);
res->setHeader("Server","abdelfetah-dev");
res->send("<h1>404 Not Found!</h1>");
return;
}
#else
if(my_file.getFileHandle() < 0) {
printf("\nINVALID_HANDLE_VALUE\n");
// An error occurred
res->status(404);
res->setHeader("Server","abdelfetah-dev");
res->send("<h1>404 Not Found!</h1>");
return;
}
#endif
// Determine the size of the file
uint fileSize = my_file.getSize();
// Allocate a buffer to hold the file contents
char* buffer = new char[fileSize+1];
// Read the contents of the file
bool success = my_file.read(buffer);
if(!success){
printf("\nReadFile failed!\n");
// An error occurred
res->status(404);
res->setHeader("Server","abdelfetah-dev");
res->send("<h1>404 Not Found!</h1>");
return;
}
// determine a mimeType by its extension.
String ext(path.get() + path.indexOf('.'));
ext.to_lower_case();
auto mime_type = supported_mime_types.get(ext);
if(!mime_type.is_error()){
res->setHeader("Content-Type", mime_type.value());
}else{ // if files exist with unkonws mediaType.
res->setHeader("Content-Type","application/octet-stream");
}
res->status(200);
res->send(buffer, fileSize);
// Free the buffer
delete[] buffer;
// Close the handle to the file
my_file.close();
}
bool WebServer::handle_path(HttpRequest* req, HttpResponse* res){
LinkedListIterator<LinkedList<PathHandler*>::Bucket> iterator(handlers.get_head());
while(!iterator.is_end()){
if(iterator.node()->value->handlePath(req, res)){
return true;
}
iterator.increment();
}
return false;
}
void WebServer::handle(HttpRequest* req, HttpResponse* res){
if(req->getMethod().startWith("GET")){
// 1. Checking the static_paths.
String path(req->getPath());
if(is_static_path(path)){
handle_static_path(path, req, res);
return;
}
}
bool did_handle = handle_path(req, res);
if(!did_handle){
res->status(404);
res->setHeader("Server","abdelfetah-dev");
res->send("<h1>404 Not Found!</h1>");
}
}
THREAD_FUNC Worker(void* args){
Thread_Params* params = (Thread_Params*) args;
WebServer* Server = params->web_server;
Socket m_client(params->client);
char* buffer[DEFAULT_BUFLEN];
// FIXME: implement such a utility to clean the buffer, or add a method to ByteBuffer class
#ifdef _WIN32
ZeroMemory(buffer, DEFAULT_BUFLEN);
#else
bzero(buffer, DEFAULT_BUFLEN);
#endif
Client client(&m_client);
client.recv((char*) buffer, DEFAULT_BUFLEN);
HttpRequest req((char*) buffer, client);
HttpResponse res(client);
int is_valid = req.parse();
if(is_valid != 0){
printf("parsing error: %d\n", is_valid);
res.status(400);
res.send("Bad Request!");
}else{
String key(req.getMethod());
key.push(":");
key.push(req.getPath());
key.show();
Server->handle(&req, &res);
printf("\n");
}
delete params;
return 0;
}