From 9ecadcf93859af5a44304b393a833fa96ed28d74 Mon Sep 17 00:00:00 2001 From: txiong000 Date: Mon, 11 Mar 2019 14:09:22 -0500 Subject: [PATCH 1/5] Init commit Toua C-web-server --- src/cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache.c b/src/cache.c index c72975cdd..c6eee7c6d 100644 --- a/src/cache.c +++ b/src/cache.c @@ -5,7 +5,7 @@ #include "cache.h" /** - * Allocate a cache entry + * Allocate a cach entry */ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length) { From 89c15b7802929cdfe841870d1071ccc1a6a809f8 Mon Sep 17 00:00:00 2001 From: txiong000 Date: Mon, 11 Mar 2019 16:57:52 -0500 Subject: [PATCH 2/5] send_response --- src/server.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index ea43306fc..c71f05a8e 100644 --- a/src/server.c +++ b/src/server.c @@ -59,8 +59,11 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont // IMPLEMENT ME! // /////////////////// + // Send it all! - int rv = send(fd, response, response_length, 0); + int response_length = sprintf(response, "%s\nConnection: close\nContent-Length: %d\nContent-Type: %s\nDate: %s\n", header, content_length, content_type); + + if (rv < 0) { perror("send"); From 4594c76951a0e14230adfaef1824b254b7fab5e1 Mon Sep 17 00:00:00 2001 From: txiong000 Date: Tue, 12 Mar 2019 14:35:06 -0500 Subject: [PATCH 3/5] handle http request, get_d20 --- src/server.c | 74 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/src/server.c b/src/server.c index c71f05a8e..563fa3778 100644 --- a/src/server.c +++ b/src/server.c @@ -54,16 +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 - + int response_length = snprintf(response, max_response_size, + "%s\n" + "Connection: close \n" + // "Date : %s" + "Content-Length: %d\n" + "Content-Type: %s\n" + "\n" + "%s", + header, content_length, content_type, body + ); /////////////////// // IMPLEMENT ME! // /////////////////// - - + // Send it all! - int response_length = sprintf(response, "%s\nConnection: close\nContent-Length: %d\nContent-Type: %s\nDate: %s\n", header, content_length, content_type); - - + int rv = send(fd, response, response_length, 0); if (rv < 0) { perror("send"); @@ -79,13 +85,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 - + char response[12]; + int random = (rand() % 20) + 1; + sprintf(response, "%d", random); /////////////////// // IMPLEMENT ME! // /////////////////// - + + char response[12]; + int random = (rand() % 20) + 1; + sprintf(response, "%d", random); // Use send_response() to send it back as text/plain data - + send_response(fd, "HTTP/1.1 200 OK", "text/plain", response, strlen(response)); /////////////////// // IMPLEMENT ME! // /////////////////// @@ -125,6 +136,25 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// + char filepath[4096]; + struct file_data *filedata; + char *mime_type; + + // Fetch the 404.html file + snprintf(filepath, sizeof filepath, "%s/404.html", SERVER_FILES); + filedata = file_load(filepath); + + if (filedata == NULL) { + // TODO: make this non-fatal + fprintf(stderr, "cannot find system 404 file\n"); + exit(3); + } + + mime_type = mime_type_get(filepath); + + send_response(fd, "HTTP/1.1 200 ok", mime_type, filedata->data, filedata->size); + + file_free(filedata); } /** @@ -138,6 +168,8 @@ char *find_start_of_body(char *header) /////////////////// // IMPLEMENT ME! // (Stretch) /////////////////// + (void) header; + return NULL; } /** @@ -147,6 +179,9 @@ void handle_http_request(int fd, struct cache *cache) { const int request_buffer_size = 65536; // 64K char request[request_buffer_size]; + //similar to yesterday + char method[512]; //GET or POST + char path[8192]; // Read request int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0); @@ -156,19 +191,32 @@ void handle_http_request(int fd, struct cache *cache) return; } - + (void)cache; /////////////////// // IMPLEMENT ME! // /////////////////// // Read the three components of the first request line - + sscanf(request, "%s %s", method, path); + printf("GOT REQUEST: \"%s\" \"%s\"\n", method, path); + + //testing function. + // resp_404(fd); // 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, "d20") == 0) { + get_d20(fd) + } + else { + get_file(fd, cache, path) + } + } else { + resp_404(fd); + } + // (Stretch) If POST, handle the post request } From 201236860989c295151176f3b5bfaa1344319c67 Mon Sep 17 00:00:00 2001 From: txiong000 Date: Wed, 13 Mar 2019 17:33:17 -0500 Subject: [PATCH 4/5] cache_put --- src/cache.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/cache.c b/src/cache.c index c6eee7c6d..955fe2ca8 100644 --- a/src/cache.c +++ b/src/cache.c @@ -125,6 +125,23 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache_entry *new = alloc_entry(path, content_type, content, content_length); + + dllist_insert_head(cache, new); + + hashtable_put(cache->index, path, new); + + cache->cur_size++; + + if (cache->cur_size > cache->max_size) { + struct cache_entry *last_tail = dllist_remove_tail(cache); + hashtable_delete(cache->index, last_tail->path); + free_entry(last_tail); + + if (cache->cur_size > cache->max_size) { + cache->cur_size--; + } + } } /** @@ -135,4 +152,7 @@ struct cache_entry *cache_get(struct cache *cache, char *path) /////////////////// // IMPLEMENT ME! // /////////////////// + + + } From 13d827235de4dce77637122e1e82439e0e309c1c Mon Sep 17 00:00:00 2001 From: txiong000 Date: Thu, 14 Mar 2019 17:21:12 -0500 Subject: [PATCH 5/5] cache_get --- src/cache.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/cache.c b/src/cache.c index 955fe2ca8..75abdd46a 100644 --- a/src/cache.c +++ b/src/cache.c @@ -12,6 +12,32 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i /////////////////// // IMPLEMENT ME! // /////////////////// + //use malloc to some degree + struct cache_entry *ce = malloc(sizeof *ce); + /*Shallow*/ + // ce->path = path; + // ce->content_type = content_type; + // ce->content_length = content_length; + // ce->content = content; + + /*Deep Copy */ + ce->path = malloc(strlen(path) + 1); //allocate a block of memory + //copy path into the allocated space + strcpy(ce->path, path); // deep copy + + ce->content_type = malloc(strlen(content_type ) + 1); + strcpy(ce->content_type, content_type); + + ce->content_length = content_length; + + ce->content = malloc(content_length); + memcpy(ce->content, content, content_length); + + ce->prev = ce->next = NULL; + // ce->prev = NULL; + // ce->next = NULL; + + return ce; } /** @@ -22,6 +48,10 @@ void free_entry(struct cache_entry *entry) /////////////////// // IMPLEMENT ME! // /////////////////// + free(entry->content_type); + free(entry->content); + free(entry->path); + free(entry); } /** @@ -94,6 +124,14 @@ struct cache *cache_create(int max_size, int hashsize) /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache *cache = malloc(sizeof *cache); + + cache->index = hashtable_create(hashsize, NULL); + cache->head = cache->tail = NULL; + cache->max_size = max_size; + cache->cur_size = 0; + + return cache } void cache_free(struct cache *cache) @@ -152,7 +190,18 @@ struct cache_entry *cache_get(struct cache *cache, char *path) /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache_entry *entry = hashtable_get(cache->index, path); + + + if (entry == NULL) { + return NULL; + } + + dllist_move_to_head(cache, entry); + + return entry; + }