From 056b7724ec27d707dd164760e5a0b573783d0ba9 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Mon, 15 Apr 2019 14:41:28 -0700 Subject: [PATCH 1/9] reviewing source files --- src/autorun.sh | 1 + src/run.sh | 1 + src/server.c | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) create mode 100755 src/autorun.sh create mode 100755 src/run.sh diff --git a/src/autorun.sh b/src/autorun.sh new file mode 100755 index 000000000..f51f5e0c9 --- /dev/null +++ b/src/autorun.sh @@ -0,0 +1 @@ +nodemon -w . --exec "make clean; clear; make && ./server" diff --git a/src/run.sh b/src/run.sh new file mode 100755 index 000000000..a7edc6f0d --- /dev/null +++ b/src/run.sh @@ -0,0 +1 @@ +make clean; clear; make && ./server \ No newline at end of file diff --git a/src/server.c b/src/server.c index 30b878464..658d8f137 100644 --- a/src/server.c +++ b/src/server.c @@ -54,11 +54,22 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont char response[max_response_size]; // Build HTTP response and store it in response + body = "
Hello
"; + int response_length = strlen(body); + + sprintf(response, "HTTP/1.1 200 OK\n" + "Content-Type: text/html\n" + "Content-Length: %d\n" + "Connection: close\n" + "\n" + "%s", response_length, body); /////////////////// // IMPLEMENT ME! // /////////////////// + printf("test"); + // Send it all! int rv = send(fd, response, response_length, 0); @@ -66,6 +77,7 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont perror("send"); } + return rv; } @@ -76,6 +88,8 @@ 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 + srand(1); + float d20_val = rand(); /////////////////// // IMPLEMENT ME! // @@ -159,6 +173,8 @@ void handle_http_request(int fd, struct cache *cache) /////////////////// // Read the first two components of the first line of the request + + resp_404(fd); // If GET, handle the get endpoints From d85467695fb5ace6776019016fd0ffdb69a05497 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Mon, 15 Apr 2019 15:28:50 -0700 Subject: [PATCH 2/9] writing handle_http_response --- src/server.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/server.c b/src/server.c index 658d8f137..6f04cf233 100644 --- a/src/server.c +++ b/src/server.c @@ -91,6 +91,7 @@ void get_d20(int fd) srand(1); float d20_val = rand(); + printf("d20 roll: %f\n", d20_val); /////////////////// // IMPLEMENT ME! // /////////////////// @@ -172,16 +173,42 @@ void handle_http_request(int fd, struct cache *cache) // IMPLEMENT ME! // /////////////////// - // Read the first two components of the first line of the request + // char *request_parts = strtok(request, " "); + - resp_404(fd); + // parse header + + // Read the first two components of the first line of the request + printf("request: %s\n", request); // If GET, handle the get endpoints + char *is_get = strstr(request, "GET"); + char *route = strstr(request, "/"); + + + + char filepath[4096]; + struct file_data *filedata; + char *mime_type; + if (is_get == "GET") { + snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_FILES); + filedata = file_load(filepath); + + if (filedata == NULL) { + fprintf(stderr, "cannot find index.html\n"); + exit(3); + } + + mime_type = mime_type_get(filepath); + + send_response(fd, "HTTP/1.1 200 index.html", mime_type, filedata->data, filedata->size); + file_free(filedata); + } // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() - + resp_404(fd); // (Stretch) If POST, handle the post request } From 6735abcab545e5a3d13833a295f9a64d9937a82d Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Tue, 16 Apr 2019 09:36:42 -0700 Subject: [PATCH 3/9] send_response sends back a http resonse --- src/server.c | 57 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/server.c b/src/server.c index 6f04cf233..d9aaedce2 100644 --- a/src/server.c +++ b/src/server.c @@ -53,22 +53,34 @@ 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]; + /////////////////// + // IMPLEMENT ME! // + /////////////////// + // Build HTTP response and store it in response - body = "
Hello
"; - int response_length = strlen(body); + // body = "
Hello
"; + int body_length = strlen(body); - sprintf(response, "HTTP/1.1 200 OK\n" - "Content-Type: text/html\n" + printf("body:%s\n%i",body, body_length); + + time_t rawtime; + struct tm * timeinfo; + + time (&rawtime); + timeinfo = localtime (&rawtime); + char * current_date = asctime(timeinfo); + printf ("Current local time and date: %s", current_date); + + int response_length = sprintf(&response, "HTTP/1.1 200 OK\n" + "Date: %s" + "Connection: keep-alive\n" "Content-Length: %d\n" - "Connection: close\n" + "Content-Type: text/html\n" "\n" - "%s", response_length, body); + "%s", current_date, body_length, body); - /////////////////// - // IMPLEMENT ME! // - /////////////////// + printf("sr sprintf:%s\n-----------\n", response); - printf("test"); // Send it all! int rv = send(fd, response, response_length, 0); @@ -123,7 +135,7 @@ void resp_404(int fd) } mime_type = mime_type_get(filepath); - + // printf("\"%s\"", filedata->data); send_response(fd, "HTTP/1.1 404 NOT FOUND", mime_type, filedata->data, filedata->size); file_free(filedata); @@ -179,19 +191,26 @@ void handle_http_request(int fd, struct cache *cache) // parse header // Read the first two components of the first line of the request - printf("request: %s\n", request); + // printf("request: %s\n", request); // If GET, handle the get endpoints char *is_get = strstr(request, "GET"); - char *route = strstr(request, "/"); - + char *route = strchr(request, '/'); + printf("request endpoint: \"%s\" \"%s\"\n", is_get, route); char filepath[4096]; struct file_data *filedata; char *mime_type; - if (is_get == "GET") { - snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_FILES); + + + // trying to get a response + resp_404(fd); + +/* + if (is_get == "GET" || is_get != NULL) { + + snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT); filedata = file_load(filepath); if (filedata == NULL) { @@ -204,11 +223,15 @@ void handle_http_request(int fd, struct cache *cache) send_response(fd, "HTTP/1.1 200 index.html", mime_type, filedata->data, filedata->size); file_free(filedata); } + else + { + printf("is get is null: \n"); + } +*/ // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() - resp_404(fd); // (Stretch) If POST, handle the post request } From 812d2b4e0ba0a277c52f82ebac02383c86c34a95 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Tue, 16 Apr 2019 14:12:34 -0700 Subject: [PATCH 4/9] got index.html to load --- src/server.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/server.c b/src/server.c index d9aaedce2..f34ddda29 100644 --- a/src/server.c +++ b/src/server.c @@ -58,29 +58,24 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont /////////////////// // Build HTTP response and store it in response - // body = "
Hello
"; - int body_length = strlen(body); - - printf("body:%s\n%i",body, body_length); + // gets current time as a string time_t rawtime; struct tm * timeinfo; - time (&rawtime); timeinfo = localtime (&rawtime); + char * current_date = asctime(timeinfo); - printf ("Current local time and date: %s", current_date); - int response_length = sprintf(&response, "HTTP/1.1 200 OK\n" + int response_length = sprintf(&response, "%s\n" "Date: %s" - "Connection: keep-alive\n" + "Connection: close\n" "Content-Length: %d\n" "Content-Type: text/html\n" "\n" - "%s", current_date, body_length, body); - - printf("sr sprintf:%s\n-----------\n", response); + "%s", header, current_date, content_length, body); + printf("response\n------------\n%s\n-----------\n", response); // Send it all! int rv = send(fd, response, response_length, 0); @@ -89,7 +84,6 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont perror("send"); } - return rv; } @@ -169,7 +163,7 @@ char *find_start_of_body(char *header) */ void handle_http_request(int fd, struct cache *cache) { - const int request_buffer_size = 65536; // 64K + const int request_buffer_size = 262144; // 256 kb char request[request_buffer_size]; // Read request @@ -186,7 +180,11 @@ void handle_http_request(int fd, struct cache *cache) /////////////////// // char *request_parts = strtok(request, " "); + // printf("request verb: %s request endpoint: %s", request_parts[0],request_parts[1]); + char request_type[10], path[50]; + sscanf(request, "%s %s", request_type, path); + printf("request: %s %s\n", request_type, path); // parse header @@ -194,21 +192,19 @@ void handle_http_request(int fd, struct cache *cache) // printf("request: %s\n", request); // If GET, handle the get endpoints - char *is_get = strstr(request, "GET"); - char *route = strchr(request, '/'); + // char *is_get = strstr(request, "GET"); + // char *route = strchr(request, '/'); - printf("request endpoint: \"%s\" \"%s\"\n", is_get, route); + // printf("request endpoint: \"%s\" \"%s\"\n", is_get, route); char filepath[4096]; struct file_data *filedata; char *mime_type; - // trying to get a response - resp_404(fd); - -/* - if (is_get == "GET" || is_get != NULL) { + if (strcmp(request_type, "GET") == 0 ) + { + printf("is get\n"); snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT); filedata = file_load(filepath); @@ -220,14 +216,14 @@ void handle_http_request(int fd, struct cache *cache) mime_type = mime_type_get(filepath); - send_response(fd, "HTTP/1.1 200 index.html", mime_type, filedata->data, filedata->size); + send_response(fd, "HTTP/1.1 200 /index.html", mime_type, filedata->data, filedata->size); file_free(filedata); } else { printf("is get is null: \n"); + resp_404(fd); } -*/ // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() From 09d5c1f691aab8608e62fad0e46a51b3e3a9ac9a Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Tue, 16 Apr 2019 14:21:56 -0700 Subject: [PATCH 5/9] will only render homepage on get /index.html --- src/server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.c b/src/server.c index f34ddda29..623a01baa 100644 --- a/src/server.c +++ b/src/server.c @@ -202,7 +202,7 @@ void handle_http_request(int fd, struct cache *cache) char *mime_type; - if (strcmp(request_type, "GET") == 0 ) + if (strcmp(request_type, "GET") == 0 && strcmp(path, "/index.html") == 0) { printf("is get\n"); @@ -221,7 +221,7 @@ void handle_http_request(int fd, struct cache *cache) } else { - printf("is get is null: \n"); + printf("path does not exist \n"); resp_404(fd); } From 5d6236dd7e37e602a8710e6fc21e152cb4234c9b Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Tue, 16 Apr 2019 15:08:13 -0700 Subject: [PATCH 6/9] server sends back random number without crashing --- src/server.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/server.c b/src/server.c index 623a01baa..79acabc98 100644 --- a/src/server.c +++ b/src/server.c @@ -39,6 +39,15 @@ #define SERVER_FILES "./serverfiles" #define SERVER_ROOT "./serverroot" +// int get_current_time() { +// // gets current time as a string +// time_t rawtime; +// struct tm * timeinfo; +// time (&rawtime); +// timeinfo = localtime (&rawtime); +// return timeinfo; +// } + /** * Send an HTTP response * @@ -94,19 +103,27 @@ 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 - srand(1); - float d20_val = rand(); - - printf("d20 roll: %f\n", d20_val); + /////////////////// // IMPLEMENT ME! // /////////////////// - // Use send_response() to send it back as text/plain data + // time_t rawtime; + srand(time(NULL)); + int d20_val = rand(); + + char output[100]; // one hundred just to be safe + sprintf(output, "%d\0", d20_val); + int output_len = strlen(output);//(int)((ceil(log10(d20_val))+1)*sizeof(char)); + printf("d20 roll: %f\n", d20_val); + + // Use send_response() to send it back as text/plain data /////////////////// // IMPLEMENT ME! // /////////////////// + + send_response(fd, "http/1.1 200 /d20", "text/plain", output, output_len); } /** @@ -219,6 +236,10 @@ void handle_http_request(int fd, struct cache *cache) send_response(fd, "HTTP/1.1 200 /index.html", mime_type, filedata->data, filedata->size); file_free(filedata); } + else if (strcmp(request_type, "GET") == 0 && strcmp(path, "/d20") == 0) + { + get_d20(fd); + } else { printf("path does not exist \n"); From f0a0d726f1150f23abdba200fd6e457cc495230b Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 17 Apr 2019 09:31:09 -0700 Subject: [PATCH 7/9] can serve up images --- src/server.c | 68 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/src/server.c b/src/server.c index 79acabc98..c64b182e1 100644 --- a/src/server.c +++ b/src/server.c @@ -48,6 +48,20 @@ // return timeinfo; // } +void mem_copy(void *dest, const void *src, int n) +{ + // type cast to char + char * chardest = (char*) dest; + char * charsrc = (char*) src; + // get num chars from bytes + int length = n / sizeof(char); + // copy values + for (int i = 0; i < length; i++) { + chardest[i] = charsrc[i]; + } + chardest[length+1] = '\0'; +} + /** * Send an HTTP response * @@ -76,18 +90,24 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont char * current_date = asctime(timeinfo); - int response_length = sprintf(&response, "%s\n" + int header_length = sprintf(&response, "%s\n" "Date: %s" "Connection: close\n" "Content-Length: %d\n" - "Content-Type: text/html\n" - "\n" - "%s", header, current_date, content_length, body); + "Content-Type: %s\n" + "\n", header, current_date, content_length, content_type); + + // char *file_blob = malloc(content_length); + printf("copying memory %d - %d\n", header_length, content_length); + + memcpy(response + header_length , body, content_length); + + // response[header_length + content_length+1] = '\0'; printf("response\n------------\n%s\n-----------\n", response); // Send it all! - int rv = send(fd, response, response_length, 0); + int rv = send(fd, response, header_length + content_length, 0); if (rv < 0) { perror("send"); @@ -160,6 +180,33 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// + + struct file_data *filedata; + char *mime_type; + char filepath[4096]; + + + // snprintf(request_path, sizeof request_path, "%s/index.html", SERVER_ROOT); + snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); + printf("getting file: %s\n", filepath); + filedata = file_load(filepath); + + if (filedata == NULL) { + fprintf(stderr, "cannot find %s\n", filepath); + printf("path does not exist \n"); + resp_404(fd); + // exit(3); + return; + } + + mime_type = mime_type_get(filepath); + + char header[64]; + sprintf(header, "HTTP/1.1 200 %s", request_path); + printf("header: %s\n", header); + + send_response(fd, header, mime_type, filedata->data, filedata->size); + file_free(filedata); } /** @@ -238,14 +285,19 @@ void handle_http_request(int fd, struct cache *cache) } else if (strcmp(request_type, "GET") == 0 && strcmp(path, "/d20") == 0) { + get_d20(fd); } else { - printf("path does not exist \n"); - resp_404(fd); - } + // snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, path); + // printf("%s", filepath); + + // check if file exists? + get_file(fd, cache, path); + + } // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() From acb030d1b51a069001f58b318d31da4808ee9c93 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 17 Apr 2019 16:46:50 -0700 Subject: [PATCH 8/9] implimented missing cache functions. --- src/cache.c | 45 +++++++++++++++++++-- src/server.c | 108 +++++++++++++++++++++++++++++---------------------- 2 files changed, 104 insertions(+), 49 deletions(-) diff --git a/src/cache.c b/src/cache.c index c72975cdd..adc329622 100644 --- a/src/cache.c +++ b/src/cache.c @@ -12,6 +12,15 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache_entry * new_entry = malloc(sizeof(struct cache_entry)); + new_entry->path = path; + new_entry->content_type = content_type; + new_entry->content = content; + new_entry->content_length = content_length; + new_entry->next = NULL; + new_entry->prev = NULL; + + return new_entry; } /** @@ -22,6 +31,7 @@ void free_entry(struct cache_entry *entry) /////////////////// // IMPLEMENT ME! // /////////////////// + free(entry); } /** @@ -94,6 +104,16 @@ struct cache *cache_create(int max_size, int hashsize) /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache *new_cache; + new_cache = malloc(sizeof(struct cache)); + new_cache->head = NULL; + new_cache->tail = NULL; + new_cache->cur_size = 0; + new_cache->max_size = max_size; + new_cache->index = malloc(sizeof(struct hashtable)); + new_cache->index->size = hashsize; + return new_cache; + } void cache_free(struct cache *cache) @@ -125,6 +145,21 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache_entry *new_entry = alloc_entry(path, content_type, content, content_length); + + dllist_insert_head(cache, new_entry); + hashtable_put(cache->index, path, new_entry); + + cache->cur_size++; + if (cache->cur_size > cache->max_size) { + struct cache_entry *old_entry = dllist_remove_tail(cache); + hashtable_delete(cache->index, old_entry->path); + free_entry(old_entry); + // keep cache at max_size + cache->cur_size = cache->max_size; + + } + } /** @@ -132,7 +167,11 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten */ struct cache_entry *cache_get(struct cache *cache, char *path) { - /////////////////// - // IMPLEMENT ME! // - /////////////////// + // attempting to find entry in hashtable + struct cache_entry * ce = hashtable_get(cache->index, path); + if (ce == NULL) { + return NULL; + } + dllist_move_to_head(cache, ce); + return ce; } diff --git a/src/server.c b/src/server.c index c64b182e1..242b148a8 100644 --- a/src/server.c +++ b/src/server.c @@ -48,20 +48,6 @@ // return timeinfo; // } -void mem_copy(void *dest, const void *src, int n) -{ - // type cast to char - char * chardest = (char*) dest; - char * charsrc = (char*) src; - // get num chars from bytes - int length = n / sizeof(char); - // copy values - for (int i = 0; i < length; i++) { - chardest[i] = charsrc[i]; - } - chardest[length+1] = '\0'; -} - /** * Send an HTTP response * @@ -76,10 +62,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]; - /////////////////// - // IMPLEMENT ME! // - /////////////////// - // Build HTTP response and store it in response // gets current time as a string @@ -90,7 +72,7 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont char * current_date = asctime(timeinfo); - int header_length = sprintf(&response, "%s\n" + int header_length = sprintf(response, "%s\n" "Date: %s" "Connection: close\n" "Content-Length: %d\n" @@ -100,6 +82,9 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont // char *file_blob = malloc(content_length); printf("copying memory %d - %d\n", header_length, content_length); + if (header_length + content_length > max_response_size) { + content_length = content_length - (header_length + content_length - max_response_size); + } memcpy(response + header_length , body, content_length); // response[header_length + content_length+1] = '\0'; @@ -133,10 +118,10 @@ void get_d20(int fd) int d20_val = rand(); char output[100]; // one hundred just to be safe - sprintf(output, "%d\0", d20_val); + sprintf(output, "%d", d20_val); int output_len = strlen(output);//(int)((ceil(log10(d20_val))+1)*sizeof(char)); - printf("d20 roll: %f\n", d20_val); + printf("d20 roll: %d\n", d20_val); // Use send_response() to send it back as text/plain data /////////////////// @@ -184,29 +169,44 @@ void get_file(int fd, struct cache *cache, char *request_path) struct file_data *filedata; char *mime_type; char filepath[4096]; + char header[64]; + mime_type = mime_type_get(filepath); + sprintf(header, "HTTP/1.1 200 %s", request_path); + struct cache_entry * cached_entry = cache_get(cache, request_path); // snprintf(request_path, sizeof request_path, "%s/index.html", SERVER_ROOT); - snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); - printf("getting file: %s\n", filepath); - filedata = file_load(filepath); - - if (filedata == NULL) { - fprintf(stderr, "cannot find %s\n", filepath); - printf("path does not exist \n"); - resp_404(fd); - // exit(3); - return; + if (cached_entry != NULL) // file was cached + { + printf("serving cached file: %s \n", request_path); + send_response(fd, header, mime_type, cached_entry->content, cached_entry->content_length); } + else // load from disk + { - mime_type = mime_type_get(filepath); - char header[64]; - sprintf(header, "HTTP/1.1 200 %s", request_path); - printf("header: %s\n", header); + // concat request_path with local dir + snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); + printf("getting file: %s\n", filepath); + filedata = file_load(filepath); - send_response(fd, header, mime_type, filedata->data, filedata->size); - file_free(filedata); + if (filedata == NULL) { + fprintf(stderr, "cannot find %s\n", filepath); + printf("path does not exist \n"); + resp_404(fd); + // exit(3); + return; + } + + // cache file + cache_put(cache, request_path, mime_type, filedata->data, filedata->size); + + // mime_type = mime_type_get(filepath); + printf("header: %s\n", header); + + send_response(fd, header, mime_type, filedata->data, filedata->size); + file_free(filedata); + } } /** @@ -220,6 +220,7 @@ char *find_start_of_body(char *header) /////////////////// // IMPLEMENT ME! // (Stretch) /////////////////// + return ""; } /** @@ -268,20 +269,35 @@ void handle_http_request(int fd, struct cache *cache) if (strcmp(request_type, "GET") == 0 && strcmp(path, "/index.html") == 0) { - printf("is get\n"); - snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT); - filedata = file_load(filepath); - if (filedata == NULL) { - fprintf(stderr, "cannot find index.html\n"); - exit(3); + // I could make this into another function + printf("is get index.html\n"); + struct cache_entry * cached_entry = cache_get(cache, path); + if (cached_entry == NULL) + { + + snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT); + filedata = file_load(filepath); + + if (filedata == NULL) { + fprintf(stderr, "cannot find index.html\n"); + exit(3); + } + + mime_type = mime_type_get(filepath); + + cache_put(cache, path, mime_type, filedata->data, filedata->size); + send_response(fd, "HTTP/1.1 200 /index.html", mime_type, filedata->data, filedata->size); + file_free(filedata); } + else + { - mime_type = mime_type_get(filepath); + printf("serving cached file: %s \n", path); + send_response(fd, "HTTP/1.1 200 /index.html", mime_type, cached_entry->content, cached_entry->content_length); + } - send_response(fd, "HTTP/1.1 200 /index.html", mime_type, filedata->data, filedata->size); - file_free(filedata); } else if (strcmp(request_type, "GET") == 0 && strcmp(path, "/d20") == 0) { From 8f9db74b1437d634f78911bf86e4101f8ac477a8 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Thu, 18 Apr 2019 13:49:01 -0700 Subject: [PATCH 9/9] Forgot to call hashtable_create --- src/cache.c | 6 +++--- src/server.c | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cache.c b/src/cache.c index adc329622..4e86449c0 100644 --- a/src/cache.c +++ b/src/cache.c @@ -110,8 +110,8 @@ struct cache *cache_create(int max_size, int hashsize) new_cache->tail = NULL; new_cache->cur_size = 0; new_cache->max_size = max_size; - new_cache->index = malloc(sizeof(struct hashtable)); - new_cache->index->size = hashsize; + new_cache->index = hashtable_create(hashsize, NULL); + // new_cache->index->size = hashsize; return new_cache; } @@ -149,8 +149,8 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten dllist_insert_head(cache, new_entry); hashtable_put(cache->index, path, new_entry); - cache->cur_size++; + if (cache->cur_size > cache->max_size) { struct cache_entry *old_entry = dllist_remove_tail(cache); hashtable_delete(cache->index, old_entry->path); diff --git a/src/server.c b/src/server.c index 242b148a8..8ee34e8cf 100644 --- a/src/server.c +++ b/src/server.c @@ -274,6 +274,8 @@ void handle_http_request(int fd, struct cache *cache) // I could make this into another function printf("is get index.html\n"); struct cache_entry * cached_entry = cache_get(cache, path); + get_file(fd, cache, "index.html"); + /* if (cached_entry == NULL) { @@ -297,6 +299,7 @@ void handle_http_request(int fd, struct cache *cache) printf("serving cached file: %s \n", path); send_response(fd, "HTTP/1.1 200 /index.html", mime_type, cached_entry->content, cached_entry->content_length); } + */ } else if (strcmp(request_type, "GET") == 0 && strcmp(path, "/d20") == 0)