From e05f335307d37c0eeac2fffde10b60b471f2de36 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Mon, 11 Mar 2019 14:08:52 -0600 Subject: [PATCH 01/13] Ian Cameron C Web Server --- src/server.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index ea43306fc..1ab68598a 100644 --- a/src/server.c +++ b/src/server.c @@ -52,12 +52,19 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont { const int max_response_size = 262144; char response[max_response_size]; - // Build HTTP response and store it in response /////////////////// // IMPLEMENT ME! // /////////////////// + sprintf(response, + "%s\n" + "Content-type: %s\n" + "Content-length: %d\n" + "Connection: close\n" + "\n" + "%s\n", header, content_type, content_length, body + ); // Send it all! int rv = send(fd, response, response_length, 0); From 7e2252f922890eadb2a3e670a49b2fbdc602cd05 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Mon, 11 Mar 2019 16:31:55 -0600 Subject: [PATCH 02/13] adds date time --- src/server.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index 1ab68598a..7ad7f4523 100644 --- a/src/server.c +++ b/src/server.c @@ -53,17 +53,26 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont const int max_response_size = 262144; char response[max_response_size]; // Build HTTP response and store it in response + int response_length = strlen(body); /////////////////// // IMPLEMENT ME! // /////////////////// + //time + time_t rawtime; + struct tm *info; + time (&rawtime); + info = localtime(&rawtime); + + //build header sprintf(response, "%s\n" + "Date: %s\n" "Content-type: %s\n" "Content-length: %d\n" "Connection: close\n" "\n" - "%s\n", header, content_type, content_length, body + "%s\n", header, asctime(info), content_type, content_length, body ); // Send it all! @@ -166,6 +175,10 @@ void handle_http_request(int fd, struct cache *cache) /////////////////// // Read the three components of the first request line + char method[512]; + char path[8192]; + //char request_body + sscanf(request, "%s %s %s", method, path); // If GET, handle the get endpoints From 3870a5fb7328bb46c92e4a1072f598b485a934e5 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Mon, 11 Mar 2019 16:59:38 -0600 Subject: [PATCH 03/13] adds get request --- src/server.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index 7ad7f4523..90f8bdc47 100644 --- a/src/server.c +++ b/src/server.c @@ -177,13 +177,25 @@ void handle_http_request(int fd, struct cache *cache) // Read the three components of the first request line char method[512]; char path[8192]; - //char request_body + sscanf(request, "%s %s %s", method, path); + printf("method: \"%s\"\n", method); + printf("path: \"%s\"\n", path); // If GET, handle the get endpoints // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() + if (strcmp(method, "GET") == 0) + if (strcmp(path, xxxxxx) == 0) + { + //get something + } + else + { + resp_404(fd); + } + // (Stretch) If POST, handle the post request From 65dc926a61fb12ee7088f6eb08dd9c0f30249a65 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Tue, 12 Mar 2019 09:33:17 -0600 Subject: [PATCH 04/13] adds handle http request --- src/server.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/server.c b/src/server.c index 90f8bdc47..ebae318e8 100644 --- a/src/server.c +++ b/src/server.c @@ -138,6 +138,9 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// + (void)fd; + (void)cache; + (void)request_path; } /** @@ -151,6 +154,7 @@ char *find_start_of_body(char *header) /////////////////// // IMPLEMENT ME! // (Stretch) /////////////////// + (void)header; } /** @@ -158,6 +162,7 @@ char *find_start_of_body(char *header) */ void handle_http_request(int fd, struct cache *cache) { + (void)cache; //rememember to remove all these voids. reeeeeeeeemember! const int request_buffer_size = 65536; // 64K char request[request_buffer_size]; @@ -169,16 +174,14 @@ void handle_http_request(int fd, struct cache *cache) return; } - /////////////////// // IMPLEMENT ME! // /////////////////// - // Read the three components of the first request line char method[512]; char path[8192]; - sscanf(request, "%s %s %s", method, path); + sscanf(request, "%s %s", method, path); printf("method: \"%s\"\n", method); printf("path: \"%s\"\n", path); @@ -187,18 +190,17 @@ void handle_http_request(int fd, struct cache *cache) // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() if (strcmp(method, "GET") == 0) - if (strcmp(path, xxxxxx) == 0) + if (strcmp(path, "/d20") == 0) //strcmp returns 0 if equal { - //get something + get_d20(fd); } else { resp_404(fd); } - - // (Stretch) If POST, handle the post request + if (strcmp(method, "POST") == 0) } /** From 5f5ef4404bb016254221e2a1d4e3e7f7df98bb47 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Tue, 12 Mar 2019 10:04:28 -0600 Subject: [PATCH 05/13] adds get d20 --- src/server.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/server.c b/src/server.c index ebae318e8..f4d297281 100644 --- a/src/server.c +++ b/src/server.c @@ -92,16 +92,18 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont void get_d20(int fd) { // Generate a random number between 1 and 20 inclusive - /////////////////// // IMPLEMENT ME! // /////////////////// + char str; + int random_number = rand() % 21; //num = (rand() % (upper – lower + 1)) + lower + sprintf(str, "%d\n", random_number); // Use send_response() to send it back as text/plain data - /////////////////// // IMPLEMENT ME! // /////////////////// + send_response(fd, "HTTP/1.1 200 OK", "text/plain", str, strlen(str)); } /** @@ -200,7 +202,7 @@ void handle_http_request(int fd, struct cache *cache) } // (Stretch) If POST, handle the post request - if (strcmp(method, "POST") == 0) + //if (strcmp(method, "POST") == 0) } /** From 3824b2ef95507ab88651536f038b7a3725507905 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Tue, 12 Mar 2019 16:54:14 -0600 Subject: [PATCH 06/13] adds get file --- src/server.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/server.c b/src/server.c index f4d297281..de59326c0 100644 --- a/src/server.c +++ b/src/server.c @@ -95,7 +95,7 @@ void get_d20(int fd) /////////////////// // IMPLEMENT ME! // /////////////////// - char str; + char str[4]; int random_number = rand() % 21; //num = (rand() % (upper – lower + 1)) + lower sprintf(str, "%d\n", random_number); @@ -142,7 +142,30 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// (void)fd; (void)cache; - (void)request_path; + //read file + char filepath[4096]; + struct file_data *file_data; //loads file into memory and returns pointer to data + char *mime_type; + //fetch file + snprintf(filepath, sizeof filepath, "%s/%s", SERVER_ROOT, request_path); + if (entry == NULL) + { + filedata = file_load(filepath); + + if (filedata == NULL) + { + // TODO: make this non-fatal + fprintf(stderr, "cannot find file\n"); + exit(3); + } + } + //get mimetype + mime_type = mime_type_get(filepath); + //send file out + send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size); + //then free data bc loaded these data into memory + file_free(filedata); + //server files v root } /** From ef441e4bb1f80d4138a373f8dd438aa02930411d Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 13 Mar 2019 14:55:18 -0600 Subject: [PATCH 07/13] refactors send to accommodate png --- src/server.c | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/server.c b/src/server.c index de59326c0..0f21e7941 100644 --- a/src/server.c +++ b/src/server.c @@ -71,13 +71,20 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont "Content-type: %s\n" "Content-length: %d\n" "Connection: close\n" - "\n" - "%s\n", header, asctime(info), content_type, content_length, body + "\n", + //"%s\n", //taking body off the response header and creating separate send + header, asctime(info), content_type, content_length //,body ); // Send it all! int rv = send(fd, response, response_length, 0); + if (rv < 0) { + perror("send"); + } + //refactor this (creating another send) to accommodate png so send body (and its length) separately from the response so still only one request + rv = send(fd, body, content_length, 0); + if (rv < 0) { perror("send"); } @@ -144,20 +151,19 @@ void get_file(int fd, struct cache *cache, char *request_path) (void)cache; //read file char filepath[4096]; - struct file_data *file_data; //loads file into memory and returns pointer to data + struct file_data *filedata; //loads file into memory and returns pointer to data char *mime_type; - //fetch file - snprintf(filepath, sizeof filepath, "%s/%s", SERVER_ROOT, request_path); - if (entry == NULL) - { + //fetch file. + //append path user requests (which != filepath) to server root (3490/index.html) + snprintf(filepath, sizeof filepath, "%s %s", SERVER_ROOT, request_path); + printf("\"%s\"n", filepath); + //load that file filedata = file_load(filepath); - - if (filedata == NULL) - { - // TODO: make this non-fatal - fprintf(stderr, "cannot find file\n"); - exit(3); - } + //if file not found + if (filedata == NULL) + { + resp_404(fd); + return; } //get mimetype mime_type = mime_type_get(filepath); @@ -165,7 +171,6 @@ void get_file(int fd, struct cache *cache, char *request_path) send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size); //then free data bc loaded these data into memory file_free(filedata); - //server files v root } /** @@ -174,13 +179,13 @@ void get_file(int fd, struct cache *cache, char *request_path) * "Newlines" in HTTP can be \r\n (carriage return followed by newline) or \n * (newline) or \r (carriage return). */ -char *find_start_of_body(char *header) -{ - /////////////////// - // IMPLEMENT ME! // (Stretch) - /////////////////// - (void)header; -} +// char *find_start_of_body(char *header) +// { +// /////////////////// +// // IMPLEMENT ME! // (Stretch) +// /////////////////// +// (void)header; +// } /** * Handle HTTP request and send response @@ -221,7 +226,8 @@ void handle_http_request(int fd, struct cache *cache) } else { - resp_404(fd); + get_file(fd, NULL, path); + //resp_404(fd); //used 404 before completing get_file } // (Stretch) If POST, handle the post request From 5b00fbe0d089efd8f58c90d4d55a03e252e9ee2e Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 13 Mar 2019 16:32:45 -0600 Subject: [PATCH 08/13] adds cache entry --- src/cache.c | 17 +++++++++++++++++ src/server.c | 25 +++++++++++-------------- src/serverroot/{cat.jpg => cat.png} | Bin 3 files changed, 28 insertions(+), 14 deletions(-) rename src/serverroot/{cat.jpg => cat.png} (100%) diff --git a/src/cache.c b/src/cache.c index c72975cdd..595200615 100644 --- a/src/cache.c +++ b/src/cache.c @@ -12,6 +12,22 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i /////////////////// // IMPLEMENT ME! // /////////////////// + //allocate space for the struct were going to return + struct cache_entry *ce = malloc(sizeof *ce); //size of dereference ce if dereffed it is cache_entry + //set all fields to values being passed in to intialize + //have a pointer to something but have to make sure what you are pointing to persists(make sure it doesnt accidentally get freed) + ce->path = malloc(strlen(path) + 1); //so make a copy of path we know will persist. +1 for null terminator + strcpy(ce->path, path); //(dest, args) + + ce->content_type = malloc(strlen(content_type) +1); + strcpy(ce->content_type, content_type); + + ce->content = malloc(content_length); //content is void pointer so dont know what type so cant use strlen, but know its length so can use that + memcpy(ce->content, content, content_length); + + ce->content_length = content_length; //int so dont have to malloc + + return ce; } /** @@ -125,6 +141,7 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten /////////////////// // IMPLEMENT ME! // /////////////////// + } /** diff --git a/src/server.c b/src/server.c index 0f21e7941..4759beef0 100644 --- a/src/server.c +++ b/src/server.c @@ -53,7 +53,6 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont const int max_response_size = 262144; char response[max_response_size]; // Build HTTP response and store it in response - int response_length = strlen(body); /////////////////// // IMPLEMENT ME! // @@ -65,11 +64,11 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont info = localtime(&rawtime); //build header - sprintf(response, + int response_length = snprintf(response, max_response_size, "%s\n" - "Date: %s\n" - "Content-type: %s\n" - "Content-length: %d\n" + "Date: %s" //asctime adds its own new line + "Content-Type: %s\n" + "Content-Length: %d\n" "Connection: close\n" "\n", //"%s\n", //taking body off the response header and creating separate send @@ -147,7 +146,6 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// - (void)fd; (void)cache; //read file char filepath[4096]; @@ -155,8 +153,8 @@ void get_file(int fd, struct cache *cache, char *request_path) char *mime_type; //fetch file. //append path user requests (which != filepath) to server root (3490/index.html) - snprintf(filepath, sizeof filepath, "%s %s", SERVER_ROOT, request_path); - printf("\"%s\"n", filepath); + snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); + printf("\"%s\"\n", filepath); //load that file filedata = file_load(filepath); //if file not found @@ -167,6 +165,7 @@ void get_file(int fd, struct cache *cache, char *request_path) } //get mimetype mime_type = mime_type_get(filepath); + printf("%s\n", mime_type); //send file out send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size); //then free data bc loaded these data into memory @@ -219,16 +218,14 @@ void handle_http_request(int fd, struct cache *cache) // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() - if (strcmp(method, "GET") == 0) - if (strcmp(path, "/d20") == 0) //strcmp returns 0 if equal - { + if (strcmp(method, "GET") == 0) { + if (strcmp(path, "/d20") == 0) { //strcmp returns 0 if equal get_d20(fd); - } - else - { + } else { get_file(fd, NULL, path); //resp_404(fd); //used 404 before completing get_file } + } // (Stretch) If POST, handle the post request //if (strcmp(method, "POST") == 0) diff --git a/src/serverroot/cat.jpg b/src/serverroot/cat.png similarity index 100% rename from src/serverroot/cat.jpg rename to src/serverroot/cat.png From 376eee76e81af398a1d176bcb97e8d19fd2b131a Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 13 Mar 2019 16:46:38 -0600 Subject: [PATCH 09/13] adds free entry --- src/cache.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cache.c b/src/cache.c index 595200615..0770037f5 100644 --- a/src/cache.c +++ b/src/cache.c @@ -38,6 +38,12 @@ void free_entry(struct cache_entry *entry) /////////////////// // IMPLEMENT ME! // /////////////////// + //have to free the cache entry passed in but have to free everything we malloc'ed space for + //free everything in sttucture, then free the structure itself so entry last + free(entry->path); + free(entry->content_type); + free(entry->content); + free(entry); } /** From 81ef38e7f45fdf010dbd0709e5cf7ad93ab19af3 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 14 Mar 2019 13:24:28 -0600 Subject: [PATCH 10/13] adds cache create --- src/cache.c | 24 +++++++++++++++++++----- src/cache.h | 2 ++ src/server.c | 7 +++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/cache.c b/src/cache.c index 0770037f5..8f2ac18a5 100644 --- a/src/cache.c +++ b/src/cache.c @@ -14,19 +14,21 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i /////////////////// //allocate space for the struct were going to return struct cache_entry *ce = malloc(sizeof *ce); //size of dereference ce if dereffed it is cache_entry - //set all fields to values being passed in to intialize + //set all fields to values being passed in to initialize //have a pointer to something but have to make sure what you are pointing to persists(make sure it doesnt accidentally get freed) ce->path = malloc(strlen(path) + 1); //so make a copy of path we know will persist. +1 for null terminator - strcpy(ce->path, path); //(dest, args) + strcpy(ce->path, path); //(dest, args) copy path into allocated memory. this is deep copy. ce->content_type = malloc(strlen(content_type) +1); strcpy(ce->content_type, content_type); ce->content = malloc(content_length); //content is void pointer so dont know what type so cant use strlen, but know its length so can use that - memcpy(ce->content, content, content_length); + memcpy(ce->content, content, content_length); //(dest, src, size) ce->content_length = content_length; //int so dont have to malloc + ce->prev = ce->next = NULL; + return ce; } @@ -39,7 +41,7 @@ void free_entry(struct cache_entry *entry) // IMPLEMENT ME! // /////////////////// //have to free the cache entry passed in but have to free everything we malloc'ed space for - //free everything in sttucture, then free the structure itself so entry last + //free everything in stucture, then free the structure itself so entry last free(entry->path); free(entry->content_type); free(entry->content); @@ -115,7 +117,19 @@ struct cache *cache_create(int max_size, int hashsize) { /////////////////// // IMPLEMENT ME! // - /////////////////// + /////////////////// going in opposite order from cache free below. also refer to cache struct in cache.h + //allocate cache + struct cache *cache = malloc(sizeof *cache); + //functions in hashtable.h and c + cache->index = hashtable_create(hashsize, NULL); //size and pointer to a function (that takes in certain values and returns int) + //initialize head and tail null + cache->head = cache->tail = NULL; + + cache->max_size = max_size; + + cache->cur_size = 0; + + return cache; } void cache_free(struct cache *cache) diff --git a/src/cache.h b/src/cache.h index f64c976ad..8bb0fb204 100644 --- a/src/cache.h +++ b/src/cache.h @@ -8,6 +8,8 @@ struct cache_entry { int content_length; void *content; + time_t timestamp; + struct cache_entry *prev, *next; // Doubly-linked list }; diff --git a/src/server.c b/src/server.c index 4759beef0..350fe04f1 100644 --- a/src/server.c +++ b/src/server.c @@ -146,6 +146,8 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// + + (void)cache; //read file char filepath[4096]; @@ -155,6 +157,11 @@ void get_file(int fd, struct cache *cache, char *request_path) //append path user requests (which != filepath) to server root (3490/index.html) snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); printf("\"%s\"\n", filepath); + + //time comparison for expiration of cache (implemented with cache put/get) + //expire then refresh file and delete item out of cache + struct cache_entry *ce = cache_get(filepath); + //load that file filedata = file_load(filepath); //if file not found From d32644017bbc06ad6014717e1016b9e3c7cf15f1 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 14 Mar 2019 15:53:14 -0600 Subject: [PATCH 11/13] adds cache put --- src/cache.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cache.c b/src/cache.c index 8f2ac18a5..87d099b10 100644 --- a/src/cache.c +++ b/src/cache.c @@ -125,7 +125,7 @@ struct cache *cache_create(int max_size, int hashsize) //initialize head and tail null cache->head = cache->tail = NULL; - cache->max_size = max_size; + cache->max_size = max_size; //int so dont allocate space cache->cur_size = 0; @@ -161,7 +161,25 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten /////////////////// // IMPLEMENT ME! // /////////////////// - + //Allocate a new cache entry with the passed parameters + struct cache_entry *ce = alloc_entry(path, content_type, content, content_length); + //Insert the entry at the head of the doubly-linked list + dllist_move_to_head(cache, ce); + //Store the entry in the hashtable as well, indexed by the entry's path + hashtable_put(cache->index, ce->path, ce); + //Increment the current size of the cache + cache->cur_size++; + //If the cache size is greater than the max size + if (cache->cur_size > cache->max_size) { + //Remove the cache entry at the tail of the linked list + dllist_remove_tail(ce); + //Remove that same entry from the hashtable, using the entry's path and the hashtable_delete function + hashtable_delete(ce->path, ce); + //Free the cache entry + cache_free(ce); + //Ensure the size counter for the number of entries in the cache is correct + cache->cur_size--; + } } /** From 23b56f4aeba145416b5a225c9073001457180ce2 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 14 Mar 2019 16:26:14 -0600 Subject: [PATCH 12/13] starts cache get --- src/cache.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cache.c b/src/cache.c index 87d099b10..c7334b740 100644 --- a/src/cache.c +++ b/src/cache.c @@ -190,4 +190,13 @@ struct cache_entry *cache_get(struct cache *cache, char *path) /////////////////// // IMPLEMENT ME! // /////////////////// + //Attempt to find the cache entry pointer by path in the hash table. If not found, return NULL + if (hashtable_get(cache->index, path) == NULL) { + return NULL; + } else { + //Move the cache entry to the head of the doubly-linked list + dllist_move_to_head(cache, ce); + } + //Return the cache entry pointer + return ce; } From e9d91024d24b783f85196a80ab82d4a4cee843e1 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 14 Mar 2019 17:29:06 -0600 Subject: [PATCH 13/13] debugs put --- src/cache.c | 41 ++++++++++++++++++++++------------- src/cache.h | 2 +- src/cache_tests/cache_tests.c | 8 +++---- src/server.c | 10 ++++----- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/cache.c b/src/cache.c index c7334b740..0713d3c7e 100644 --- a/src/cache.c +++ b/src/cache.c @@ -149,6 +149,15 @@ void cache_free(struct cache *cache) free(cache); } +void clean_lru(struct cache *cache) +{ + while (cache->cur_size > cache->max_size) { + struct cache_entry *oldtail = dllist_remove_tail(cache); + + hashtable_delete(cache->index, oldtail->path); + free_entry(oldtail); + } +} /** * Store an entry in the cache * @@ -164,22 +173,23 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten //Allocate a new cache entry with the passed parameters struct cache_entry *ce = alloc_entry(path, content_type, content, content_length); //Insert the entry at the head of the doubly-linked list - dllist_move_to_head(cache, ce); + dllist_insert_head(cache, ce); //Store the entry in the hashtable as well, indexed by the entry's path hashtable_put(cache->index, ce->path, ce); //Increment the current size of the cache cache->cur_size++; + clean_lru(cache); //If the cache size is greater than the max size - if (cache->cur_size > cache->max_size) { - //Remove the cache entry at the tail of the linked list - dllist_remove_tail(ce); - //Remove that same entry from the hashtable, using the entry's path and the hashtable_delete function - hashtable_delete(ce->path, ce); - //Free the cache entry - cache_free(ce); - //Ensure the size counter for the number of entries in the cache is correct - cache->cur_size--; - } + // if (cache->cur_size > cache->max_size) { + // //Remove the cache entry at the tail of the linked list + // dllist_remove_tail(ce); + // //Remove that same entry from the hashtable, using the entry's path and the hashtable_delete function + // hashtable_delete(ce->path, ce); + // //Free the cache entry + // cache_free(ce); + // //Ensure the size counter for the number of entries in the cache is correct + // cache->cur_size--; + // } } /** @@ -191,12 +201,13 @@ struct cache_entry *cache_get(struct cache *cache, char *path) // IMPLEMENT ME! // /////////////////// //Attempt to find the cache entry pointer by path in the hash table. If not found, return NULL - if (hashtable_get(cache->index, path) == NULL) { + struct cache_entry *ce = hashtable_get(cache->index, path); + + if (ce == NULL) { return NULL; - } else { - //Move the cache entry to the head of the doubly-linked list - dllist_move_to_head(cache, ce); } + //Move the cache entry to the head of the doubly-linked list + dllist_move_to_head(cache, ce); //Return the cache entry pointer return ce; } diff --git a/src/cache.h b/src/cache.h index 8bb0fb204..519f68203 100644 --- a/src/cache.h +++ b/src/cache.h @@ -8,7 +8,7 @@ struct cache_entry { int content_length; void *content; - time_t timestamp; + //time_t timestamp; //added for comparison implementing cache get/put struct cache_entry *prev, *next; // Doubly-linked list }; diff --git a/src/cache_tests/cache_tests.c b/src/cache_tests/cache_tests.c index 5e245d005..a4cb58667 100644 --- a/src/cache_tests/cache_tests.c +++ b/src/cache_tests/cache_tests.c @@ -63,7 +63,7 @@ char *test_cache_put() mu_assert(check_cache_entries(cache->head, test_entry_1) == 0, "Your cache_put function did not put an entry into the head of the empty cache with the expected form"); mu_assert(check_cache_entries(cache->tail, test_entry_1) == 0, "Your cache_put function did not put an entry into the tail of the empty cache with the expected form"); mu_assert(check_cache_entries(hashtable_get(cache->index, "/1"), test_entry_1) == 0, "Your cache_put function did not put the expected entry into the hashtable"); - + // Add in a second entry to the cache cache_put(cache, test_entry_2->path, test_entry_2->content_type, test_entry_2->content, test_entry_2->content_length); // Check that the cache is handling both entries as expected @@ -72,7 +72,7 @@ char *test_cache_put() mu_assert(check_cache_entries(cache->tail, test_entry_1) == 0, "Your cache_put function did not move the oldest entry in the cache to the tail of the cache"); mu_assert(check_cache_entries(cache->head->next, test_entry_1) == 0, "Your cache_put function did not correctly set the head->next pointer of the cache"); mu_assert(check_cache_entries(hashtable_get(cache->index, "/2"), test_entry_2) == 0, "Your cache_put function did not put the expected entry into the hashtable"); - + // Add in a third entry to the cache cache_put(cache, test_entry_3->path, test_entry_3->content_type, test_entry_3->content, test_entry_3->content_length); // Check that the cache is handling all three entries as expected @@ -83,7 +83,7 @@ char *test_cache_put() mu_assert(check_cache_entries(cache->head->next->next, test_entry_1) == 0, "Your cache_put function did not update the head->next->next pointer to point to the tail entry"); mu_assert(check_cache_entries(cache->tail->prev, test_entry_2) == 0, "Your cache_put function did not update the tail->prev pointer to poin to the second-to-last entry"); mu_assert(check_cache_entries(cache->tail, test_entry_1) == 0, "Your cache_put function did not correctly update the tail pointer of the cache"); - + // Add in a fourth entry to the cache cache_put(cache, test_entry_4->path, test_entry_4->content_type, test_entry_4->content, test_entry_4->content_length); // Check that the cache removed the oldest entry and is handling the three most-recent entries correctly @@ -94,7 +94,7 @@ char *test_cache_put() mu_assert(check_cache_entries(cache->head->next->next, test_entry_2) == 0, "Your cache_put function did not update the head->next->next pointer to point to the tail entry"); mu_assert(check_cache_entries(cache->tail->prev, test_entry_3) == 0, "Your cache_put function did not update the tail->prev pointer to poin to the second-to-last entry"); mu_assert(check_cache_entries(cache->tail, test_entry_2) == 0, "Your cache_put function did not correctly handle the tail of an already-full cache"); - + cache_free(cache); return NULL; diff --git a/src/server.c b/src/server.c index 350fe04f1..cc3eec6ff 100644 --- a/src/server.c +++ b/src/server.c @@ -146,9 +146,6 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// - - - (void)cache; //read file char filepath[4096]; struct file_data *filedata; //loads file into memory and returns pointer to data @@ -159,8 +156,11 @@ void get_file(int fd, struct cache *cache, char *request_path) printf("\"%s\"\n", filepath); //time comparison for expiration of cache (implemented with cache put/get) - //expire then refresh file and delete item out of cache - struct cache_entry *ce = cache_get(filepath); + struct cache_entry *ce = cache_get(cache, filepath); + //expire then refresh file and delete item out of cache (eg if cur time less that time greater than 1min) + if (time(NULL) - ce->timestamp > 60) { //timestamp decl in struct cache_entry in cache.h so know when it was created + + } //load that file filedata = file_load(filepath);