Skip to content

Kevin Sooter - C-Web-Server #300

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ In this project, we'll finish the implementation of a web server in C.

What you need to write:

* HTTP request parser
* HTTP response builder
* LRU cache
* Doubly linked list (some functionality provided)
* Use existing hashtable functionality (below)
- HTTP request parser
- HTTP response builder
- LRU cache

* Your code will interface with the existing code. Understanding the existing
- Doubly linked list (some functionality provided)
- Use existing hashtable functionality (below)

- Your code will interface with the existing code. Understanding the existing
code is an expected part of this challenge.

What's already here:

* `net.h` and `net.c` contain low-level networking code
* `mime.h` and `mime.c` contains functionality for determining the MIME type of a file
* `file.h` and `file.c` contains handy file-reading code that you may want to utilize, namely the `file_load()` and `file_free()` functions for reading file data and deallocating file data, respectively (or you could just perform these operations manually as well)
* `hashtable.h` and `hashtable.c` contain an implementation of a hashtable (this one is a bit more complicated than what you built in the Hashtables sprint)
* `llist.h` and `llist.c` contain an implementation of a doubly-linked list (used solely by the hashable--you don't need it)
* `cache.h` and `cache.c` are where you will implement the LRU cache functionality for days 3 and 4
- `net.h` and `net.c` contain low-level networking code
- `mime.h` and `mime.c` contains functionality for determining the MIME type of a file
- `file.h` and `file.c` contains handy file-reading code that you may want to utilize, namely the `file_load()` and `file_free()` functions for reading file data and deallocating file data, respectively (or you could just perform these operations manually as well)
- `hashtable.h` and `hashtable.c` contain an implementation of a hashtable (this one is a bit more complicated than what you built in the Hashtables sprint)
- `llist.h` and `llist.c` contain an implementation of a doubly-linked list (used solely by the hashable--you don't need it)
- `cache.h` and `cache.c` are where you will implement the LRU cache functionality for days 3 and 4

## What is a Web Server?

Expand All @@ -29,17 +30,17 @@ requests for HTML pages), and returns responses (e.g. HTML pages). Other common

## Reading

* [Networking Background](guides/net.md)
* [Doubly-Linked Lists](guides/dllist.md)
* [LRU Caches](guides/lrucache.md)
* [MIME types](guides/mime.md)
- [Networking Background](guides/net.md)
- [Doubly-Linked Lists](guides/dllist.md)
- [LRU Caches](guides/lrucache.md)
- [MIME types](guides/mime.md)

## Assignment

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:
- `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:

```
http://localhost:3490/index.html
Expand Down Expand Up @@ -72,7 +73,7 @@ _Read through all the main and stretch goals before writing any code to get an o
1. Implement `send_response()`.

This function is responsible for formatting all the pieces that make up an HTTP response into the proper format that clients expect. In other words, it needs to build a complete HTTP response with the given parameters. It should write the response to the string in the `response` variable.

The total length of the header **and** body should be stored in the `response_length` variable so that the `send()` call knows how many bytes to
send out over the wire.

Expand All @@ -86,7 +87,7 @@ _Read through all the main and stretch goals before writing any code to get an o
> the header. But the `response_length` variable used by `send()` is the
> total length of both header and body.

You can test whether you've gotten `send_response` working by calling the `resp_404` function from somewhere inside the `main` function, and seeing if the client receives the 404 response.
You can test whether you've gotten `send_response` working by calling the `resp_404` function from somewhere inside the `main` function, and seeing if the client receives the 404 response.

2. Examine `handle_http_request()` in the file `server.c`.

Expand Down Expand Up @@ -153,24 +154,24 @@ The hashtable code is already written and can be found in `hashtable.c`.

Algorithm:

* Allocate a new cache entry with the passed parameters.
* Insert the entry at the head of the doubly-linked list.
* Store the entry in the hashtable as well, indexed by the entry's `path`.
* Increment the current size of the cache.
* If the cache size is greater than the max size:
* Remove the cache entry at the tail of the linked list.
* Remove that same entry from the hashtable, using the entry's `path` and the `hashtable_delete` function.
* Free the cache entry.
* Ensure the size counter for the number of entries in the cache is correct.
- Allocate a new cache entry with the passed parameters.
- Insert the entry at the head of the doubly-linked list.
- Store the entry in the hashtable as well, indexed by the entry's `path`.
- Increment the current size of the cache.
- If the cache size is greater than the max size:
- Remove the cache entry at the tail of the linked list.
- Remove that same entry from the hashtable, using the entry's `path` and the `hashtable_delete` function.
- Free the cache entry.
- Ensure the size counter for the number of entries in the cache is correct.

2. Implement `cache_get()` in `cache.c`.

Algorithm:

* Attempt to find the cache entry pointer by `path` in the hash table.
* If not found, return `NULL`.
* Move the cache entry to the head of the doubly-linked list.
* Return the cache entry pointer.
- Attempt to find the cache entry pointer by `path` in the hash table.
- If not found, return `NULL`.
- Move the cache entry to the head of the doubly-linked list.
- Return the cache entry pointer.

3. Add caching functionality to `server.c`.

Expand All @@ -181,11 +182,11 @@ The hashtable code is already written and can be found in `hashtable.c`.

If it's not there:

* Load the file from disk (see `file.c`)
* Store it in the cache
* Serve it
- Load the file from disk (see `file.c`)
- Store it in the cache
- Serve it

There's a set of unit tests included to ensure that your cache implementation is functioning correctly. From the `src` directory, run `make tests` in order to run the unit tests against your implementation.
There's a set of unit tests included to ensure that your cache implementation is functioning correctly. From the `src` directory, run `make tests` in order to run the unit tests against your implementation.

### Stretch Goals

Expand Down Expand Up @@ -247,4 +248,3 @@ When a new connection comes in, launch a thread to handle it.
Be sure to lock the cache when a thread accesses it so the threads don't step on each other's toes and corrupt the cache.

Also have thread cleanup handlers to handle threads that have died.

86 changes: 59 additions & 27 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,56 @@ 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];
int response_length;

time_t rawtime;
struct tm *timestamp;
char buffer[100];

time(&rawtime);

timestamp = localtime(&rawtime);
strftime(buffer, 100, "%a %b %d %T %Z %Y", timestamp);
// Build HTTP response and store it in response

///////////////////
// IMPLEMENT ME! //
///////////////////
char *newBody = body;

sprintf(response, "%s\n"
"Date: %s\n"
"Connection: close\n"
"Content-Length: %d\n"
"Content-Type: %s\n"
"\n"
"%s\n",
header, buffer, content_length, content_type, newBody);

response_length = strlen(response);

// 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

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

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

///////////////////
// IMPLEMENT ME! //
///////////////////
Expand All @@ -94,21 +113,21 @@ 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);
}

mime_type = mime_type_get(filepath);

send_response(fd, "HTTP/1.1 404 NOT FOUND", mime_type, filedata->data, filedata->size);

file_free(filedata);
Expand Down Expand Up @@ -148,23 +167,36 @@ 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! //
///////////////////
char *method[50];
char *path[200];
char *HTTP[50];

// Read the three components of the first request line

sscanf(request, "%s %s %s", method, path, HTTP);
// If GET, handle the get endpoints
if (strcmp(method, "GET") == 0)
{

// Check if it's /d20 and handle that special case
// Otherwise serve the requested file by calling get_file()

// Check if it's /d20 and handle that special case
if (strncmp(path, "/d20", 4) == 0)
{
get_d20(fd);
}
else
{
resp_404(fd);
}
// Otherwise serve the requested file by calling get_file()
}

// (Stretch) If POST, handle the post request
}
Expand All @@ -174,16 +206,16 @@ 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];

struct cache *cache = cache_create(10, 0);

// 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,24 +225,25 @@ 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) {
socklen_t sin_size = sizeof their_addr;
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.

Expand All @@ -223,4 +256,3 @@ int main(void)

return 0;
}