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
71 changes: 70 additions & 1 deletion src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,39 @@
#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)
{
///////////////////
// 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;
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -125,6 +163,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--;
}
}
}

/**
Expand All @@ -135,4 +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;



}
69 changes: 60 additions & 9 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@ 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 rv = send(fd, response, response_length, 0);

Expand All @@ -76,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! //
///////////////////
Expand Down Expand Up @@ -122,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);
}

/**
Expand All @@ -135,6 +168,8 @@ char *find_start_of_body(char *header)
///////////////////
// IMPLEMENT ME! // (Stretch)
///////////////////
(void) header;
return NULL;
}

/**
Expand All @@ -144,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);
Expand All @@ -153,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
}

Expand Down