A C++ web server
$ make build
$ ./webserver <config>
$ make check
The config file must specify:
-
which port the server will listen on
port <port number>;
-
each request handler
path <handler URI prefix> <handler name> { <handler-specific config statements> }
# This is a comment.
port 2020; # This is also a comment.
path / StaticHandler {
root /foo/bar;
}
path /echo EchoHandler {}
# Default response handler if no handlers match.
default NotFoundHandler {}
All header files can be found in the include/
directory. All .cc
files can be found in either the src/
directory or the config_parser/
directory.
Here are the primary classes that make up the Server:
Server
- top-level class
- takes a config filename as input
- uses the
NginxConfigParser
to parse and process the config - initializes each request handler
- creates asynchronous
Connection
s to listen to requests
Connection
- reads a single request from a socket
- invokes the matching
RequestHandler
RequestHandler
- given a request, will generate the correct response
- base class that all request handlers must inherit from
Here are the helper classes:
Request
- parses the raw HTTP request
- holds the HTTP request data
Response
- holds the HTTP response data
- generates a properly formatted HTTP response using the given data
NginxConfigParser
- parses the config file into an
NginxConfig
object
- parses the config file into an
NginxConfig
andNginxConfigStatement
- these hold the config, parsed into tokens
All request handlers must inherit from the RequestHandler
base class.
To add a new request handler:
- Create a new class that inherits from
RequestHandler
- Implement the pure virtual functions
RequestHandler::Init
andRequestHandler::HandleRequest
in the new request handler - Add the
REGISTER_REQUEST_HANDLER(<handler name>)
macro to the new request handler's header file to register it with theServer
- Add the new request handler to your config file
We provide several request handlers (EchoHandler
, StaticHandler
, NotFoundHandler
, StatusHandler
) to use as examples for implementing your own request handlers.
- Crystal Hsieh
- Jason Liu
- Ryan Voong