-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
428 lines (376 loc) · 15.1 KB
/
server.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//
// Created by mouha on 30/12/2022.
//
#include "threadpool.h"
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#define VERSION "HTTP/1.1"
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
//function initial
char *get_mime_type(char *name);
void build_response(int, char *, char *, char *, char *, int, char *, char *, int);
int read_request(int sd);
int get_index(char *str);
void socketConnect(int, int, int);
int main(int argc, char *argv[]) {
//check if arguments are faulty
if (argc < 4) {
printf("Usage: server <port> <pool-size> <max-number-of-request>\n");
exit(0);
} else if (argc > 4) {
printf("Usage: server <port> <pool-size> <max-number-of-request>\n");
exit(0);
}
//assign numbers to integers
int port = atoi(argv[1]);
int pool_size = atoi(argv[2]);
int Max_From_server = atoi(argv[3]);
if(port<0||pool_size<0||Max_From_server<0){
printf("Usage: server <port> <pool-size> <max-number-of-request>\n");
exit(0);
}
socketConnect(port, pool_size, Max_From_server);
}
void socketConnect(int port, int pool_size, int Max_From_server) {
//connection structs initialization
int sd;
struct sockaddr_in serv_addr;
serv_addr.sin_family = PF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
//socket innit
if ((sd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
int bin;
//bind
bin = bind(sd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (bin < 0) {
perror("bind");
exit(1);
}
//create threadpool
threadpool *pool = create_threadpool(pool_size);
if(pool == NULL) {
perror("threadpool");
exit(1);
}
//listen to connections from servers
int lis = listen(sd, Max_From_server);
if (lis == -1) {
perror("listen");
exit(1);
}
int i = 0;
intptr_t connection[Max_From_server];
//check until max number of requests is reached
while (i < Max_From_server) {
int clientLen = sizeof(serv_addr);
if ((connection[i] = accept(sd, (struct sockaddr *) &serv_addr, (socklen_t*)&clientLen)) < 0) {
perror("accept");
} else {
dispatch(pool, (dispatch_fn) read_request, (void *) connection[i]);
}
i++;
}
//destroy threadpool and close socket
destroy_threadpool(pool);
close(sd);
}
int read_request(int sd) {
//usefull inits
char request[1024] = {0};
memset(request, 0, sizeof(request));
int reading;
int status;
char *phrase;
time_t now;
char timebuf[128] = { 0};
now = time(NULL);
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
//read the request into the request buffer
while ((reading = read(sd, request, sizeof(request))) >= 0) {
if (reading == -1) {
perror("read");
exit(1);
}
//stop at \r\n
if (reading > 0) {
request[get_index(request)] = '\0';
break;
}
}
//check if the requests contains of 2 spaces, so we make sure it is not a faulty response
int i = 0, space_count = 0;
while (i < strlen(request)) {
if (request[i] == ' ')
space_count++;
i++;
}
char *method = strtok(request, " ");
char *path = strtok(NULL, " ");
char *protocol = strtok(NULL, " ");
//check request for error 400
if (method == NULL || path == NULL || protocol == NULL || space_count != 2 || strcmp(protocol, "HTTP/1.0") != 0) {
status = 400;
phrase = "Bad Request";
build_response(status, phrase, timebuf, NULL, path, 113, "400 Bad Request", "Bad Request.", sd);
return status;
}
//check for error 501
if (strcmp(method, "GET") != 0) {
status = 501;
phrase = "Not supported";
build_response(status, phrase, timebuf, NULL, path, 129, "501 Not supported", "Method is not supported.", sd);
return status;
}
//path2 for path that can be entered
char path2[1024] = {0};
memset(path2, 0, sizeof(path2));
path2[0] = '.';
strcat(path2, path);
DIR *dir = opendir(path2);
struct stat file_stats;
//if it is a file or no permissions:
if (dir == NULL) {
//temp is stores every directory from the beginning till the end running on it slash slash
char *temp = (char *) malloc(sizeof(char) * 1024);
if(temp == NULL){
status = 500;
phrase = "Internal Server Error.";
build_response(status, phrase,timebuf,NULL,path2,144,phrase,"Some server side error",sd);
}
memset(temp, 0, sizeof(char) * 1024);
i = 0;
temp[1] = '/';
while (i < strlen(path2)) {
while (path2[i] != '/' && i < strlen(path2)) {
temp[i] = path2[i];
i++;
}
//if not found
if (stat(temp, &file_stats) < 0) {
status = 404;
phrase = "Not Found";
build_response(status, phrase, timebuf, NULL, path2, 112, "404 Not Found", "File not found.", sd);
break;
}
//if it is a file then check if the file has read permissions
else if (S_ISREG(file_stats.st_mode)) {
if (!((file_stats.st_mode & S_IROTH) && (file_stats.st_mode & S_IRGRP) &&
(file_stats.st_mode & S_IRUSR))) {
status = 403;
phrase = "Forbidden";
build_response(status, phrase, timebuf, NULL, path2, 111, "403 Forbidden", "Access denied.", sd);
break;
}
else{
status = 200;
phrase = "OK";
char lastMod[128] = {0};
strftime(lastMod, sizeof(lastMod), RFC1123FMT, gmtime(&file_stats.st_mtime));
build_response(status, phrase, timebuf, lastMod, temp, (int) file_stats.st_size, NULL, NULL, sd);
break;
}
//check if path is a directory then check if it has read permissions
} else if (S_ISDIR(file_stats.st_mode)) {
if ((!(file_stats.st_mode & S_IXOTH))) {
status = 403;
phrase = "Forbidden";
build_response(status, phrase, timebuf, NULL, path2, 111, "403 Forbidden", "Access denied.", sd);
break;
}
}
temp[i] = '/';
i++;
}
free(temp);
}
//if directory is found
else {
//check if it has leading slash or not
if (path2[strlen(path2) - 1] != '/') {
status = 302;
build_response(status, "Found", timebuf, NULL, path2, 123, NULL, NULL, sd);
return 1;
} else {
//check if it has index.html
struct stat file_stats2;
char path3[1024] = {0};
strcpy(path3, path2);
strcat(path2, "index.html");
int s = stat(path2, &file_stats2);
//check if file is found and it is a file
if (s >= 0 && S_ISREG(file_stats2.st_mode)) {
// if ((file_stats2.st_mode & S_IROTH) && (file_stats2.st_mode & S_IRGRP) &&
// (file_stats2.st_mode & S_IRUSR)) {
status = 200;
phrase = "OK";
char lastMod[128] = {0};
memset(lastMod, 0, 128);
strftime(lastMod, sizeof(lastMod), RFC1123FMT, gmtime(&file_stats.st_mtime));
build_response(status, phrase, timebuf, lastMod, path2, (int) file_stats2.st_size, NULL, NULL, sd);
memset(path2, 0, sizeof(path2));
//}
} else {
//if directory found build the html table for dir content
struct stat fold;
stat(path3, &fold);
char lastMod2[128] = {0};
strftime(lastMod2, sizeof(lastMod2), RFC1123FMT, gmtime(&fold.st_mtime));
char header[4096] = {0};//priint header
sprintf(header,
"HTTP/1.1 200 OK\r\nServer: webserver/1.0\r\nDate: %s\r\nContent-Type: text/html\r\ncontent-length: %d\r\nLast-Modified: %s\r\nConnection: Close\r\n\r\n",
timebuf,(int)file_stats2.st_size, lastMod2);
if((write(sd, header, sizeof(header)))==-1){
perror("Write");
exit(1);
}
struct stat st;
struct dirent *dirent;
char body[4096] = {0};
//print html
sprintf(body,
"<html><head><title>Index of %s</title></head>\n<body><h4>Index of %s</h4>\n<table CELLSPACING=8>\n<tr><th>Name</th><th>Last Modified</th><th>Size</th></tr>\n",
path3, path3);
//write html to socket
if(((write(sd, body, sizeof(body))))==-1){
perror("Write");
exit(1);
}
//while loop to read directory content and add the content to the html header
while ((dirent = readdir(dir)) != NULL) {
bzero(body, sizeof(body));
char table_temp[strlen(path3) + strlen(dirent->d_name) + 1];
bzero(table_temp, sizeof(table_temp));
strcat(table_temp, path3);
strcat(table_temp, dirent->d_name);
stat(table_temp, &st);
bzero(timebuf, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&st.st_mtime));
if (!S_ISDIR(st.st_mode)) {
sprintf(body, "<tr><td><A HREF=\"%s\">%s</A></td><td>%s</td><td>%d</td></tr>\n", dirent->d_name,
dirent->d_name, timebuf, (int) st.st_size);
} else {
//strcat(table_temp, "/");
sprintf(body, "<tr><td><A HREF=\"%s/\">%s</A></td><td>%s</td><td>\n", dirent->d_name,
dirent->d_name, timebuf);
}
if(((write(sd, body, strlen(body)))==-1)) {
perror("Write");
exit(1);
}
}
//last body to html
sprintf(body, "</table>\n\n<HR>\n\n<ADDRESS>webserver/1.0</ADDRESS>\n\n</body></html>");
if(((write(sd, body, strlen(body)))==-1)) {
perror("Write");
exit(1);
}
}
}
}
//closer directory in the end
closedir(dir);
return 0;
}
//get the index of the \r\n
int get_index(char *str) {
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '\r' && str[i + 1] == '\n') {
return i;
}
}
return -1;
}
//function responsible for building the response
void
build_response(int status, char *phrase, char *time, char *last_mod, char *path, int length, char *body, char *last,
int socket) {
char response[1024];
memset(response, 0, sizeof(response));
//for these status
if (status == 404 || status == 501 || status == 400 || status == 403||status == 500) {
sprintf(response, "%s %d %s\r\n" //version - status - phrase
"Server: webserver/1.0\r\n"
"Date: %s\r\n" //time
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n"
"<HTML><HEAD><TITLE>%s</TITLE></HEAD>\r\n"
"<BODY><H4>%s</H4>\r\n"
"%s\r\n"
"</BODY></HTML>"
"\r\n", VERSION, status, phrase, time, "text/html", length, body, body, last);
}
//if status is 200
else if (status == 200) {
sprintf(response, "%s %d %s\r\n"
"Server: webserver/1.0\r\n"
"%s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"%s\r\n"
"Connection: close\r\n"
"\r\n", VERSION, status, phrase, time, get_mime_type(path), length, last_mod);
} else if (status == 302) {
sprintf(response, "%s %d %s\r\n"
"Server: webserver/1.0\r\n"
"Date: %s\r\n"
"Location: %s/\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n"
"<HTML><HEAD><TITLE>302 Found</TITLE></HEAD>\r\n"
"<BODY><H4>302 Found</H4>\r\n"
"Directories must end with a slash.\r\n"
"</BODY></HTML>", VERSION, status, phrase, time,path, "text/html", length);
}
if((write(socket, response, strlen(response)))<0){
perror("Write");
exit(1);
}
if (status == 200) {
//if it is a file open it then read the content then write it to the socket
int f = open(path, O_RDONLY);
if (f == -1){
perror("open");
exit(1);
}
unsigned char file_read[4096] = {0};
size_t r;
while ((r = read(f, file_read, sizeof(file_read))) > 0) {
if (write(socket, file_read, r) < 0) {
perror("Write");
exit(1);
}
memset(file_read,0,sizeof(file_read));
}
close(f);
}
}
char *get_mime_type(char *name) {
char *ext = strrchr(name, '.');
if (!ext) return NULL;
if (strcmp(ext, ".html") == 0 || strcmp(ext, ".htm") == 0) return "text/html";
if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) return "image/jpeg";
if (strcmp(ext, ".gif") == 0) return "image/gif";
if (strcmp(ext, ".png") == 0) return "image/png";
if (strcmp(ext, ".css") == 0) return "text/css";
if (strcmp(ext, ".au") == 0) return "audio/basic";
if (strcmp(ext, ".wav") == 0) return "audio/wav";
if (strcmp(ext, ".avi") == 0) return "video/x-msvideo";
if (strcmp(ext, ".mpeg") == 0 || strcmp(ext, ".mpg") == 0) return "video/mpeg";
if (strcmp(ext, ".mp3") == 0) return "audio/mpeg";
return NULL;
}