Skip to content

Commit ea12055

Browse files
committed
Added details in README and sent in response 404 when file not found
1 parent d0f9f16 commit ea12055

File tree

11 files changed

+97
-16
lines changed

11 files changed

+97
-16
lines changed

Assignments/Assignment2/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Concurrent Webserver
2+
3+
### Task Requirement:
4+
Write a program [webserver.c](./webserver.c) using threads and sockets supporting HTTP GET requests which works in
5+
the following way:
6+
- It creates a pool of threads in the beginning
7+
- Main thread waits on `epoll_wait` for any I/O on sockets
8+
- It stores the requests in a message queue used by all the threads.
9+
- Each client is supposed to go through following stages
10+
- READING_REQUEST
11+
- HEADER_PARSING
12+
- READING_DISKFILE
13+
- WRITING_HEADER
14+
- WRITING_BODY
15+
- After WRITING_HEADER stage, client cycles between READING_DISKFILE and WRITING_BODY stage until full file is transferred
16+
- Connection is not closed until server receives an EOF (i.e. client itself has closed the connection).
17+
- I/O on sockets is non blocking, but reading from file is blocking since it'll always succeed.
18+
19+
### Server performance
20+
21+
Testing is done using `httpref` which can be installed via `sudo apt install httpref`
22+
23+
Some snapshots for the statistics can be found here :
24+
25+
1. Number of connections : 100, Rate : 10, File size : 1450 bytes
26+
27+
<img src="./statistics/conn_100_rate_10.png">
28+
29+
2. Number of connections : 3000, Rate : 50, File size : 1450 bytes
30+
31+
<img src="./statistics/conn_3000_rate_50.png">
32+
33+
3. Number of connections : 10000, Rate : 50, File size : 1509793 bytes (15.1 MB)
34+
35+
<img src="./statistics/conn_10000_rate_50.png">
36+
37+
### Demo Run
38+
39+
<img src="./demo.gif">
40+
41+
### Dependencies:
42+
- Linux kernal v4.15.0 (or higher)
43+

Assignments/Assignment2/README.pdf

385 KB
Binary file not shown.

Assignments/Assignment2/demo.gif

19.2 MB
Loading

Assignments/Assignment2/files/static/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function loadImgs(names, callback) {
189189
let name = names[n];
190190
result[n] = document.createElement('img');
191191
result[n].addEventListener('load', onload);
192-
result[n].src = "../../static/images/" + name + ".png";
192+
result[n].src = "./static/images/" + name + ".png";
193193
}
194194
}
195195

Assignments/Assignment2/helper.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ ssize_t Read(int fd, void *buf, size_t count){
9696
int numRead = read(fd, buf, count);
9797
if( numRead < 0){
9898
if(errno != EWOULDBLOCK && errno != EAGAIN){
99-
errorExit("read failed");
99+
if(errno != ECONNRESET && errno != EBADF)
100+
errorExit("read failed");
100101
}
101102
}
102103

@@ -108,13 +109,26 @@ ssize_t Write(int fd, const void *buf, size_t count){
108109

109110
if(numWritten < 0){
110111
if(errno != EWOULDBLOCK && errno != EAGAIN){
111-
errorExit("write failed");
112+
if(errno != ECONNRESET && errno != EBADF)
113+
errorExit("write failed");
112114
}
113115
}
114116

115117
return numWritten;
116118
}
117119

120+
int Close(int fd){
121+
122+
printf("Closing %d\n", fd);
123+
124+
if(close(fd) < 0 ){
125+
if(errno != EBADF)
126+
errorExit("close failed");
127+
}
128+
129+
return SUCCESS;
130+
}
131+
118132
off_t Lseek(int fd, off_t offset, int whence){
119133
off_t offsetVal = lseek(fd, offset, whence);
120134

@@ -262,9 +276,19 @@ char *getMimeType(char *filename){
262276
if (strcmp(extension, "png") == 0) {
263277
return "image/png";
264278
}
265-
if(strcmp(extension, "mp3") == 0) {
279+
if (strcmp(extension, "mp3") == 0) {
266280
return "audio/mpeg";
267281
}
268282

269283
return DEFAULT_MIME_TYPE;
270284
}
285+
286+
char *getTimestamp(){
287+
char *buf = (char*)malloc(sizeof(char) * 1000);
288+
time_t now = time(0);
289+
290+
struct tm tm = *gmtime(&now);
291+
strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S %Z", &tm);
292+
293+
return buf;
294+
}

Assignments/Assignment2/helper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ ssize_t Write(int fd, const void *buf, size_t count);
8686

8787
off_t Lseek(int fd, off_t offset, int whence);
8888

89+
int Close(int fd);
90+
8991
/* epoll functions */
9092

9193
int Epoll_create1(int flags);
@@ -114,4 +116,6 @@ char *tolowerStr(char *str);
114116

115117
char *getMimeType(char *filename);
116118

119+
char *getTimestamp();
120+
117121
#endif

Assignments/Assignment2/script.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
gcc -pthread -o server hashtable.c helper.c webserver.c
2-
gcc -o client client.c
32
./server
Loading
Loading
Loading

0 commit comments

Comments
 (0)