Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# A Simple Web Server in C


In this project, we'll finish the implementation of a web server in C.

What you need to write:
Expand Down
171 changes: 115 additions & 56 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,78 @@
*/
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *ce = malloc(sizeof(struct cache_entry));
ce->path = path;
ce->content_length = content_length;
ce->content_type = content_type;
ce->content = content;
return ce;
}

/**
* Deallocate a cache entry
*/
void free_entry(struct cache_entry *entry)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
if (entry != NULL)
{

free(entry);
}
}

/**
* Insert a cache entry at the head of the linked list
*/
void dllist_insert_head(struct cache *cache, struct cache_entry *ce)
{
// Insert at the head of the list
if (cache->head == NULL) {
cache->head = cache->tail = ce;
ce->prev = ce->next = NULL;
} else {
cache->head->prev = ce;
ce->next = cache->head;
ce->prev = NULL;
cache->head = ce;
}
// Insert at the head of the list
if (cache->head == NULL)
{
cache->head = cache->tail = ce;
ce->prev = ce->next = NULL;
}
else
{
cache->head->prev = ce;
ce->next = cache->head;
ce->prev = NULL;
cache->head = ce;
}
}

/**
* Move a cache entry to the head of the list
*/
void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
{
if (ce != cache->head) {
if (ce == cache->tail) {
// We're the tail
cache->tail = ce->prev;
cache->tail->next = NULL;

} else {
// We're neither the head nor the tail
ce->prev->next = ce->next;
ce->next->prev = ce->prev;
}

ce->next = cache->head;
cache->head->prev = ce;
ce->prev = NULL;
cache->head = ce;
if (ce != cache->head)
{
if (ce == cache->tail)
{
// We're the tail
cache->tail = ce->prev;
cache->tail->next = NULL;
}
else
{
// We're neither the head nor the tail
ce->prev->next = ce->next;
ce->next->prev = ce->prev;
}
}

ce->next = cache->head;
cache->head->prev = ce;
ce->prev = NULL;
cache->head = ce;
}
}

/**
* Removes the tail from the list and returns it
Expand All @@ -73,14 +89,14 @@ void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
*/
struct cache_entry *dllist_remove_tail(struct cache *cache)
{
struct cache_entry *oldtail = cache->tail;
struct cache_entry *oldtail = cache->tail;

cache->tail = oldtail->prev;
cache->tail->next = NULL;
cache->tail = oldtail->prev;
cache->tail->next = NULL;

cache->cur_size--;
cache->cur_size--;

return oldtail;
return oldtail;
}

/**
Expand All @@ -91,26 +107,35 @@ struct cache_entry *dllist_remove_tail(struct cache *cache)
*/
struct cache *cache_create(int max_size, int hashsize)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
struct hashtable *ht = hashtable_create(hashsize, NULL);
struct cache *cache_inst = malloc(sizeof(struct cache));
cache_inst->head = NULL;
cache_inst->tail = NULL;
cache_inst->cur_size = 0;
cache_inst->max_size = max_size;
cache_inst->index = ht;
return cache_inst;
}

void cache_free(struct cache *cache)
{
struct cache_entry *cur_entry = cache->head;
struct cache_entry *cur_entry = cache->head;

hashtable_destroy(cache->index);
hashtable_destroy(cache->index);

while (cur_entry != NULL) {
struct cache_entry *next_entry = cur_entry->next;
while (cur_entry != NULL)
{
struct cache_entry *next_entry = cur_entry->next;

free_entry(cur_entry);
free_entry(cur_entry);

cur_entry = next_entry;
}
cur_entry = next_entry;
}

free(cache);
free(cache);
}

/**
Expand All @@ -122,17 +147,51 @@ void cache_free(struct cache *cache)
*/
void cache_put(struct cache *cache, char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *entry = alloc_entry(path, content_type, content, content_length);

dllist_insert_head(cache, entry);

hashtable_put(cache->index, entry->path, entry);

// Incremtn 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 entry from the hashtable, using the entry's path
hashtable_delete(cache->index, cache->tail->path);
// Remove the cache entry at the tail of the linked list
// Free the cache entry
free_entry(dllist_remove_tail(cache));

// Ensure the size counter for the number of entries in the cache is correct.
if (cache->cur_size > cache->max_size)
{
printf("Cache's current size is bigger than its capacity.");
}
}
}

/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
}
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *entry = hashtable_get(cache->index, path);
if (entry != NULL)
{
dllist_move_to_head(cache, entry);
}
else
{
return NULL;
}
}
Loading