From a2649376ef8ef7e50a8ee01440e93a6c0f238474 Mon Sep 17 00:00:00 2001 From: sean-one Date: Mon, 11 Mar 2019 11:39:30 -0700 Subject: [PATCH 01/13] local init --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8fd528143..ed824ff47 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # A Simple Web Server in C In this project, we'll finish the implementation of a web server in C. From 9e4121b91e763d89a05f3a4384cc8ce94d58ca97 Mon Sep 17 00:00:00 2001 From: sean-one Date: Mon, 11 Mar 2019 22:27:06 -0700 Subject: [PATCH 02/13] working on getting correct responses --- README.md | 2 +- src/server.c | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ed824ff47..171721630 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ - + # A Simple Web Server in C In this project, we'll finish the implementation of a web server in C. diff --git a/src/server.c b/src/server.c index ea43306fc..d610fb0b3 100644 --- a/src/server.c +++ b/src/server.c @@ -38,6 +38,7 @@ #define SERVER_FILES "./serverfiles" #define SERVER_ROOT "./serverroot" +#define RAND_MAX 20 /** * Send an HTTP response @@ -52,12 +53,20 @@ 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]; + int response_length = strlen(response); // Build HTTP response and store it in response - + sprintf(response, "%s\n" + "Content-Type: %s\n" + "Content-Length: %d\n" + "Connection: close\n" + "\n" + "%p", header, content_type, content_length, body + ); /////////////////// // IMPLEMENT ME! // /////////////////// + printf("%s/n", response); // Send it all! int rv = send(fd, response, response_length, 0); @@ -75,8 +84,10 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont */ void get_d20(int fd) { + // printf("inside get_d20\n"); // Generate a random number between 1 and 20 inclusive - + // srand(time(0)); + printf("%d\n", rand()); /////////////////// // IMPLEMENT ME! // /////////////////// @@ -144,6 +155,9 @@ void handle_http_request(int fd, struct cache *cache) { const int request_buffer_size = 65536; // 64K char request[request_buffer_size]; + char method[200]; + char path[8192]; + char *get_method = "GET"; // Read request int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0); @@ -159,9 +173,16 @@ void handle_http_request(int fd, struct cache *cache) /////////////////// // Read the three components of the first request line + sscanf(request, "%s %s", method, path); + // printf("method: \"%s\"\n", method); + // printf("path: \"%s\"\n", path); // If GET, handle the get endpoints - + if (strcmp(method, "GET") == 0) { + if (strcmp(path, "/d20") == 0) { + get_d20(fd); + } + } // Check if it's /d20 and handle that special case // Otherwise serve the requested file by calling get_file() From 1b9c160cce3bf907bae0c7cca32566d144c9fb9b Mon Sep 17 00:00:00 2001 From: sean-one Date: Mon, 11 Mar 2019 23:19:29 -0700 Subject: [PATCH 03/13] got part of it going still working through --- src/server.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/server.c b/src/server.c index d610fb0b3..d048377d6 100644 --- a/src/server.c +++ b/src/server.c @@ -38,7 +38,6 @@ #define SERVER_FILES "./serverfiles" #define SERVER_ROOT "./serverroot" -#define RAND_MAX 20 /** * Send an HTTP response @@ -61,12 +60,11 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont "Content-Length: %d\n" "Connection: close\n" "\n" - "%p", header, content_type, content_length, body + "%s", header, content_type, content_length, body ); /////////////////// // IMPLEMENT ME! // /////////////////// - printf("%s/n", response); // Send it all! int rv = send(fd, response, response_length, 0); @@ -84,15 +82,27 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont */ void get_d20(int fd) { - // printf("inside get_d20\n"); // Generate a random number between 1 and 20 inclusive - // srand(time(0)); - printf("%d\n", rand()); + int random_value; + char body[500000]; + + srand(time(0)); + int get_random() { + int num = (rand() % (20 - 1 + 1)) + 1; + return num; + } + + random_value = get_random(); + + sprintf(body, "

%d

", random_value); + + int content_length = strlen(body); /////////////////// // IMPLEMENT ME! // /////////////////// // Use send_response() to send it back as text/plain data + send_response(fd, "HTTP/1.1 200 OK", "text/plain", body, content_length); /////////////////// // IMPLEMENT ME! // From e1f5cc23b9c1facbc4c22bd52e8543f25587e886 Mon Sep 17 00:00:00 2001 From: sean-one Date: Tue, 12 Mar 2019 11:07:42 -0700 Subject: [PATCH 04/13] cleaned up after lecture --- src/server.c | 9 ++++++--- src/serverroot/{cat.jpg => cat.png} | Bin 2 files changed, 6 insertions(+), 3 deletions(-) rename src/serverroot/{cat.jpg => cat.png} (100%) diff --git a/src/server.c b/src/server.c index d048377d6..bb1f49130 100644 --- a/src/server.c +++ b/src/server.c @@ -52,11 +52,12 @@ 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]; - int response_length = strlen(response); // Build HTTP response and store it in response - sprintf(response, "%s\n" + // change this to be the content length from return value + int response_length = sprintf(response, "%s\n" "Content-Type: %s\n" + // need to add DATE to response "Content-Length: %d\n" "Connection: close\n" "\n" @@ -94,7 +95,7 @@ void get_d20(int fd) random_value = get_random(); - sprintf(body, "

%d

", random_value); + sprintf(body, "%d\n", random_value); int content_length = strlen(body); /////////////////// @@ -191,6 +192,8 @@ void handle_http_request(int fd, struct cache *cache) if (strcmp(method, "GET") == 0) { if (strcmp(path, "/d20") == 0) { get_d20(fd); + } else { + resp_404(fd); } } // Check if it's /d20 and handle that special case 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 a75598c4281530d6fff966bf576a3c1210cd38a0 Mon Sep 17 00:00:00 2001 From: sean-one Date: Tue, 12 Mar 2019 12:21:01 -0700 Subject: [PATCH 05/13] able to get index.html --- src/file.c | 1 + src/server.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/file.c b/src/file.c index 5277a92f4..b972c5352 100644 --- a/src/file.c +++ b/src/file.c @@ -14,6 +14,7 @@ struct file_data *file_load(char *filename) struct stat buf; int bytes_read, bytes_remaining, total_bytes = 0; + printf("%s", filename); // Get the file size if (stat(filename, &buf) == -1) { return NULL; diff --git a/src/server.c b/src/server.c index bb1f49130..97e23efcf 100644 --- a/src/server.c +++ b/src/server.c @@ -55,7 +55,7 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont // Build HTTP response and store it in response // change this to be the content length from return value - int response_length = sprintf(response, "%s\n" + int response_length = snprintf(response, max_response_size, "%s\n" "Content-Type: %s\n" // need to add DATE to response "Content-Length: %d\n" @@ -142,7 +142,27 @@ void resp_404(int fd) 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 requested file + snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); + filedata = file_load(filepath); + + if (filedata == NULL) { + // TODO: make this non-fatal + fprintf(stderr, "cannot find system requested 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); + + (void) cache; /////////////////// } @@ -156,6 +176,8 @@ char *find_start_of_body(char *header) { /////////////////// // IMPLEMENT ME! // (Stretch) + (void) header; + return NULL; /////////////////// } @@ -168,7 +190,6 @@ void handle_http_request(int fd, struct cache *cache) char request[request_buffer_size]; char method[200]; char path[8192]; - char *get_method = "GET"; // Read request int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0); @@ -187,11 +208,13 @@ void handle_http_request(int fd, struct cache *cache) sscanf(request, "%s %s", method, path); // printf("method: \"%s\"\n", method); // printf("path: \"%s\"\n", path); - + (void) cache; // If GET, handle the get endpoints if (strcmp(method, "GET") == 0) { if (strcmp(path, "/d20") == 0) { get_d20(fd); + } else if (strcmp(path, "/index.html") == 0) { + get_file(fd, cache, path); } else { resp_404(fd); } From 74fda5cf8bd92a0ea74942e31a9908dfe0207e44 Mon Sep 17 00:00:00 2001 From: sean-one Date: Wed, 13 Mar 2019 12:39:10 -0700 Subject: [PATCH 06/13] never wanted to see a cat so bad | png file works --- src/file.c | 1 - src/server.c | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/file.c b/src/file.c index b972c5352..5277a92f4 100644 --- a/src/file.c +++ b/src/file.c @@ -14,7 +14,6 @@ struct file_data *file_load(char *filename) struct stat buf; int bytes_read, bytes_remaining, total_bytes = 0; - printf("%s", filename); // Get the file size if (stat(filename, &buf) == -1) { return NULL; diff --git a/src/server.c b/src/server.c index 97e23efcf..b1ea9f648 100644 --- a/src/server.c +++ b/src/server.c @@ -60,13 +60,13 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont // need to add DATE to response "Content-Length: %d\n" "Connection: close\n" - "\n" - "%s", header, content_type, content_length, body + "\n", header, content_type, content_length ); /////////////////// // IMPLEMENT ME! // /////////////////// - + memcpy(response + response_length, body, content_length); + response_length += content_length; // Send it all! int rv = send(fd, response, response_length, 0); @@ -153,6 +153,7 @@ void get_file(int fd, struct cache *cache, char *request_path) if (filedata == NULL) { // TODO: make this non-fatal fprintf(stderr, "cannot find system requested file\n"); + resp_404(fd); exit(3); } @@ -213,7 +214,7 @@ void handle_http_request(int fd, struct cache *cache) if (strcmp(method, "GET") == 0) { if (strcmp(path, "/d20") == 0) { get_d20(fd); - } else if (strcmp(path, "/index.html") == 0) { + } else if ((strcmp(path, "/index.html") == 0) || (strcmp(path, "/cat.png") == 0)) { get_file(fd, cache, path); } else { resp_404(fd); From eac1ac3e5708dd514a2f49743830aaba5dcbdab3 Mon Sep 17 00:00:00 2001 From: sean-one Date: Wed, 13 Mar 2019 14:09:23 -0700 Subject: [PATCH 07/13] cleaned some stuff up and added \ forward --- src/server.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/server.c b/src/server.c index b1ea9f648..4278e5e14 100644 --- a/src/server.c +++ b/src/server.c @@ -126,7 +126,7 @@ void resp_404(int fd) if (filedata == NULL) { // TODO: make this non-fatal fprintf(stderr, "cannot find system 404 file\n"); - exit(3); + resp_404(fd); } mime_type = mime_type_get(filepath); @@ -151,10 +151,7 @@ void get_file(int fd, struct cache *cache, char *request_path) filedata = file_load(filepath); if (filedata == NULL) { - // TODO: make this non-fatal - fprintf(stderr, "cannot find system requested file\n"); resp_404(fd); - exit(3); } mime_type = mime_type_get(filepath); @@ -216,7 +213,13 @@ void handle_http_request(int fd, struct cache *cache) get_d20(fd); } else if ((strcmp(path, "/index.html") == 0) || (strcmp(path, "/cat.png") == 0)) { get_file(fd, cache, path); - } else { + } // else if (strcmp(path, "/") == 0) { + // char *home; + // home = "index.html"; + // strcat(path, home); + // get_file(fd, cache, path); + //} + else { resp_404(fd); } } From c1df45f6e5e0cf273012e1ad022914773bcbb1c1 Mon Sep 17 00:00:00 2001 From: sean-one Date: Wed, 13 Mar 2019 21:59:59 -0700 Subject: [PATCH 08/13] update --- src/cache.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cache.c b/src/cache.c index c72975cdd..d9b12d955 100644 --- a/src/cache.c +++ b/src/cache.c @@ -92,6 +92,10 @@ struct cache_entry *dllist_remove_tail(struct cache *cache) struct cache *cache_create(int max_size, int hashsize) { /////////////////// + // cache *cache = malloc(sizeof(cache)); + // cache->max_size = max_size; + // cache->cur_size = 0; + // IMPLEMENT ME! // /////////////////// } From 383c5eafb74751c558c9c378fb8b0c445a8a70fd Mon Sep 17 00:00:00 2001 From: SeanOne Date: Thu, 14 Mar 2019 00:32:39 -0700 Subject: [PATCH 09/13] working on create_cache --- src/cache.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cache.c b/src/cache.c index d9b12d955..7c4d1baec 100644 --- a/src/cache.c +++ b/src/cache.c @@ -92,11 +92,16 @@ struct cache_entry *dllist_remove_tail(struct cache *cache) struct cache *cache_create(int max_size, int hashsize) { /////////////////// - // cache *cache = malloc(sizeof(cache)); - // cache->max_size = max_size; - // cache->cur_size = 0; + struct cache *cache = malloc(sizeof(struct cache)); - // IMPLEMENT ME! // + cache->index = hashtable_create(hashsize, NULL); + cache->max_size = max_size; + cache->cur_size = 0; + + cache->head = NULL; + cache->tail = NULL; + + return cache; /////////////////// } From edf5c9c4eece624d33a6a13bc3ece7565d1d346f Mon Sep 17 00:00:00 2001 From: sean-one Date: Thu, 14 Mar 2019 12:56:29 -0700 Subject: [PATCH 10/13] finished cache_entry --- src/cache.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cache.c b/src/cache.c index 7c4d1baec..502a02a0d 100644 --- a/src/cache.c +++ b/src/cache.c @@ -10,7 +10,20 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length) { /////////////////// - // IMPLEMENT ME! // + struct cache_entry *ce = malloc(sizeof(struct cache_entry)); + + ce->path = malloc(strlen(path) + 1); + strcpy(ce->path, path); + + ce->content_type(strlen(content_type) + 1); + strcpy(ce->content_type, content_type); + + ce->content_length = content_length; + + ce->prev = NULL; + ce->next = NULL; + + return ce; /////////////////// } From dd5564017471fccd3ac0781aec336fdd96d45019 Mon Sep 17 00:00:00 2001 From: sean-one Date: Thu, 14 Mar 2019 13:04:28 -0700 Subject: [PATCH 11/13] finished free_entry --- src/cache.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cache.c b/src/cache.c index 502a02a0d..2fea98821 100644 --- a/src/cache.c +++ b/src/cache.c @@ -33,7 +33,10 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i void free_entry(struct cache_entry *entry) { /////////////////// - // IMPLEMENT ME! // + free(entry->path); + free(entry->content_type); + free(entry->content); + free(entry); /////////////////// } From ef5a49b119fe6e7e9f8dc6f9baac445fc0b98f91 Mon Sep 17 00:00:00 2001 From: sean-one Date: Thu, 14 Mar 2019 14:38:02 -0700 Subject: [PATCH 12/13] working on cache_put, having a tuff time following --- src/cache.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cache.c b/src/cache.c index 2fea98821..f61928705 100644 --- a/src/cache.c +++ b/src/cache.c @@ -148,7 +148,22 @@ void cache_free(struct cache *cache) void cache_put(struct cache *cache, char *path, char *content_type, void *content, int content_length) { /////////////////// - // 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_insert_head(cache, ce); + // Store the entry in the hashtable as well, indexed by the entry's `path`. + + // Increment the current size of the cache. + cache->cur_size += 1; + // 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. + // Remove that same entry from the hashtable, using the entry's `path` and the `hashtable_delete` function. + // Free the cache entry. + // Ensure the size counter for the number of entries in the cache is correct. + + } /////////////////// } From 1a38e1344a5730f839b328e8e32a936751b172e7 Mon Sep 17 00:00:00 2001 From: sean-one Date: Thu, 14 Mar 2019 21:26:40 -0700 Subject: [PATCH 13/13] quick clean up and update --- src/cache.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cache.c b/src/cache.c index f61928705..97c571cb2 100644 --- a/src/cache.c +++ b/src/cache.c @@ -15,11 +15,15 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i ce->path = malloc(strlen(path) + 1); strcpy(ce->path, path); - ce->content_type(strlen(content_type) + 1); + ce->content_type = (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 = NULL; ce->next = NULL; @@ -149,9 +153,9 @@ 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); + struct cache_entry *new_ce = alloc_entry(path, content_type, content, content_length); // Insert the entry at the head of the doubly-linked list. - dllist_insert_head(cache, ce); + dllist_insert_head(cache, new_ce); // Store the entry in the hashtable as well, indexed by the entry's `path`. // Increment the current size of the cache.