diff --git a/src/cache.c b/src/cache.c index c72975cdd..702d632c9 100644 --- a/src/cache.c +++ b/src/cache.c @@ -12,6 +12,27 @@ 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; + + // return new_entry; + new_entry->path = malloc(strlen(path) + 1); + strcpy(new_entry->path, path); + + new_entry->content_type = malloc(strlen(content_type) + 1); + strcpy(new_entry->content_type, content_type); + + new_entry->content = malloc(content_length); + memcpy(new_entry->content, content, content_length); + + new_entry->content_length = content_length; + + new_entry->prev = new_entry->next = NULL; + + return new_entry; } /** @@ -22,6 +43,11 @@ void free_entry(struct cache_entry *entry) /////////////////// // IMPLEMENT ME! // /////////////////// + + free(entry->path); + free(entry->content_type); + free(entry->content); + free(entry); } /** @@ -30,10 +56,13 @@ void free_entry(struct cache_entry *entry) void dllist_insert_head(struct cache *cache, struct cache_entry *ce) { // Insert at the head of the list - if (cache->head == NULL) { + if (cache->head == NULL) + { cache->head = cache->tail = ce; ce->prev = ce->next = NULL; - } else { + } + else + { cache->head->prev = ce; ce->next = cache->head; ce->prev = NULL; @@ -46,13 +75,16 @@ void dllist_insert_head(struct cache *cache, struct cache_entry *ce) */ void dllist_move_to_head(struct cache *cache, struct cache_entry *ce) { - if (ce != cache->head) { - if (ce == cache->tail) { + if (ce != cache->head) + { + if (ce == cache->tail) + { // We're the tail cache->tail = ce->prev; cache->tail->next = NULL; - - } else { + } + else + { // We're neither the head nor the tail ce->prev->next = ce->next; ce->next->prev = ce->prev; @@ -65,7 +97,6 @@ void dllist_move_to_head(struct cache *cache, struct cache_entry *ce) } } - /** * Removes the tail from the list and returns it * @@ -94,6 +125,17 @@ struct cache *cache_create(int max_size, int hashsize) /////////////////// // IMPLEMENT ME! // /////////////////// + struct cache *new_cache = malloc(sizeof(struct cache)); + struct hashtable *hash_table = hashtable_create(hashsize, NULL); + + // new_cache->head = NULL; + // new_cache->tail = NULL; + new_cache->head = new_cache->tail = NULL; + new_cache->cur_size = 0; + new_cache->max_size = max_size; + new_cache->index = hash_table; + + return new_cache; } void cache_free(struct cache *cache) @@ -102,7 +144,8 @@ void cache_free(struct cache *cache) hashtable_destroy(cache->index); - while (cur_entry != NULL) { + while (cur_entry != NULL) + { struct cache_entry *next_entry = cur_entry->next; free_entry(cur_entry); @@ -125,6 +168,26 @@ 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 += 1; + + 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); + if (cache->cur_size > cache->max_size) + { + cache->cur_size -= 1; + } + } } /** @@ -135,4 +198,15 @@ struct cache_entry *cache_get(struct cache *cache, char *path) /////////////////// // IMPLEMENT ME! // /////////////////// + 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, hashtable_get(cache->index, path)); + // Return the cache entry pointer. + return cache->head; + } } diff --git a/src/cache_tests/cache_tests.c b/src/cache_tests/cache_tests.c index 5e245d005..1f4689083 100644 --- a/src/cache_tests/cache_tests.c +++ b/src/cache_tests/cache_tests.c @@ -82,7 +82,7 @@ char *test_cache_put() mu_assert(check_cache_entries(cache->head->next->prev, test_entry_3) == 0, "Your cache_put function did not update the head->next->prev pointer to point to the new head entry"); 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"); + 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); diff --git a/src/server.c b/src/server.c index ea43306fc..f98de6e59 100644 --- a/src/server.c +++ b/src/server.c @@ -34,7 +34,7 @@ #include "mime.h" #include "cache.h" -#define PORT "3490" // the port users will be connecting to +#define PORT "3490" // the port users will be connecting to #define SERVER_FILES "./serverfiles" #define SERVER_ROOT "./serverroot" @@ -54,29 +54,43 @@ 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", + header, content_length, content_type); /////////////////// // IMPLEMENT ME! // /////////////////// - + memcpy(response + response_length, body, content_length); + response_length += content_length; // Send it all! int rv = send(fd, response, response_length, 0); - if (rv < 0) { + if (rv < 0) + { perror("send"); } + // rv = send(fd, body, content_length, 0); + + // if (rv < 0) { + // perror("send"); + // } + return rv; } - /** * Send a /d20 endpoint response */ void get_d20(int fd) { // Generate a random number between 1 and 20 inclusive - + (void)fd; /////////////////// // IMPLEMENT ME! // /////////////////// @@ -94,14 +108,15 @@ void get_d20(int fd) void resp_404(int fd) { char filepath[4096]; - struct file_data *filedata; + 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) { + if (filedata == NULL) + { // TODO: make this non-fatal fprintf(stderr, "cannot find system 404 file\n"); exit(3); @@ -122,6 +137,47 @@ void get_file(int fd, struct cache *cache, char *request_path) /////////////////// // IMPLEMENT ME! // /////////////////// + // char filepath[4096]; + // struct file_data *filedata; + // char *mime_type; + /////////////////// + // IMPLEMENT ME! // + /////////////////// + + struct cache_entry *cache_entry = cache_get(cache, request_path); + + if (cache_entry != NULL) + { + send_response(fd, "HTTP/1.1 200 OK", cache_entry->content_type, cache_entry->content, cache_entry->content_length); + } + else + { + char filepath[4096]; + struct file_data *filedata; + char *mime_type; + + if (strcmp(request_path, "/") == 0 || strcmp(request_path, "/index.html") == 0) + { + snprintf(filepath, sizeof filepath, "%s/index.html", SERVER_ROOT); + } + else + { + snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path); + } + + filedata = file_load(filepath); + + if (filedata == NULL) + { + resp_404(fd); + return; + } + + mime_type = mime_type_get(filepath); + cache_put(cache, request_path, mime_type, filedata->data, filedata->size); + send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size); + file_free(filedata); + } } /** @@ -135,6 +191,8 @@ char *find_start_of_body(char *header) /////////////////// // IMPLEMENT ME! // (Stretch) /////////////////// + (void)header; + return NULL; } /** @@ -144,28 +202,42 @@ 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); - if (bytes_recvd < 0) { + if (bytes_recvd < 0) + { perror("recv"); 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(path, "/jonathan") == 0) + { + send_response(fd, "HTTP/1.1 200 OK", "text/plain", "Jonathan!", 9); + } + else + { + get_file(fd, NULL, path); + } // (Stretch) If POST, handle the post request } @@ -174,7 +246,7 @@ void handle_http_request(int fd, struct cache *cache) */ int main(void) { - int newfd; // listen on sock_fd, new connection on newfd + int newfd; // listen on sock_fd, new connection on newfd struct sockaddr_storage their_addr; // connector's address information char s[INET6_ADDRSTRLEN]; @@ -183,7 +255,8 @@ int main(void) // Get a listening socket int listenfd = get_listener_socket(PORT); - if (listenfd < 0) { + if (listenfd < 0) + { fprintf(stderr, "webserver: fatal error getting listening socket\n"); exit(1); } @@ -193,24 +266,26 @@ int main(void) // This is the main loop that accepts incoming connections and // forks a handler process to take care of it. The main parent // process then goes back to waiting for new connections. - - while(1) { + + while (1) + { socklen_t sin_size = sizeof their_addr; // Parent process will block on the accept() call until someone // makes a new connection: newfd = accept(listenfd, (struct sockaddr *)&their_addr, &sin_size); - if (newfd == -1) { + if (newfd == -1) + { perror("accept"); continue; } // Print out a message that we got the connection inet_ntop(their_addr.ss_family, - get_in_addr((struct sockaddr *)&their_addr), - s, sizeof s); + get_in_addr((struct sockaddr *)&their_addr), + s, sizeof s); printf("server: got connection from %s\n", s); - + // newfd is a new socket descriptor for the new connection. // listenfd is still listening for new connections. @@ -222,5 +297,4 @@ int main(void) // Unreachable code return 0; -} - +} \ No newline at end of file