-
Notifications
You must be signed in to change notification settings - Fork 5
/
allizom2.c
634 lines (532 loc) · 16.2 KB
/
allizom2.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Course Code 350
*
* A Multithreaded Tiny HTTP Server
* Demonstrating Thread Pool Management
* Following the Thread Pool Management
* Outline from Unix Network Programming Vol 1
* by W. Richard Stevens.
*
* Project accomplished by: Abu Mohammad Omar Shehab Uddin Ayub
* Reg No. 2000 330 096
* Section B
* 3rd Year 2nd Semester
* Dept of CSE, SUST
*
* Idea and guided by: Mahmud Shahriar Hussain
* Lecturer
* Dept of CSE, SUST
*
* Course Instructor: Rukhsana Tarannum Tazin
* Lecturer
* Dept of CSE, SUST
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_CLIENT 5
#define MAX_THREAD 3
#define MAX_REQ_HEADER 10
#define MAX 10
#define MAX_LENGTH 6
#define MIME_LENGTH 20
typedef struct {
pthread_t threadID;
long threadCount;
}Thread;
typedef struct {
char hostName[100];
char filePath[100];
}Request;
typedef struct{
int code;
char date[30];
char server[30];
char last_modified[30];
long content_length;
char content_type[10];
char file_path[80];
}Reply;
Thread thrdPool[MAX_THREAD];
Request request[MAX_THREAD];
Reply reply[MAX_THREAD];
int clntConnection[MAX_CLIENT], clntGet, clntPut;
pthread_mutex_t clntMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t srvrMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t srvrMutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t emitMutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t clntCondition = PTHREAD_COND_INITIALIZER;
static int numThreads;
void *request_handler(void *arg);
int find_method (char *req);
Request parse_request(char *rbuff);
Reply prepare_reply(int code, Request rq);
char *getMimeType(char *str);
void emit_reply(int conn, Reply rply);
int main(void)
{
int i, serverSockfd, clientSockfd;
int serverLen, clientLen;
struct sockaddr_in serverAddress, clientAddress, tempAddress;
//defining number of threads
numThreads = MAX_THREAD;
// initializing queue parameters
clntGet = clntPut = 0;
//creating all the threads
for(i = 0; i < numThreads; i++)
{
pthread_create(&thrdPool[i].threadID, NULL, &request_handler, (void *) i);
} // end of for loop
//creating the socket descriptor
serverSockfd = socket(AF_INET, SOCK_STREAM,0);
if(serverSockfd < 0)
{
error("\nError opening socket");
exit(1);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(80);
serverLen = sizeof(serverAddress);
if(bind(serverSockfd, (struct sockaddr *)&serverAddress, serverLen) < 0)
{
printf("\nError in binding.");
exit(1);
}
listen(serverSockfd, 10);
for( ; ; )
{
fflush(stdout);
//printf("\nWaiting for clients...");
clientLen = sizeof(tempAddress);
//printf("\n\nBlocked on accept()");
clientSockfd = accept(serverSockfd, (struct sockaddr *)&clientAddress, &clientLen);
//printf("\nAllocated client descriptor# %d", clientSockfd);
pthread_mutex_lock(&srvrMutex);
clntConnection[clntPut] = clientSockfd;
if(++clntPut == MAX_CLIENT)
clntPut = 0;
if(clntPut == clntGet)
{
printf("\nCan't handle any more request...\nTerminating...");
exit(1);
}
pthread_cond_signal(&clntCondition);
pthread_mutex_unlock(&srvrMutex);
fflush(stdout);
} // end of infinite for loop
} // end of main
void *request_handler(void *arg)
{
int connfd;
char *reqBuff;
fflush(stdout);
//printf("\nThread %d starting", (int) arg);
for( ; ; )
{
pthread_mutex_lock(&clntMutex);
//printf("\nMutex locked by thread# %d.", (int) arg);
while(clntGet == clntPut)
pthread_cond_wait(&clntCondition, &clntMutex);
connfd = clntConnection[clntGet];
if(++clntGet == MAX_CLIENT)
clntGet = 0;
// thread operation
//printf("\nIn between mutex lock-unlock. Thread# %d, Connection# %d", (int) arg, connfd);
reqBuff = (char *) malloc (1000);
read(connfd, reqBuff, 1000);
int temp;
temp = find_method(reqBuff);
if(temp == 0)
{
//printf("\nfind_method returned 0");
request[(int)arg] = parse_request(reqBuff);
//printf("\nHost: %s\nFile Path: %s", request[(int)arg].hostName, request[(int)arg].filePath);
reply[(int)arg] = prepare_reply(200, request[(int)arg]);
printf("\n\nprinting the reply structure");
//printf("\nCode: %d\nDate: %s\nServer: %s\nlast_modified: %s\ncontent_length: %ld\ncontent type: %s\nfile path: %s", reply[(int)arg].code, reply[(int)arg].date, reply[(int)arg].server, reply[(int)arg].last_modified, reply[(int)arg].content_length, reply[(int)arg].content_type, reply[(int)arg].file_path);
emit_reply(connfd, reply[(int)arg]);
}
else if(temp == 1)
{
printf("\nfind_method returned 1");
reply[(int)arg] = prepare_reply(501, request[(int)arg]);
emit_reply(connfd, reply[(int)arg]);
}
else
{
printf("\nfind_method returned -1");
reply[(int)arg] = prepare_reply(400, request[(int)arg]);
emit_reply(connfd, reply[(int)arg]);
}
pthread_mutex_unlock(&clntMutex);
//printf("\nMutex unlocked by thread# %d.", (int) arg);
thrdPool[(int) arg].threadCount++;
// shahriar bhai told that closing the connection formally is not so important
// good performance achieved @ closing it formally
close(connfd);
}
} // end of request_handler function
void emit_reply(int conn, Reply rply)
{
//pthread_mutex_lock(&emitMutex2);
if(rply.code == 200)
{
char *ok_code = "HTTP/1.1 200 OK\r\n";
write(conn, ok_code, strlen(ok_code));
char *date;
date = (char *)malloc(100);
strcpy(date, "Date: ");
strcat(date, rply.date);
strcat(date, "\r\n");
write(conn, date, strlen(date));
char *server;
server = (char *)malloc(100);
strcpy(server, "Server: ");
strcat(server, rply.server);
strcat(server, "\r\n");
write(conn, server, strlen(server));
char *last_modified;
last_modified = (char *)malloc(100);
strcpy(last_modified, "Last-Modified: ");
strcat(last_modified, rply.last_modified);
strcat(last_modified, "\r\n");
write(conn, last_modified, strlen(last_modified));
char *content_length;
content_length = (char *)malloc(100);
strcpy(content_length, "Content-Length: ");
int decpnt, sign;
char *p;
p = (char *)malloc(20);
p = ecvt(rply.content_length, 15, &decpnt, &sign);
//printf("\nConversion-- %s %d %d", p, decpnt, sign);
strncat(content_length, p, decpnt);
strcat(content_length, "\r\n");
write(conn, content_length, strlen(content_length));
char *content_type;
content_type = (char *)malloc(100);
strcpy(content_type, "Content-Type: ");
strcat(content_type, rply.content_type);
strcat(content_type, "\r\n");
write(conn, content_type, strlen(content_type));
write(conn, "\r\n", 2);
int b;
char c;
FILE *fp;
fp = (FILE *) malloc(sizeof(FILE));
fp = fopen(rply.file_path, "rb");
while(!feof(fp))
{
c = getc(fp);
if(!feof(fp))
{
//printf("%c", c);
write(conn, &c, 1);
}
}
//pthread_mutex_unlock(&emitMutex2);
return;
}
else if(rply.code == 400)
{
char *ok_code = "HTTP/1.1 400 Bad Request\r\n";
write(conn, ok_code, strlen(ok_code));
char *date;
date = (char *)malloc(100);
strcpy(date, "Date: ");
strcat(date, rply.date);
strcat(date, "\r\n");
write(conn, date, strlen(date));
char *server;
server = (char *)malloc(100);
strcpy(server, "Server: ");
strcat(server, rply.server);
strcat(server, "\r\n");
write(conn, server, strlen(server));
write(conn, "\r\n", 2);
char *error_400;
error_400 = (char *)malloc(300);
strcpy(error_400, "<html><head><title>Error No: 400. Bad Request.</title></head><body><h2>The server cannot parse the request.</h2><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><hr>For further info mail to [email protected].</body><html>");
write(conn, error_400, strlen(error_400));
}
else if(rply.code == 501)
{
char *ok_code = "HTTP/1.1 501 Method Not Implemented\r\n";
write(conn, ok_code, strlen(ok_code));
char *date;
date = (char *)malloc(100);
strcpy(date, "Date: ");
strcat(date, rply.date);
strcat(date, "\r\n");
write(conn, date, strlen(date));
char *server;
server = (char *)malloc(100);
strcpy(server, "Server: ");
strcat(server, rply.server);
strcat(server, "\r\n");
write(conn, server, strlen(server));
write(conn, "\r\n", 2);
char *error_501;
error_501 = (char *)malloc(300);
strcpy(error_501, "<html><head><title>Error No: 501. Method Not Implemented.</title></head><body><h2>The server only resolves the GET method.</h2><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><hr>For further info mail to [email protected].</body><html>");
write(conn, error_501, strlen(error_501));
}
else if(rply.code == 404)
{
char *ok_code = "HTTP/1.1 404 File Not Found\r\n";
write(conn, ok_code, strlen(ok_code));
char *date;
date = (char *)malloc(100);
strcpy(date, "Date: ");
strcat(date, rply.date);
strcat(date, "\r\n");
write(conn, date, strlen(date));
char *server;
server = (char *)malloc(100);
strcpy(server, "Server: ");
strcat(server, rply.server);
strcat(server, "\r\n");
write(conn, server, strlen(server));
write(conn, "\r\n", 2);
char *error_404;
error_404 = (char *)malloc(300);
strcpy(error_404, "<html><head><title>Error No: 404. File Not Found.</title></head><body><h2>The server could not found the requested url.</h2><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><hr>For further info mail to [email protected].</body><html>");
write(conn, error_404, strlen(error_404));
}
else if(rply.code == 403)
{
char *ok_code = "HTTP/1.1 403 Forbidden\r\n";
write(conn, ok_code, strlen(ok_code));
char *date;
date = (char *)malloc(100);
strcpy(date, "Date: ");
strcat(date, rply.date);
strcat(date, "\r\n");
write(conn, date, strlen(date));
char *server;
server = (char *)malloc(100);
strcpy(server, "Server: ");
strcat(server, rply.server);
strcat(server, "\r\n");
write(conn, server, strlen(server));
write(conn, "\r\n", 2);
char *error_403;
error_403 = (char *)malloc(300);
strcpy(error_403, "<html><head><title>Error No: 403. Forbidden.</title></head><body><h2>You are not authorized to access this url.</h2><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><hr>For further info mail to [email protected].</body><html>");
write(conn, error_403, strlen(error_403));
}
} //end of emit_reply
char *getMimeType(char *str)
{
printf("\nentered mime type");
int i;
//defined here the file extension
char *fileExt[MAX];
fileExt[0] = "html";
fileExt[1] = "htm";
fileExt[2] = "txt";
fileExt[3] = "gif";
fileExt[4] = "jpg";
fileExt[5] = "qt";
//defined here the mime type
char *mType[MAX];
mType[0] = "text/html";
mType[1] = "text/html";
mType[2] = "text/plain";
mType[3] = "image/gif";
mType[4] = "image/jpeg";
mType[5] = "video/quicktime";
for(i = 0; i < 6; i++)
{
if(strcmp(str, fileExt[i]) == 0)
{
printf("\nmatched");
return mType[i];
}
} //end of for loop
printf("\nreturning ERROR");
char *er = "ERROR";
return er;
} //end of getMimetype
Reply prepare_reply(int code, Request rq)
{
// extracting the file extension
char *drive, *direc, *fname, *ext;
drive = (char *) malloc (3);
direc = (char *) malloc (66);
fname = (char *) malloc (9);
ext = (char *) malloc (5);
Reply rpl;
if(code == 200)
{
rpl.code = 200;
time_t lt;
lt = time(NULL);
strcpy(rpl.date, ctime(<));
rpl.date[strlen(rpl.date) - 1] = '\0';
strcpy(rpl.server, "TinyThreadedServer/0.1a");
char dir[100];
getcwd(dir, 100);
//printf("\ncurrent dir is %s", dir);
//printf("\nfile path is %s", rq.filePath);
strcat(dir, "/");
strcat(dir, rq.filePath);
//printf("\nabsolute file path is %s", dir);
char *path, *p;
path = (char *) malloc (80);
p = (char *) malloc (80);
path = strtok(dir, "/");
strcpy(p, "/");
strcat(p, path);
do{
path = strtok('\0', "/");
if(path)
{
strcpy(ext, path);
strcat(p, "/");
strcat(p, path);
}
} while(path);
//printf("\nthe c++ file path %s", p);
char *e;
e = (char *)malloc(10);
e = strtok(ext, ".");
e = strtok('\0', ".");
//printf("\nfile extension is %s", e);
FILE *fp;
fp = (FILE *) malloc(sizeof(FILE));
if((fp = fopen(p, "r")) == NULL)
{
printf("\nFILE NOT FOUND.\nERROR CODE 404");
rpl.code = 404;
return rpl;
}
struct stat buff;
stat(p, &buff);
//printf("\nSize: %ld\ntime of last modification: %s", buff.st_size, ctime(&buff.st_mtime));
strcpy(rpl.last_modified, ctime(&buff.st_mtime));
rpl.last_modified[strlen(rpl.last_modified) - 1] = '\0';
rpl.content_length = buff.st_size;
if(strcmp(getMimeType(e), "ERROR") == 0)
{
rpl.code = 403;
fclose(fp);
return rpl;
}
strcpy(rpl.content_type, getMimeType(e));
strcpy(rpl.file_path, p);
fclose(fp);
}
else if (code == 400)
{
rpl.code = 400;
time_t lt;
lt = time(NULL);
strcpy(rpl.date, ctime(<));
rpl.date[strlen(rpl.date) - 1] = '\0';
strcpy(rpl.server, "TinyThreadedServer/0.1a");
}
else if (code == 501)
{
rpl.code = 501;
time_t lt;
lt = time(NULL);
strcpy(rpl.date, ctime(<));
rpl.date[strlen(rpl.date) - 1] = '\0';
strcpy(rpl.server, "TinyThreadedServer/0.1a");
}
return rpl;
} //end of prepare_reply
Request parse_request(char *rbuff)
{
char *fileName;
char *hostName;
int j, k, l, m, n;
Request r;
fileName = (char *) malloc (100);
hostName = (char *) malloc (100);
for(j = 5, k = 0; ;j++, k++)
{
if(rbuff[j] == ' ') break;
fileName[k] = rbuff[j];
} //end of for loop
fileName[k]= '\0';
//printf("\nThe valid file path is %s", fileName);
strcpy(r.filePath, fileName);
//finding the host
char *p;
p = strstr(rbuff, "Host: ");
for(l = 0; l < 6; l++)
{
p++;
} //end of for loop
for(m = 0, n = 0; ; m++, n++)
{
if(*p == '\r' || *p == '\n') break;
hostName[n] = *p;
p++;
} //end of for loop
hostName[n]= '\0';
fflush(stdout);
strcpy(r.hostName, hostName);
return r;
} // end of parse_request
int find_method (char *req)
{
pthread_mutex_lock(&srvrMutex2);
//printf("\nin the find_method function\n%s\nthe size of the request is: %d", req, strlen(req));
char *method_list[6];
method_list[0] = (char *) malloc (10);
strcpy(method_list[0], "PUT");
method_list[1] = (char *) malloc (10);
strcpy(method_list[1], "HEAD");
method_list[2] = (char *) malloc (10);
strcpy(method_list[2], "POST");
method_list[3] = (char *) malloc (10);
strcpy(method_list[3], "DELETE");
method_list[4] = (char *) malloc (10);
strcpy(method_list[4], "TRACE");
method_list[5] = (char *) malloc (10);
strcpy(method_list[5], "CONNECT");
int i, j = 0;
char *r;
r = (char *) malloc (10);
for(i = 0; i < strlen(req); i++)
{
if(req[i] == ' ') break;
r[i] = req[i];
} //end of for loop
r[i] = '\0';
//printf("\nExtracted method is %s", r);
if(strcmp(r, "GET") == 0)
{
//printf("\nit's a GET method !!!");
pthread_mutex_unlock(&srvrMutex2);
return 0;
}
for(i = 0; i < 7; i++)
{
if(strcmp(r, method_list[i]) == 0)
{
printf("\n%s matched with %s", r, method_list[i]);
j = 1;
break;
}
} //end of for loop
if(j == 1)
{
pthread_mutex_unlock(&srvrMutex2);
return 1;
}
else
{
pthread_mutex_unlock(&srvrMutex2);
return -1;
}
} // end of find_method