-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny.c
483 lines (422 loc) · 13.3 KB
/
tiny.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
/* $begin tinymain */
/*
* tiny.c - A simple, iterative HTTP/1.0 Web server that uses the
* GET method to serve static and dynamic content.
*/
#include "csapp.h"
#define MAX_CACHE_SIZE 20000
typedef struct cache_object* cache_obj;
/* Cache struct */
struct cache_object {
char* name;
void* handle;
cache_obj next;
int size;
};
struct cache_queue {
cache_obj front;
cache_obj back;
int size;
pthread_rwlock_t lock;
};
/*Global cache variable that is initialized with init_cache()*/
struct cache_queue* cache;
char scratch[MAXLINE];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* handle_request(void* arg);
void doit(int fd);
void read_requesthdrs(rio_t *rp);
int parse_uri(char *uri, char *function_name, char *cgiargs);
void serve_static(int fd, char *function_name, int filesize);
void get_filetype(char *function_name, char *filetype);
void serve_dynamic(int fd, char *function_name, char *cgiargs);
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg);
void cleanup(int fd);
void init_cache();
void* add_to_cache(char* name, void* handle, int size);
void* search_cache(char* name, int fd, char* cgiargs);
cache_obj create_node(char* name, void* handle, int size);
void evict_lru();
/*Lock wrapper functions*/
void init_lock(pthread_rwlock_t* lock);
void read_lock();
void write_lock();
void unlock();
int main(int argc, char **argv)
{
int listenfd, port, clientlen;
int* connfd_ptr;
struct sockaddr_in clientaddr;
pthread_t tid;
printf("%x\n", mutex);
init_cache();
/* Check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
port = atoi(argv[1]);
listenfd = Open_listenfd(port);
while (1) {
clientlen = sizeof(clientaddr);
connfd_ptr = (int*) Malloc(sizeof(int));
*connfd_ptr = Accept(listenfd, (SA *)&clientaddr, &clientlen);
printf("Connection made.\n");
Pthread_create(&tid, NULL, handle_request, (void*) connfd_ptr);
}
}
/* $end tinymain */
/*Small wrapper to parse/forward the request that fits Pthread specs*/
void* handle_request(void* connfd_ptr) {
int fd = *((int*) connfd_ptr);
Free(connfd_ptr);
doit(fd);
return NULL;
}
/*
* doit - handle one HTTP request/response transaction
*/
/* $begin doit */
void doit(int fd)
{
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char function_name[MAXLINE], cgiargs[MAXLINE];
rio_t rio;
/* This thread doesn't have to be joined */
Pthread_detach(Pthread_self());
/* Read request line and headers */
Rio_readinitb(&rio, fd);
Rio_readlineb(&rio, buf, MAXLINE);
sscanf(buf, "%s %s %s", method, uri, version);
printf("Scanned input. %s\n", buf);
if (strcasecmp(method, "GET")) {
clienterror(fd, method, "501", "Not Implemented",
"Tiny does not implement this method");
cleanup(fd);
return;
}
read_requesthdrs(&rio);
/* Parse URI from GET request */
is_static = parse_uri(uri, function_name, cgiargs);
if (stat(function_name, &sbuf) < 0 && is_static) {
clienterror(fd, function_name, "404", "Not found",
"Tiny couldn't find this file");
cleanup(fd);
return;
}
if (is_static) { /* Serve static content */
if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
clienterror(fd, function_name, "403", "Forbidden",
"Tiny couldn't read the file");
cleanup(fd);
return;
}
serve_static(fd, function_name, sbuf.st_size);
}
else { /* Serve dynamic content */
#ifdef OLD
if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
clienterror(fd, function_name, "403", "Forbidden",
"Tiny couldn't run the CGI program");
cleanup(fd);
return;
}
#endif
serve_dynamic(fd, function_name, cgiargs);
}
cleanup(fd);
}
/* $end doit */
/*
* read_requesthdrs - read and parse HTTP request headers
*/
/* $begin read_requesthdrs */
void read_requesthdrs(rio_t *rp)
{
char buf[MAXLINE];
Rio_readlineb(rp, buf, MAXLINE);
printf("%s", buf);
while(strcmp(buf, "\r\n")) {
Rio_readlineb(rp, buf, MAXLINE);
printf("%s", buf);
}
return;
}
/* $end read_requesthdrs */
/*
* parse_uri - parse URI into function_name and CGI args
* return 0 if dynamic content, 1 if static
*/
/* $begin parse_uri */
int parse_uri(char *uri, char *function_name, char *cgiargs)
{
char *ptr;
if (!strstr(uri, "cgi-bin")) { /* Static content */
strcpy(cgiargs, "");
strcpy(function_name, ".");
strcat(function_name, uri);
if (uri[strlen(uri)-1] == '/')
strcat(function_name, "home.html");
return 1;
}
else { /* Dynamic content */
ptr = index(uri, '?');
if (ptr) {
strcpy(cgiargs, ptr+1);
*ptr = '\0';
}
else
strcpy(cgiargs, "");
//strcpy(function_name, ".");
// Skip the "/cgi-bin/" portion and jump to the program
strcat(function_name, uri+9);
return 0;
}
}
/* $end parse_uri */
/*
* serve_static - copy a file back to the client
*/
/* $begin serve_static */
void serve_static(int fd, char *function_name, int filesize)
{
int srcfd;
char *srcp, filetype[MAXLINE], buf[MAXBUF];
/* Send response headers to client */
get_filetype(function_name, filetype);
sprintf(buf, "HTTP/1.0 200 OK\r\n");
sprintf(buf, "%sServer: Tiny Web Server\r\n", buf);
sprintf(buf, "%sContent-length: %d\r\n", buf, filesize);
sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
Rio_writen(fd, buf, strlen(buf));
/* Send response body to client */
srcfd = Open(function_name, O_RDONLY, 0);
srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
Close(srcfd);
Rio_writen(fd, srcp, filesize);
Munmap(srcp, filesize);
}
/*
* get_filetype - derive file type from file name
*/
void get_filetype(char *function_name, char *filetype)
{
if (strstr(function_name, ".html"))
strcpy(filetype, "text/html");
else if (strstr(function_name, ".gif"))
strcpy(filetype, "image/gif");
else if (strstr(function_name, ".jpg"))
strcpy(filetype, "image/jpeg");
else
strcpy(filetype, "text/plain");
}
/* $end serve_static */
size_t getfilesize(char* filename) {
struct stat st;
stat(filename, &st);
return st.st_size;
}
/*
* serve_dynamic - run a CGI program on behalf of the client
*/
/* $begin serve_dynamic */
void serve_dynamic(int fd, char *function_name, char *cgiargs)
{
char buf[MAXLINE], *emptylist[] = { NULL };
void *handle;
void (*function)(int, char*);
char *error;
size_t size;
sprintf(buf, "Hello\n");
write(fd, buf, strlen(buf));
/* Return first part of HTTP response */
sprintf(buf, "HTTP/1.0 200 OK\r\n");
write(fd, buf, strlen(buf));
sprintf(buf, "Server: Tiny Web Server\r\n");
write(fd, buf, strlen(buf));
//#ifdef OLD
/* search_cache finds and evaluates function if cached */
if ((function = search_cache(function_name, fd, cgiargs)) == NULL) {
/* else... add to cache and execute here */
pthread_mutex_lock(&mutex);
printf("Didn't find in cache, opening file\n");
/* DL_Open the corresponding .so file */
sprintf(buf, "./lib/%s.so", function_name);
size = getfilesize(buf);
if ((handle = dlopen(buf, RTLD_LAZY)) == NULL) {
sprintf(buf, "%s\n", dlerror());
write(fd, buf, strlen(buf));
return;
}
printf("Opened file and got handle to function\n");
/* Get the function (from dlysm) and add to cache */
function = (void (*)(int, char*)) add_to_cache(function_name, handle, size);
if ((error = dlerror()) != NULL) {
printf("Invalid function error: %s %s\n", function_name, error);
sprintf(buf, "Invalid function %s\n", function_name);
Rio_writen(fd, buf, strlen(buf));
return;
}
/* Execute the function */
if (function != NULL) {
function(fd, cgiargs);
}
/* At this point the function is complete and in the cache */
pthread_mutex_unlock(&mutex);
printf("released mutex, served client\n");
}
//#endif
}
/* $end serve_dynamic */
/* cleanup -- Frees up descriptors in use and ends thread */
void cleanup(int fd) {
Close(fd);
Pthread_exit(NULL);
}
/*
* clienterror - returns an error message to the client
*/
/* $begin clienterror */
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg)
{
char buf[MAXLINE], body[MAXBUF];
/* Build the HTTP response body */
sprintf(body, "<html><title>Tiny Error</title>");
sprintf(body, "%s<body bgcolor=""ffffff"">\r\n", body);
sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause);
sprintf(body, "%s<hr><em>The Tiny Web server</em>\r\n", body);
/* Print the HTTP response */
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n");
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
Rio_writen(fd, buf, strlen(buf));
Rio_writen(fd, body, strlen(body));
}
/* $end clienterror */
/******* CACHE FUNCTIONS ******/
void init_cache() {
/*Create dummy node, initialize all fields to NULL or 0 */
cache_obj dummy_node = Calloc(1, sizeof(struct cache_object));
dummy_node->name = NULL;
dummy_node->handle = NULL;
dummy_node->next = NULL;
dummy_node->size = 0;
cache = Malloc(sizeof(struct cache_queue));
cache->front = dummy_node;
cache->back = dummy_node;
cache->size = 0;
init_lock(&cache->lock);
}
/* Always called under protection of mutex */
void evict_lru() {
cache_obj first = cache->front->next;
cache->front->next = first->next;
if (first == cache->back)
cache->back = cache->front;
cache->size -= first->size;
printf("Evicting %s from the cache.\n", first->name);
/* unload the shared library */
if (dlclose(first->handle) < 0) {
fprintf(stderr, "%s\n", dlerror());
}
free(first->name);
free(first);
}
cache_obj create_node(char* name, void* handle, int size) {
cache_obj new_node = Malloc(sizeof(struct cache_object));
new_node->name = Calloc(strlen(name)+1, sizeof(char));
strncpy(new_node->name, name, strlen(name));
new_node->handle = handle;
new_node->size = size;
new_node->next = NULL;
return new_node;
}
/* Always called under protection of mutex */
void* add_to_cache(char* name, void* handle, int size) {
void* function;
write_lock();
printf("Adding %s to cache.\n", name);
/*Evicts if necessary until there is enough space to cache */
while (cache->size > MAX_CACHE_SIZE)
evict_lru();
/* Create new node and add to back of cache */
cache_obj new_node = create_node(name, handle, size);
cache->back->next = new_node;
cache->back = new_node;
cache->size += size;
unlock();
/* Open the shared library containing function name */
sprintf(scratch, "./lib/%s.so", name);
/* Resolve the function */
function = dlsym(handle, name);
printf("Done adding %s to cache\n", name);
return function;
}
void* search_cache(char* name, int fd, char* cgiargs) {
void (*function)(int, char*);
read_lock();
cache_obj cur = cache->front->next;
cache_obj prev = cache->front;
for (; cur; cur = cur->next) {
/*If next matches key, object is found */
if (!strcmp(cur->name, name)) {
pthread_mutex_lock(&mutex);
unlock(); /* Unlocks the read lock*/
if (cur != cache->back) {
/* Takes the write lock to move to the end of the cache */
write_lock();
prev->next = cur->next;
cache->back->next = cur;
cache->back = cur;
cur->next = NULL;
unlock(); /* Unlocks the write lock */
}
/* Found in the cache, get and execute function */
function = dlsym(cur->handle, name);
if (dlerror() != NULL) {
sprintf(scratch, "Invalid function %s\n", name);
Rio_writen(fd, scratch, strlen(scratch));
return NULL;
}
function(fd, cgiargs);
pthread_mutex_unlock(&mutex);
return cur->handle;
}
prev = cur;
}
/*If reached here, key not found in cache. */
unlock();
return NULL;
}
/******* LOCK WRAPPER FUNCTIONS ******/
void init_lock(pthread_rwlock_t* lock) {
if (pthread_rwlock_init(lock, NULL) != 0) {
fprintf(stderr, "Error: Init lock failed.\n");
exit(-1);
}
}
void write_lock() {
if (pthread_rwlock_wrlock((pthread_rwlock_t*) (&cache->lock)) != 0) {
fprintf(stderr, "Error: Write lock failed.\n");
exit(-1);
}
}
void read_lock() {
if (pthread_rwlock_rdlock((pthread_rwlock_t*) (&cache->lock)) != 0) {
fprintf(stderr, "Error: Read lock failed.\n");
exit(-1);
}
}
void unlock() {
if (pthread_rwlock_unlock((pthread_rwlock_t*) (&cache->lock)) != 0) {
fprintf(stderr, "Error: Unlock failed.\n");
exit(-1);
}
}