-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.c
550 lines (491 loc) · 12.9 KB
/
connection.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "connection.h"
int r_count = 0;
void err_n_die(const char *fmt, ...)
{
int errno_save;
va_list ap;
// any system or library call can errno, so we need to save it now
errno_save = errno;
// print out the fmt+args to standard out
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
fprintf(stdout, "\n");
fflush(stdout);
// print our error message is errno was set.
if (errno_save != 0)
{
fprintf(stdout, "(errno = %d) : %s\n", errno_save, strerror(errno_save));
fprintf(stdout, "\n");
fflush(stdout);
}
va_end(ap);
// this is the ..and_die part, Terminate with an error.
exit(1);
}
ssize_t readline(int fd, void *buf, size_t maxlen)
{
char c;
char *bufp = buf;
int n;
for (n = 0; n < maxlen - 1; n++)
{ // leave room at end for '\0'
int rc;
if ((rc = read(fd, &c, 1)) == 1)
{
*bufp++ = c;
if (c == '\n')
break;
}
else if (rc == 0)
{
if (n == 1)
return 0; /* EOF, no data read */
else
break; /* EOF, some data was read */
}
else
return -1; /* error */
}
*bufp = '\0';
return n;
}
int open_server_connection()
{
// Create a socket descriptor
int listen_fd;
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return -1;
}
// Eliminates "Address already in use" error from bind
int opt_val = 1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt_val, sizeof(int)) < 0)
{
return -2;
}
// Endpoint for all requests to port on any IP address for this host
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT); /* server port */
if ((bind(listen_fd, (SA *)&server_addr, sizeof(server_addr))) < 0)
{
return -3;
}
if ((listen(listen_fd, 1024)) < 0)
{
// accepts connection requests
return -4;
}
return listen_fd;
}
int open_client_connection(char *hostname, int port)
{
int client_fd;
struct hostent *hp;
struct sockaddr_in server_addr;
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1;
if ((hp = gethostbyname(hostname)) == NULL)
return -2;
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
bcopy((char *)hp->h_addr, (char *)&server_addr.sin_addr.s_addr, hp->h_length);
server_addr.sin_port = htons(port);
// Establish a connection with the server
if (connect(client_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
return -1;
return client_fd;
}
// request error
void request_error(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg)
{
char buf[MAXLINE], body[MAXLINE];
// Create the body of error message first (have to know its length for header)
sprintf(body,
""
"<!doctype html>\r\n"
"<head>\r\n"
" <title>IIITH WebServer Error</title>\r\n"
"</head>\r\n"
"<body>\r\n"
" <h2>%s: %s</h2>\r\n"
" <p>%s: %s</p>\r\n"
"</body>\r\n"
"</html>\r\n",
errnum, shortmsg, longmsg, cause);
// Write out the header information for this response
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error.");
}
sprintf(buf, "Content-Type: text/html\r\n");
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error.");
}
sprintf(buf, "Content-Length: %lu\r\n\r\n", strlen(body));
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error.");
}
// Write out the body last
if (write(fd, body, strlen(body)) < 0)
{
err_n_die("write error.");
}
}
//
// Reads and discards everything up to an empty text line
//
void request_read_headers(int fd)
{
char buf[MAXLINE];
if (readline(fd, buf, MAXLINE) < 0)
{
err_n_die("readline error.");
}
while (strcmp(buf, "\r\n"))
{
if (readline(fd, buf, MAXLINE) < 0)
{
err_n_die("readline error.");
}
}
return;
}
//
// returns 1 if reader, 2 if writer else -1.
//
int request_parse_uri(char *uri, int *line_num, char *content)
{
char *ptr;
int index = 0;
char line[MAXLINE];
if (strlen(uri) == 1 && uri[0] == '/')
{
// root
strcat(content, "./index.html");
return 0;
}
if (strstr(uri, "/reader") != NULL) {
// reader
if ((ptr = strstr(uri, "line_num")) == NULL)
{
return -1;
}
ptr += strlen("line_num") + 1;
while (*ptr != '\0' && '0' <= *ptr && *ptr <= '9')
{
line[index++] = (*ptr);
ptr++;
}
line[index] = '\0';
*line_num = atoi(line);
return 1;
}
if ((strstr(uri, "/writer") != NULL) || (strstr(uri, "/docs_writer") != NULL)) {
// writer
if ((ptr = strstr(uri, "line_num")) == NULL)
{
return -1;
}
ptr += strlen("line_num") + 1;
while (*ptr != '\0' && '0' <= *ptr && *ptr <= '9')
{
line[index++] = (*ptr);
ptr++;
}
line[index] = '\0';
*line_num = atoi(line);
if ((ptr = strstr(uri, "content")) == NULL)
{
return -1;
}
ptr += strlen("content") + 1;
index = 0;
while (*ptr != '\0')
{
if (*ptr != '+')
{
content[index++] = *ptr;
}
else
{
content[index++] = ' ';
}
ptr++;
}
content[index] = '\0';
return 2;
}
if (strstr(uri, "docs_reader") != NULL) {
// call read_all
return 0;
}
if (strstr(uri, "?") == NULL) {
// That represents that we are not receiving any query.
content[0] = '\0';
strcat(content, ".");
strcat(content, uri);
return 0;
}
return -1;
}
//
// Find the file type
//
void request_get_filetype(char *filename, char *filetype)
{
if (strstr(filename, ".html"))
strcpy(filetype, "text/html");
else
strcpy(filetype, "text/plain");
}
void request_serve_static(int fd, char *filename, int filesize) {
int srcfd;
char *srcp, filetype[MAXLINE], buf[MAXLINE];
request_get_filetype(filename, filetype);
if ((srcfd = open(filename, O_RDONLY, 0)) < 0)
{
err_n_die("open error.");
}
// Rather than call read() to read the file into memory,
// which would require that we allocate a buffer, we memory-map the file
if ((srcp = mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0)) < 0)
{
err_n_die("mmap error.");
}
if (close(srcfd) < 0)
{
err_n_die("close error.");
}
// put together response
sprintf(buf,
""
"HTTP/1.0 200 OK\r\n"
"Server: IIITH WebServer\r\n"
"Content-Length: %d\r\n"
"Content-Type: %s\r\n\r\n",
filesize, filetype);
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error.");
}
// Writes out to the client socket the memory-mapped file
if (write(fd, srcp, filesize) < 0)
{
err_n_die("write error.");
}
if (munmap(srcp, filesize) < 0)
{
err_n_die("munmap error.");
}
}
/* Function to read the data present at the given line number
* and send the data as response */
void read_line_file(int fd, int line_number)
{
// semaphore to be added here.
FILE *file = fopen("data.txt", "r");
if (file == NULL)
{
err_n_die("Unable to open the file");
}
char buf[MAXLINE], header[MAXLINE];
int line = 1, found = 0;
while (fgets(buf, MAXLINE, file))
{
if (line == line_number)
{
found = 1;
break;
}
line++;
}
if (found)
{
sprintf(header,
""
"HTTP/1.0 200 OK\r\n"
"Server: IIITH WebServer\r\n"
"Content-Length: %ld\r\n"
"Content-Type: text/plain\r\n\r\n",
strlen(buf));
if (write(fd, header, strlen(header)) < 0)
{
err_n_die("write error");
}
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error");
}
}
else
{
err_n_die("Unable to read this line");
}
}
void edit_files(int fd, int lno, char* newln, char* uri)
{
const char *filename = "data.txt";
char temp[] = "temp.txt";
char str[MAXLINE];
char header[MAXLINE];
newln = strcat(newln, "\n");
int found = 0;
FILE *fptr1, *fptr2;
int linectr = 0;
fptr1 = fopen(filename, "r");
fptr2 = fopen(temp, "w");
while (!feof(fptr1))
{
strcpy(str, "\0");
fgets(str, MAXLINE, fptr1);
if (!feof(fptr1))
{
linectr++;
if (linectr != lno)
fprintf(fptr2, "%s", str);
else
{
found = 1;
fprintf(fptr2,"%s",newln);
}
}
}
if(found){
if (strstr(uri, "docs_writer") == NULL) {
sprintf(header,
""
"HTTP/1.0 200 OK\r\n"
"Server: IIITH WebServer\r\n"
"Content-Length: %ld\r\n"
"Content-Type: \r\n\r\n",
strlen(newln));
if (write(fd, header , strlen(header))< 0){
err_n_die("write error");
}
if (write(fd, newln , strlen(newln))< 0){
err_n_die("write error") ;
}
}
}
else
{
err_n_die("Unable to read this line");
}
fclose(fptr1);
fclose(fptr2);
remove(filename);
rename(temp,filename);
if (strstr(uri, "docs_writer") != NULL) {
read_all(fd);
}
}
void read_all(int fd)
{
int srcfd;
struct stat st;
char *srcp, buf[MAXLINE];
stat("data.txt", &st);
if ((srcfd = open("data.txt", O_RDONLY, 0)) < 0)
{
err_n_die("open error.");
}
if ((srcp = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, srcfd, 0)) < 0)
{
err_n_die("mmap error.");
}
if (close(srcfd) < 0)
{
err_n_die("close error.");
}
sprintf(buf,
""
"HTTP/1.0 200 OK\r\n"
"Server: IIITH WebServer\r\n"
"Content-Length: %ld\r\n"
"Content-Type: text/plain\r\n\r\n",
st.st_size );
if (write(fd, buf, strlen(buf)) < 0)
{
err_n_die("write error");
}
if (write(fd, srcp, st.st_size) < 0)
{
err_n_die("write error.");
}
if (munmap(srcp, st.st_size) < 0)
{
err_n_die("munmap error.");
}
}
// handle a request
void handle_request(int fd)
{
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
if (readline(fd, buf, MAXLINE) < 0)
{
err_n_die("readline error.");
}
sscanf(buf, "%s %s %s", method, uri, version);
printf("method:%s uri:%s version:%s\n", method, uri, version);
fflush(stdout);
if (strcasecmp(method, "GET"))
{
request_error(fd, method, "501", "Not Implemented", "server does not implement this method");
return;
}
// Start by reading headers.
request_read_headers(fd);
int line_num = -1;
char content[MAXLINE];
int is_reader = request_parse_uri(uri, &line_num, content);
if (is_reader == -1)
{
request_error(fd, "path", "404", "Not Found", "server could not find the requested path");
return;
}
if (is_reader == 0) {
// serve static files.
if (strstr(uri, "docs_reader") != NULL) {
read_all(fd);
return;
}
char* filename = content;
struct stat st;
if (stat(filename, &st) < 0) {
// Just do a safe return from here.
return;
}
request_serve_static(fd, filename, st.st_size);
} else if (is_reader == 1) {
// request_serve_reader(fd, line_num);
sem_wait(&mutex1);
r_count++;
if (r_count == 1)
sem_wait(&wrt);
sem_post(&mutex1);
read_line_file(fd, line_num);
// sleep(1); // this is for testing
sem_wait(&mutex1);
r_count--;
if (r_count == 0)
{
sem_post(&wrt);
}
sem_post(&mutex1);
} else {
// writer
sem_wait(&wrt);
edit_files(fd, line_num, content, uri);
// sleep(1) ; // this is for testing
sem_post(&wrt);
}
}