Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C-Web-Server - Pedro Montesinos #304

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ requests for HTML pages), and returns responses (e.g. HTML pages). Other common
We will write a simple web server that returns files and some specialized data on a certain endpoint.

* `http://localhost:3490/d20` should return a random number between 1 and 20 inclusive as `text/plain` data.
* Any other URL should map to the `serverroot` directory and files that lie within. For example:
* Any other URL should map to the `serverroot` directory and files that lie within. For example:

```
http://localhost:3490/index.html
Expand Down
40 changes: 40 additions & 0 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ struct cache_entry *alloc_entry(char *path, char *content_type, void *content, i
///////////////////
// IMPLEMENT ME! //
///////////////////

struct cache_entry *entry = malloc(sizeof(struct cache_entry));

entry->path = path;
entry->content_type = content_type;
entry->content = content;
entry->content_length = content_length;
entry->next = NULL;
entry->prev = NULL;

return entry;
}

/**
Expand All @@ -22,6 +33,7 @@ void free_entry(struct cache_entry *entry)
///////////////////
// IMPLEMENT ME! //
///////////////////
free(entry);
}

/**
Expand Down Expand Up @@ -94,6 +106,14 @@ struct cache *cache_create(int max_size, int hashsize)
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache *newcache = malloc(sizeof(struct cache));
newcache->index = hashtable_create(hashsize, NULL);
newcache->head = NULL;
newcache->tail = NULL;
newcache->max_size = max_size;
newcache->cur_size = 0;

return newcache;
}

void cache_free(struct cache *cache)
Expand Down Expand Up @@ -125,6 +145,20 @@ void cache_put(struct cache *cache, char *path, char *content_type, void *conten
///////////////////
// IMPLEMENT ME! //
///////////////////

struct cache_entry *entry = alloc_entry(path, content_type, content, content_length);
dllist_insert_head(cache, entry);
hashtable_put(cache->index, path, entry);
cache->cur_size++;
if(cache->cur_size > cache->max_size){
struct cache_entry *tail = dllist_remove_tail(cache);
hashtable_delete(cache->index, tail->path);
free_entry(tail);
if(cache->cur_size > cache->max_size){
cache->cur_size--;
}

}
}

/**
Expand All @@ -135,4 +169,10 @@ struct cache_entry *cache_get(struct cache *cache, char *path)
///////////////////
// IMPLEMENT ME! //
///////////////////
struct cache_entry *pointer = hashtable_get(cache->index, path);
if(pointer == NULL){
return NULL;
}
dllist_move_to_head(cache, pointer);
return pointer;
}
106 changes: 82 additions & 24 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -52,37 +52,53 @@ 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];

time_t t = time(NULL);
struct tm *tm = localtime(&t);
// Build HTTP response and store it in response

///////////////////
// IMPLEMENT ME! //
///////////////////
int response_length = sprintf(response,
"%s\n"
"Content-Type: %s\n"
"Content-Length: %d\n"
"Connection: close\n"
"\n"
"%s",
header,
// asctime(tm),
content_type,
content_length,
body
);

// Send it all!
int rv = send(fd, response, response_length, 0);

if (rv < 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

int random_number = rand() % (1 + 21);
char num[50];
sprintf(num, "%i", random_number);
///////////////////
// IMPLEMENT ME! //
///////////////////

// Use send_response() to send it back as text/plain data

send_response(fd, "HTTP 200 OK", "text/plain", num, 1);
///////////////////
// IMPLEMENT ME! //
///////////////////
Expand All @@ -94,17 +110,18 @@ 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);
// fprintf(stderr, "cannot find system 404 file\n");
resp_404(fd);
}

mime_type = mime_type_get(filepath);
Expand All @@ -122,6 +139,24 @@ void get_file(int fd, struct cache *cache, char *request_path)
///////////////////
// IMPLEMENT ME! //
///////////////////
char filepath[4096];
struct file_data *filedata;
char *mime_type;

snprintf(filepath, sizeof filepath, "%s%s", SERVER_ROOT, request_path);
filedata = file_load(filepath);

if (filedata == NULL){
resp_404(fd);
}

mime_type = mime_type_get(filepath);

send_response(fd, "HTTP 200 OK", mime_type, filedata->data, filedata->size);
struct cache_entry *pointer = cache_get(cache, request_path);
if(pointer != NULL){
send_response(fd, "HTTP 200 OK", pointer->content_type, pointer->content, pointer->content_length);
}
}

/**
Expand All @@ -132,7 +167,7 @@ void get_file(int fd, struct cache *cache, char *request_path)
*/
char *find_start_of_body(char *header)
{
///////////////////
///////////////////
// IMPLEMENT ME! // (Stretch)
///////////////////
}
Expand All @@ -148,24 +183,44 @@ void handle_http_request(int fd, struct cache *cache)
// Read request
int bytes_recvd = recv(fd, request, request_buffer_size - 1, 0);

if (bytes_recvd < 0) {
if (bytes_recvd < 0)
{
perror("recv");
return;
}


///////////////////
// IMPLEMENT ME! //
///////////////////

// Read the three components of the first request line
char request_type[8];
char request_endpoint[1024];
char request_protocol[128];

// If GET, handle the get endpoints
sscanf(request, "%s %s %s", request_type, request_endpoint, request_protocol);

fprintf(stderr, "%s %s %s\n", request_type, request_endpoint, request_protocol);

// If GET, handle the get endpoints
if (strcmp(request_type, "GET") == 0)
{
if (strcmp(request_endpoint, "/d20") == 0)
{
get_d20(fd);
}
else
{
get_file(fd, cache, request_endpoint);
}
}
else
{
resp_404(fd);
}
// Check if it's /d20 and handle that special case
// Otherwise serve the requested file by calling get_file()


// (Stretch) If POST, handle the post request
}

Expand All @@ -174,7 +229,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];

Expand All @@ -183,7 +238,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);
}
Expand All @@ -193,34 +249,36 @@ 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.

handle_http_request(newfd, cache);

// resp_404(newfd);
close(newfd);
}

// Unreachable code

return 0;
}

File renamed without changes