Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
120 changes: 65 additions & 55 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,71 @@
*/
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
// (void)path;
// (void)content_type;
// (void)content;
// (void)content_length
///////////////////
}

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

/**
* 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 +82,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 +100,27 @@ struct cache_entry *dllist_remove_tail(struct cache *cache)
*/
struct cache *cache_create(int max_size, int hashsize)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
}

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 +132,17 @@ 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! //
///////////////////
}

/**
* Retrieve an entry from the cache
*/
struct cache_entry *cache_get(struct cache *cache, char *path)
{
///////////////////
// IMPLEMENT ME! //
///////////////////
///////////////////
// IMPLEMENT ME! //
///////////////////
}
Loading