-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincApache_http.c
537 lines (449 loc) · 16.5 KB
/
incApache_http.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
/*
* incApache_http.c: implementazione del protocollo HTTP per il web server
* del corso di SETI
*
* Programma sviluppato a supporto del laboratorio di
* Sistemi di Elaborazione e Trasmissione del corso di laurea
* in Informatica classe L-31 presso l'Universita` degli Studi di
* Genova, per l'anno accademico 2022/2023.
*
* Copyright (C) 2012-2014 by Giovanni Chiola <chiolag@acm.org>
* Copyright (C) 2015-2016 by Giovanni Lagorio <giovanni.lagorio@unige.it>
* Copyright (C) 2016-2022 by Giovanni Chiola <chiolag@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/***
#define DEBUG
***/
#define COOKIE_EXPIRE "; Expires=Wed, 31 Dec 2023 23:59:59 GMT"
#include "incApache.h"
#define OptimizeTCP
#define RESPONSE_CODE_OK 200
#define RESPONSE_CODE_MOVED_PERMANENTLY 301
#define RESPONSE_CODE_NOT_MODIFIED 304
#define RESPONSE_CODE_BAD_REQUEST 400
#define RESPONSE_CODE_NOT_FOUND 404
#define RESPONSE_CODE_INTERNAL_ERROR 500
#define RESPONSE_CODE_NOT_IMPLEMENTED 501
static int CurUID = 0;
static int UserTracker[MAX_COOKIES];
pthread_mutex_t cookie_mutex = PTHREAD_MUTEX_INITIALIZER;
int get_new_UID(void)
{
int retval;
/*** Compute retval by incrementing CurUID mod MAX_COOKIES
*** and reset UserTracker[retval] to 0.
*** Be careful in order to avoid race conditions ***/
/*** TO BE DONE 6.0 START ***/
if(pthread_mutex_lock(&cookie_mutex) != 0)
fail_errno("Errore locking mutex");
int fl=1;
while (fl){
retval = ++CurUID;
if(!UserTracker[retval]){
UserTracker[retval] = 0;
fl=0;
}
}
if(pthread_mutex_unlock(&cookie_mutex) != 0) fail_errno("Errore unlocking mutex");
/*** TO BE DONE 6.0 END ***/
return retval;
}
int keep_track_of_UID(int myUID)
{
int newcount;
if ( (myUID < 0) || (myUID >= MAX_COOKIES) )
return -1;
/*** Increment UserTracker[myUID] and return the computed value.
*** Be careful in order to avoid race conditions ***/
/*** TO BE DONE 6.0 START ***/
newcount=0;
if(pthread_mutex_lock(&cookie_mutex) != 0) fail_errno("Error: mutex unlocking failed");
++UserTracker[myUID];
newcount += UserTracker[myUID];
if(pthread_mutex_unlock(&cookie_mutex) != 0) fail_errno("Error: mutex unlocking failed");
/*** TO BE DONE 6.0 END ***/
return newcount;
}
void send_response(int client_fd, int response_code, int cookie,
#ifdef INCaPACHE_6_1
int is_http1_0, int thread_no,
#endif
char *filename, struct stat *stat_p)
{
time_t now_t = time(NULL);
struct tm now_tm;
time_t file_modification_time;
struct tm file_modification_tm;
char time_as_string[MAX_TIME_STR];
char http_header[MAX_HEADER_SIZE] = "HTTP/1.";
size_t header_size;
int fd = -1;
off_t file_size = 0;
char *mime_type = NULL;
const char * const HTML_mime = "text/html";
struct stat stat_buffer;
size_t header_sent;
debug(" ... start send_response(response_code=%d, filename=%s)\n", response_code, filename);
/*** Compute date of servicing current HTTP Request using a variant of gmtime() ***/
/*** TO BE DONE 6.0 START ***/
gmtime_r(&now_t, &now_tm);
/*** TO BE DONE 6.0 END ***/
strftime(time_as_string, MAX_TIME_STR, "%a, %d %b %Y %T GMT", &now_tm);
#ifdef INCaPACHE_6_1
strcat(http_header, is_http1_0 ? "0 " : "1 ");
#else /* #ifdef INCaPACHE_6_1 */
strcat(http_header, "0 ");
#endif /* #ifdef INCaPACHE_6_1 */
switch (response_code) {
case RESPONSE_CODE_OK:
if (filename != NULL) {
if (stat_p == NULL) {
stat_p = &stat_buffer;
if (stat(filename, stat_p)) {
debug("stat failed");
response_code = RESPONSE_CODE_INTERNAL_ERROR;
goto int_err;
}
} else {
fd = open(filename, O_RDONLY);
if (fd<0) {
debug("send_response: cannot open file for reading (has the file vanished?)");
response_code = RESPONSE_CODE_INTERNAL_ERROR;
goto int_err;
}
debug(" ... send_response(%d, %s): opened file %d\n", response_code, filename, fd);
}
mime_type = get_mime_type(filename);
debug(" ... send_response(%d, %s): got mime type %s\n", response_code, filename, mime_type);
/*** compute file_size and file_modification_time ***/
/*** TO BE DONE 6.0 START ***/
file_size=stat_p->st_size;
file_modification_time=stat_p->st_mtim.tv_sec;
/*** TO BE DONE 6.0 END ***/
debug(" ... send_response(%3d,%s) : file opened, size=%lu, mime=%s\n", response_code, filename, (unsigned long)file_size, mime_type);
strcat(http_header, "200 OK");
} else
strcat(http_header, "200 OK");
break;
case RESPONSE_CODE_MOVED_PERMANENTLY:
strcat(http_header, "301 Moved Permanently");
strcat(http_header, "Location: ");
strcat(http_header, filename);
break;
case RESPONSE_CODE_NOT_MODIFIED:
strcat(http_header, "304 Not Modified");
break;
case RESPONSE_CODE_BAD_REQUEST:
strcat(http_header, "400 Bad Request");
break;
case RESPONSE_CODE_NOT_FOUND:
strcat(http_header, "404 Not Found");
if ((fd = open(HTML_404, O_RDONLY)) >= 0) {
/*** compute file_size, mime_type, and file_modification_time of HTML_404 ***/
/*** TO BE DONE 6.0 START ***/
mime_type=my_strdup(HTML_mime);
stat_p=&stat_buffer;
if(stat(HTML_404, stat_p)==-1)
fail_errno("Errore in funzione stat");
file_size=stat_p->st_size;
file_modification_time=stat_p->st_mtim.tv_sec;
/*** TO BE DONE 6.0 END ***/
}
break;
case RESPONSE_CODE_INTERNAL_ERROR:
int_err:
strcat(http_header, "500 Internal Error");
break;
default:
/*** TO BE OPTIONALLY CHANGED START ***/
strcat(http_header, "501 Method Not Implemented\r\nAllow: HEAD,GET");
/*** TO BE OPTIONALLY CHANGED END ***/
if ((fd = open(HTML_501, O_RDONLY)) >= 0) {
/*** compute file_size, mime_type, and file_modification_time of HTML_501 ***/
/*** TO BE DONE 6.0 START ***/
memset(filename, 0, strlen(filename));
strcpy(filename, "404_Not_Found.html");
mime_type = get_mime_type(filename);
if (stat_p == NULL){
stat_p = &stat_buffer;
if (stat(filename, stat_p))
fail_errno("stat 404");
}
file_size = stat_p -> st_size;
file_modification_time = stat_p -> st_mtime;
/*** TO BE DONE 6.0 END ***/
}
break;
}
strcat(http_header, "\r\nDate: ");
strcat(http_header, time_as_string);
if ( cookie >= 0 ) {
/*** set permanent cookie in order to identify this client ***/
/*** TO BE DONE 6.0 START ***/
struct tm time;
time.tm_mday=12;
time.tm_mon=05;
time.tm_year=2023;
time.tm_hour=13;
time.tm_min=00;
time.tm_sec=0;
sprintf(http_header + strlen(http_header), "\r\nset cookie:%d, max age: %d", cookie, my_timegm(&time));
/*** TO BE DONE 6.0 END ***/
}
#ifdef INCaPACHE_6_1
strcat(http_header, "\r\nServer: incApache 6.1 for SETI.\r\n");
if (response_code >= 500 || is_http1_0)
strcat(http_header, "Connection: close\r\n");
#else
strcat(http_header, "\r\nServer: incApache 6.0 for SETI.\r\n");
strcat(http_header, "Connection: close\r\n");
#endif
if (file_size > 0 && mime_type != NULL) {
sprintf(http_header + strlen(http_header), "Content-Length: %lu \r\nContent-Type: %s\r\nLast-Modified: ", (unsigned long)file_size, mime_type);
/*** compute time_as_string, corresponding to file_modification_time, in GMT standard format;
see gmtime and strftime ***/
/*** TO BE DONE 6.0 START ***/
gmtime_r(&file_modification_time, &file_modification_tm);
strftime(time_as_string, MAX_TIME_STR, "%a, %d %b %Y %T GMT", &file_modification_tm);
/*** TO BE DONE 6.0 END ***/
strcat(http_header, time_as_string);
strcat(http_header, "\r\n");
}
strcat(http_header, "\r\n");
debug(" ... send_response(%d, %s) : header prepared\n", response_code, filename);
printf("Sending the following response:\n%s\n",http_header);
header_size = strlen(http_header);
#ifdef INCaPACHE_6_1
join_prev_thread(thread_no);
#endif
#ifdef OptimizeTCP
if ((header_sent=send_all(client_fd, http_header, header_size, (fd >= 0 ? MSG_MORE : 0))) < header_size)
#else
if ((header_sent=send_all(client_fd, http_header, header_size, 0)) < header_size )
#endif
{
if (header_sent==-1)
fail_errno("incApache: could not send HTTP header");
debug("header partially sent; header_size=%lu, header_sent=%lu\n", (unsigned long) header_size, (unsigned long) header_sent);
close(fd);
fd = -1;
}
debug(" ... send_response(%d, %s): header sent\n", response_code, filename);
if (fd >= 0) {
/*** send fd file on client_fd, then close fd; see syscall sendfile ***/
/*** TO BE DONE 6.0 START ***/
if(sendfile(client_fd, fd, NULL, file_size) == -1)
fail_errno("Errore sendfile");
if(close(fd) == -1)
fail_errno("Errore close fd");
/*** TO BE DONE 6.0 END ***/
}
#ifdef OptimizeTCP
if (fd >= 0) {
int optval = 1;
if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)))
fail_errno("Cannot set socket options");
}
#endif
debug(" ... end send_response(%d, %s)\n", response_code, filename);
free(mime_type);
}
void manage_http_requests(int client_fd
#ifdef INCaPACHE_6_1
, int connection_no
#endif
)
{
#define METHOD_NONE 0
#define METHOD_HEAD 1
#define METHOD_GET 2
#define METHOD_POST 4
#define METHOD_NOT_CHANGED 8
#define METHOD_CONDITIONAL 16
#define MethodIsConditional(m) ((m)&METHOD_CONDITIONAL)
FILE *client_stream = fdopen(client_fd, "r");
char *http_request_line = NULL;
char *strtokr_save;
size_t n = 0;
int http_method;
struct tm since_tm;
struct stat *stat_p;
int UIDcookie = -1;
#ifdef INCaPACHE_6_1
int is_http1_0 = 0;
int thread_idx;
#endif
if (!client_stream)
fail_errno("cannot open client stream");
while (getline(&http_request_line, &n, client_stream) >= 0) {
char *method_str, *filename, *protocol;
char *http_option_line = NULL;
char *option_name, *option_val;
printf("\n-----------------------------------------------\n");
printf("Received the following request:\n");
printf("%s", http_request_line);
#ifdef INCaPACHE_6_1
thread_idx = find_unused_thread_idx(connection_no);
#endif
/*** parse first line defining the 3 strings method_str,
*** filename, and protocol ***/
/*** TO BE DONE 6.0 START ***/
method_str = strtok_r(http_request_line, " ", &strtokr_save);
filename = strtok_r(NULL, " ", &strtokr_save);
protocol = strtok_r(NULL, "\r", &strtokr_save);
/*** TO BE DONE 6.0 END ***/
debug(" ... method_str=%s, filename=%s (0=%c), protocol=%s (len=%d)\n",
method_str, filename, filename ? filename[0] : '?', protocol, (int)(protocol ? strlen(protocol) : 0));
if (method_str == NULL || filename == NULL || protocol == NULL ||
filename[0] != '/' || strncmp(protocol, "HTTP/1.", 7) != 0 ||
strlen(protocol) != 8) {
debug(" ... Bad Request!\n");
SEND_RESPONSE(client_fd, RESPONSE_CODE_BAD_REQUEST, UIDcookie,
#ifdef INCaPACHE_6_1
1, connection_no, thread_idx,
#endif
NULL, NULL);
free(http_request_line);
break;
}
#ifdef INCaPACHE_6_1
is_http1_0 = !strcmp(protocol, "HTTP/1.0");
#endif
memset(&since_tm, 0, sizeof(since_tm));
http_method = METHOD_NONE;
if (strcmp(method_str, "GET") == 0)
http_method = METHOD_GET;
else if (strcmp(method_str, "HEAD") == 0)
http_method = METHOD_HEAD;
else if (strcmp(method_str, "POST") == 0)
http_method = METHOD_POST;
debug(" ... http_method=%d\n", http_method);
for (http_option_line = NULL, n = 0;
getline(&http_option_line, &n, client_stream) >= 0 && strcmp(http_option_line, "\r\n") != 0;
free(http_option_line), http_option_line = NULL, n = 0) {
debug("http_option_line: %s", http_option_line);
option_name = strtok_r(http_option_line, ": ", &strtokr_save);
if ( option_name != NULL ) {
if ( strcmp(option_name, "Cookie") == 0 ) {
/*** parse the cookie in order to get the UserID and count the number of requests coming from this client ***/
/*** TO BE DONE 6.0 START ***/
UIDcookie= atoi(strtok_r(NULL,"\r\n",&strtokr_save));
/*** TO BE DONE 6.0 END ***/
}
if ( http_method == METHOD_GET ) {
/*** parse option line, recognize "If-Modified-Since" option,
*** and possibly add METHOD_CONDITIONAL flag to http_method
*** and store date in since_tm
***/
/*** TO BE DONE 6.0 START ***/
option_name = strtok_r(http_option_line, ":", &strtokr_save);
option_val = strtok_r(NULL, "\n\0", &strtokr_save);
if((strcmp(option_name,"If-Modified-Since")) == 0)
if(strptime(option_val, " %a, %d %b %Y %T GMT", &since_tm) == NULL)
fail_errno("Errore in strptime");
else
http_method=METHOD_CONDITIONAL;
/*** TO BE DONE 6.0 END ***/
}
}
}
free(http_option_line);
if ( UIDcookie >= 0 ) { /*** increment visit count for this user ***/
int current_visit_count = keep_track_of_UID(UIDcookie);
if ( current_visit_count < 0 ) /*** wrong Cookie value ***/
UIDcookie = get_new_UID();
else {
printf("\n client provided UID Cookie %d for the %d time\n", UIDcookie, current_visit_count);
UIDcookie = -1;
}
} else /*** user did not provide any Cookie ***/
UIDcookie = get_new_UID();
/*** TO BE OPTIONALLY CHANGED START ***/
if (http_method == METHOD_NONE || http_method == METHOD_POST) {
/*** TO BE OPTIONALLY CHANGED END ***/
printf("method not implemented\n");
SEND_RESPONSE(client_fd, 501, UIDcookie,
#ifdef INCaPACHE_6_1
1, connection_no, thread_idx,
#endif
method_str, NULL);
break;
}
printf("\n-----------------------------------------------\n");
if (strcmp(filename, "/") == 0)
filename = "index.html";
else
filename += 1; /* remove leading '/' */
debug("http_method=%d, filename=%s\n", http_method, filename);
stat_p = my_malloc(sizeof(*stat_p));
if (access(filename, R_OK) != 0 || stat(filename, stat_p) < 0) {
debug(" ... file %s not found!\n", filename);
free(stat_p);
SEND_RESPONSE(client_fd, RESPONSE_CODE_NOT_FOUND, UIDcookie,
#ifdef INCaPACHE_6_1
is_http1_0, connection_no, thread_idx,
#endif
filename, NULL);
} else {
if (MethodIsConditional(http_method)) {
/*** compare file last modification time and decide
*** whether to transform http_method to METHOD_NOT_CHANGED
*** Use something like timegm() to convert from struct tm to time_t
***/
/*** TO BE DONE 6.0 START ***/
if(my_timegm(&since_tm) >= stat_p->st_mtim.tv_sec)
http_method=METHOD_NOT_CHANGED;
else
http_method=METHOD_CONDITIONAL;
/*** TO BE DONE 6.0 END ***/
}
switch (http_method) {
case METHOD_HEAD :
debug(" ... sending header for file %s\n", filename);
free(stat_p);
SEND_RESPONSE(client_fd, RESPONSE_CODE_OK, UIDcookie, /*** OK, without body ***/
#ifdef INCaPACHE_6_1
is_http1_0, connection_no, thread_idx,
#endif
filename, NULL);
break;
case METHOD_NOT_CHANGED :
debug(" ... file %s not modified\n", filename);
free(stat_p);
SEND_RESPONSE(client_fd, RESPONSE_CODE_NOT_MODIFIED, UIDcookie, /*** Not Modified, without body ***/
#ifdef INCaPACHE_6_1
is_http1_0, connection_no, thread_idx,
#endif
NULL, NULL);
break;
case METHOD_GET :
debug(" ... sending file %s\n", filename);
SEND_RESPONSE(client_fd, RESPONSE_CODE_OK, UIDcookie, /*** OK, with body ***/
#ifdef INCaPACHE_6_1
is_http1_0, connection_no, thread_idx,
#endif
filename, stat_p);
break;
case METHOD_POST :
/*** TO BE OPTIONALLY DONE START ***/
/*** TO BE OPTIONALLY DONE END ***/
default:
assert(0);
}
}
#ifdef INCaPACHE_6_1
if (is_http1_0)
#endif
break;
}
#ifdef INCaPACHE_6_1
join_all_threads(connection_no);
#endif
if (close(client_fd))
fail_errno("Cannot close the connection");
}