|
|
|
@@ -1,11 +1,48 @@
|
|
|
|
|
/*
|
|
|
|
|
* Design Doc:
|
|
|
|
|
* ##############
|
|
|
|
|
* # Design Doc #
|
|
|
|
|
* ##############
|
|
|
|
|
* The server is broken into the following files:
|
|
|
|
|
* - main.c: Argument parsing and high-level control
|
|
|
|
|
* - http.c: HTTP request parsing and response formatting
|
|
|
|
|
* - server.c: Abstraction over sockets
|
|
|
|
|
* - threadpool.c: Thread pool and woker queue implementation
|
|
|
|
|
* - util.c: Misc. utility functions
|
|
|
|
|
*
|
|
|
|
|
* Parallelization:
|
|
|
|
|
* I use a thread pool (implemented in threadpool.{h,c}) for parallelization. It
|
|
|
|
|
* uses a job queue that allows threads to push tasks (functions and a datum) to
|
|
|
|
|
* the queue and then distributes these tasks out to threads. It also makes sure
|
|
|
|
|
* to take a cleanup function in case the queue is destroyed before a given task
|
|
|
|
|
* is executed. Also, all the worker threads have a signal mask installed that
|
|
|
|
|
* prevents them frow receiving any signals (except KILL, CONT, and STOP, of
|
|
|
|
|
* course), so none of the tasks need to worry about such things.
|
|
|
|
|
*
|
|
|
|
|
* Abstraction:
|
|
|
|
|
* All multithreading stuff is contained in threadpool.{h,c}. Other files don't
|
|
|
|
|
* need to worry about what thread they are running on, they just need to ensure
|
|
|
|
|
* their functions are multithread safe.
|
|
|
|
|
*
|
|
|
|
|
* All HTTP stuff is handled in http.{h,c}. This includes both parsing requests
|
|
|
|
|
* and formatting responses. None of the other files do any major parsing, they
|
|
|
|
|
* just call into http.c and its `HTTPRequest` and `HTTPResponse` objects for
|
|
|
|
|
* that.
|
|
|
|
|
*
|
|
|
|
|
* Socket stuff is handled in server.{h,c}. The `Server` object contained in
|
|
|
|
|
* this file is a thin wrapper around the POSIX sockets API.
|
|
|
|
|
*
|
|
|
|
|
* util.{h,c} has some wrappers around the `malloc` family of functions that
|
|
|
|
|
* ensure that the application crashes "cleanly" if it is out of memory. As some
|
|
|
|
|
* functions want to catch these kinds of errors, they aren't used everywhere.
|
|
|
|
|
*
|
|
|
|
|
* Argument parsing and the request-to-task flow is handled in main.c. This is
|
|
|
|
|
* also where signal handling stuff is set up to ensure clean shutdown.
|
|
|
|
|
*
|
|
|
|
|
* Testing:
|
|
|
|
|
* I tested the server using cURL and netcat. I used cURL to create various
|
|
|
|
|
* kinds of syntactically valid requests. I used netcat (along with a text
|
|
|
|
|
* editor) to create and send syntactically invalid requests (e.g. `nc
|
|
|
|
|
* 127.0.0.1:8080 <test`).
|
|
|
|
|
*/
|
|
|
|
|
#include "http.h"
|
|
|
|
|
#include "server.h"
|
|
|
|
|