-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
510 lines (454 loc) · 15.7 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//server.c file
//Name: Yahya Saad
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "threadpool.h"
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
//---------some macros---------
#define VERSION1 "HTTP/1.0"
#define BUF_SIZE 4096 // buffer size supposed to get max size up to 4096 --> 2^12
#define RESPONSE 512// max response size
#define HTML 255 // 2^8 - 1
#define MAX_PORT_VAL 9999
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" //construct the date header
#define STATUS 255// 2^8 - 1
#define EMPTY 0// set content and some char* to empty
#define ENTITY_LINE 500 //length of the first line in the request is no more than 500
//---status code parameters---
#define FOUND 302 // if the error is 302 then add location header, otherwise omit the location header
//FOUND response too, Missing "/" at the end of the requested directory
#define INTERNAL 500 //"Internal Server Error"
#define BAD_REQ 400 //"Bad request"
#define NOT_SUPP 501//Method is not "GET" -> this code run GET method only
#define NOT_FOUND 404 //File not found
#define FORBIDDEN 403 // Cannot access file.(not regular file or no permissions)
#define OK 200 //Proper request GET the request in normal situation
typedef enum
{
false,
true
} bool; //bool struct: set a boolean value to an important flags
char timebuf[128]; // return the current time through another function
//---------private functions---------
void printUsage();//usage error
void systemError(char *); //return error msg
char *getTime();//returns the current date and time
char *get_mime_type(char *);// check the content type is the mime type of the response body
int connect_to_server(void*);//modifying the content with client request and set the response
//-----------------------------------
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;
}
int connect_to_server(void* in)
{
//---response content parameters---
char* res_header=NULL;
unsigned char* res_body=NULL;
char* res_type=NULL;
int status=0;//status code for each response
bool errorFlag=false; // error flag
//-------------------
//---file parameters---
int stream=*(int*)in;
struct stat flop;
struct stat fs;
int file=EMPTY;
struct dirent* dentry;
DIR* dir;
bool fileFlag=false;
//-------------------
bool contentFlag=false;
//---request content parameters---
char* req_path=NULL;
char* req_protocol=NULL;
char* req_method=NULL;
//---------------------
int size=EMPTY;
// in case accept failed in main failed
if(stream<0){
perror("ERROR on accept");
errorFlag=true;
status=INTERNAL;
res_header="Internal Server Error";
res_body="Some server side error.";
res_type="text/html";
}
char request[BUF_SIZE]={EMPTY};// build the request with make buffer size
int rc=0;
rc=read(stream,request,BUF_SIZE);
// in case read failed
if(rc < 0){
perror("Read failed");
errorFlag=true;
status=INTERNAL;
res_header="Internal Server Error";
res_body="Some server side error.";
res_type="text/html";
}
if(rc == 0){
return EXIT_SUCCESS;
}
char buildReq[2]=" "; //get request tokens - start building it
req_method=strtok(request,buildReq);
req_path=strtok(NULL,buildReq);
req_protocol=strtok(NULL,buildReq);
if(req_protocol!=NULL){
int count=0;
while(req_protocol[count]!='\r')
count++;
req_protocol[count]='\0';
}
//In case the request is wrong, send a “400 Bad Request" response
if(req_method==NULL || req_path==NULL || req_protocol==NULL){
errorFlag=true;
status=BAD_REQ;
res_header="Bad Request";
res_body="Bad Request.";
res_type="text/html";
}
//HTTP versions
else if(strncmp(req_protocol,VERSION1,strlen(req_protocol))!=0 ){
errorFlag=true;
status=BAD_REQ;
res_header="Bad Request";
res_body="Bad Request.";
res_type="text/html";
}
else if(strncmp(req_method,"GET",strlen(req_method))!=0){//GET requests only
errorFlag=true;
status=NOT_SUPP;
res_header="Not supported";
res_body="Method is not supported.";
res_type="text/html";
}
if(errorFlag==false){
//create full path
char createPath[STATUS]={EMPTY};
createPath[0]='.';
strcat(createPath,req_path);
//create dir content
char dirContent[STATUS]={EMPTY};
dirContent[0]='.';
strcat(dirContent,req_path);
dir=opendir(createPath);
// in case path is not directory or no permissions
if(dir==NULL){
char* mkdir = (char*)calloc(sizeof(createPath),sizeof(char));
int index = 0;
while(index < strlen(createPath))
{
while(createPath[index] != '/' && index < strlen(createPath))
{
mkdir[index] = createPath[index];
index++;
}
if(stat(mkdir,&flop) < 0)
{
errorFlag=true;
status=NOT_FOUND;
res_header="Not Found";
res_body="File not found.";
res_type="text/html";
break;
}
if(S_ISREG(flop.st_mode))
{
if((flop.st_mode & S_IROTH) && (flop.st_mode & S_IRGRP) && (flop.st_mode & S_IRUSR)){}
else
{
errorFlag=true;
status=FORBIDDEN;
res_header="Forbidden";
res_body="Access denied.";
res_type="text/html";
break;
}
}
else if(S_ISDIR(flop.st_mode))
{
if(flop.st_mode & S_IXOTH ){}
else
{
errorFlag=true;
status=FORBIDDEN;
res_header="Forbidden";
res_body="Access denied.";
res_type="text/html";
break;
}
}
mkdir[index]='/';
index++;
}
free(mkdir);
// return file
if(status==0)
{
file=open(createPath,O_RDONLY);
stat(createPath,&fs);
fileFlag=true;
status=OK;
res_header="OK";
res_type=get_mime_type(req_path);
size=(int)fs.st_size;
res_body=(unsigned char*)calloc(size+1,sizeof(unsigned char));
read(file,res_body,size);
}
}
// directory found
else
{
// 302 ERROR
if(req_path[strlen(req_path)-1]!='/')
{
errorFlag=true;
status=FOUND;
res_header="Found";
res_body="Directories must end with a slash.";
res_type="text/html";
closedir(dir);
}
// directory found
else
{
// index.html file
strcat(createPath,"index.html");
file=open(createPath,O_RDONLY);
if(file>EMPTY)
{
stat(createPath,&fs);
status=OK;
res_header="OK";
res_type="text/html";
fileFlag=true;
size=(int)fs.st_size;
res_body=(unsigned char*)calloc(size+1,sizeof(unsigned char));
read(file,res_body,size);
closedir(dir);
}
// no index.html, return contents of the directory
else
{
stat(dirContent,&fs);
status=OK;
res_header="OK";
res_type="text/html";
fileFlag=true;
contentFlag=true;
while((dentry=readdir(dir))!=NULL)
{
size+=ENTITY_LINE;
}
closedir(dir);
res_body=(unsigned char*)calloc((size+1+RESPONSE),sizeof(unsigned char));
strcat(res_body,"<HTML>\r\n<HEAD><TITLE>Index of ");
strcat(res_body,dirContent);
strcat(res_body,"</TITLE></HEAD>\r\n\r\n<BODY>\r\n<H4>Index of ");
strcat(res_body,dirContent);
strcat(res_body,"</H4>\r\n\r\n<table CELLSPACING=8>\r\n<tr><th>Name</th><th>Last Modified</th><th>Size</th></tr>\r\n");
dir=opendir(dirContent);
while((dentry=readdir(dir))!=NULL)
{
//set a temporary parameter to strcat dirContent
char* tmp=(char*)calloc(strlen(dirContent)+1+strlen(dentry->d_name),sizeof(char));
strcat(tmp,dirContent);
strcat(tmp,dentry->d_name);
stat(tmp,&flop);
strcat(res_body,"<tr>\r\n<td><A HREF=");
strcat(res_body,dentry->d_name);
strcat(res_body,">");
strcat(res_body,dentry->d_name);
strcat(res_body,"</A></td><td>");
time_t now = flop.st_mtime;
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
strcat(res_body,timebuf);
strcat(res_body,"</td>\r\n<td>");
if(!S_ISDIR(flop.st_mode))
{
int time=(int)flop.st_size;
sprintf(timebuf,"%d",time);
strcat(res_body,timebuf);
}
strcat(res_body,"</td>\r\n</tr>");
free(tmp);
}
strcat(res_body,"\r\n\r\n</table>\r\n\r\n<HR>\r\n\r\n<ADDRESS>webserver/1.0</ADDRESS>\r\n\r\n</BODY></HTML>\r\n");
closedir(dir);
}
}
}
}
char status_response[STATUS]={EMPTY};//response status
char html[HTML]={EMPTY};// HTML length
if(errorFlag==true)
{
//HTML Text
strcat(html,"<HTML><HEAD><TITLE>");
sprintf(status_response,"%d",status);
strcat(html,status_response);
strcat(html," ");
strcat(html,res_header);
strcat(html,"</TITLE></HEAD>\r\n<BODY><H4>");
strcat(html,status_response);
strcat(html," ");
strcat(html,res_header);
strcat(html,"</H4>\r\n");
strcat(html,res_body);
strcat(html,"\r\n</BODY></HTML>\r\n");
}
//Response headers
char response[RESPONSE]={EMPTY};
strcat(response,"HTTP/1.1 ");
sprintf(status_response,"%d",status);
strcat(response,status_response);
strcat(response," ");
strcat(response,res_header);
strcat(response,"\r\nServer: webserver/1.0\r\nDate: ");
char *dateTime = getTime();
strcat(response, dateTime);
if(status==FOUND){
strcat(response,"\r\nLocation: ");
strcat(response,req_path);
strcat(response,"/");
}
if(res_type!=NULL)
{
strcat(response,"\r\nContent-Type: ");
strcat(response,res_type);
}
strcat(response,"\r\nContent-Length: ");
if(errorFlag==true)
{
sprintf(status_response,"%ld",strlen(html));
strcat(response,status_response);
}
else
{
if(contentFlag==true)
sprintf(status_response,"%ld",strlen(res_body));
else
sprintf(status_response,"%d",size);
strcat(response,status_response);
strcat(response,"\r\nLast-Modified: ");
time_t now = fs.st_mtime;
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
strcat(response,timebuf);
}
strcat(response,"\r\nConnection: close\r\n\r\n");
if(errorFlag==true)
strcat(response,html);
write(stream, response, strlen(response));
if(fileFlag==true){
write(stream,res_body,size);
free(res_body);
}
close(stream);
}
int main(int argc,char* argv[]){
if(argc!=4) //4 args as an input
{
printUsage();
exit(EXIT_SUCCESS);
}
//server <port> <pool-size> <max-number-of-request>
int port = atoi(argv[1]);
char* threadsNum=argv[2];
char* reqNum=argv[3];
//invalid input
if(port < 1 || atoi(threadsNum) < 1 || atoi(reqNum)< 1)
{
printUsage();
exit(EXIT_SUCCESS);
}
int stream; // socket fd stream
struct sockaddr_in serv_addr;
stream = socket(AF_INET, SOCK_STREAM, 0);
//check the socket
if (stream < 0){
perror("socket failed");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
//the server listens to requests in any of its addresses.
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
if (port < 0 || port > MAX_PORT_VAL)
{
printUsage();
close(stream);
systemError("Invalid port number");
}
serv_addr.sin_port = htons(port);
//check bind
if(bind(stream,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
systemError("Bind failed");
}
//check listen
if(listen(stream, 5) < 0){ // max = 5
close(stream);
systemError("Listen failed");
}
//set final request with the thread pool
int* finalReq=(int*)calloc(atoi(reqNum),sizeof(int));
if(finalReq==NULL){
systemError("Can't allocate memory");
}
threadpool* thread=create_threadpool(atoi(threadsNum));
if(thread==NULL){
free(finalReq);
systemError("Creating the thread pool failed");
}
struct sockaddr acc_addr; //accept address
int acc_len = sizeof(serv_addr); // accept length
int acc_socket; // accept socket
for(int i=0; i<atoi(reqNum) ;i++)
{
acc_socket = accept(stream, &acc_addr, (socklen_t *)&acc_len);
finalReq[i]=acc_socket;
if(finalReq[i] < 0){
close(stream);
systemError("Error on accept");
}
dispatch(thread,connect_to_server,&finalReq[i]);
}
close(stream);
destroy_threadpool(thread);
free(finalReq);
return EXIT_SUCCESS;
}
//print usage error
void printUsage(){
printf("Usage: server <port> <pool-size> <max-number-of-request>\n"); //Command line usage
}
void systemError(char *str) //return error msg
{
perror(str);
exit(EXIT_FAILURE);
}
char *getTime() // return current time
{
time_t now;
now = time(NULL);
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
return timebuf;
}