Basic HTTP server based on NGINX written in C++ with a sample webpage and CGI Scripts.
Our server manages connections using a synchronous I/O multiplexing system. Sockets are kept alive for each incoming connection and a timeout system ensures that they are closed if no activity occurs in the time given via config. Our config is rather flexible, allowing for precise location premissions and settings. CGI can be executed and configured by setting a CGI script directory. Timeout time and max body size can also be set for each server. Additionally, any amount of servers can be set up in a single config.
- Socket Management:
We use
poll()
to manage incoming connections efficiently. Servers can handle any ammout of simultaneous requests without crashing. - HTTP Methods:
Currently the implemented methods are
GET
,POST
andDELETE
. They must be set explicitly for each location in the server root through the config file.GET
sends back requested data to the client.POST
uploads data from the client to the server's upload store (also set in the config).DELETE
removes requested data from the server entirely. Obviously, these methods have safety measures. - Config Directives: There are many configuration directives that can be used to specify various features of your servers. There are always defaults as well, to fall back on any missing directives. A detailed table of these directives is shown in the following section.
- CGI Script Execution:
Our servers can handle CGI script execution which can be specified via the
cgi-path
directive. This will set the path that the server will use to identify CGI-specific requests. - Example Pages and Config: The project comes with a default configuration file and an example website with multiple HTML pages and CSS styling. We made an upload page where you can easily test POST requests and a CGI testing page where input can be sent to a sample program that will process and respond through a script as HTML.
- Verbose Logging: We also made a rebust logging and debugging system that reports useful information back to the user during runtime, such as request information and errors.
The program can be simply launched with ./webserv
and it will use the default.conf
file if available. If you wish to write your own custom config, you can run it with ./webserv [config file path]
.
The process will then run indefinately; displaying logs until it is closed by sending an interrupt signal with CTRL+c
.
There can be any amount of server scopes and location scopes inside said servers. No directive is absolutely mandatory, as there are defaults for everything that will take effect. For example, I can not define the index and it will always default to index.html
. If it does not exist, a 404 response will be sent.
Each scope is opened and closed with { }
and each directive is terminated with ;
.
Here is every available directive and scope in our configuration system:
Keyword | Type | Fields | Description |
---|---|---|---|
server | scope | server name | Defines a server to run upon execution. |
error_page | directive | status code, path to page | Sets the response page to be used for a specified status code. |
location | scope | path (relative to server root) | Defines a location in the server. Must be inside a server scope. |
listen | directive | port number | Sets the port that this server will listen to. |
host | directive | ip address | Sets the IP the server will be hosted on. |
root | directive | path | Sets the root directory of the server. |
index | directive | path/filename | Sets the default response file for a simple request to the server. |
timeout | directive | time (seconds) | Sets the maximum time the socket connection will stay open awaiting a request before timing out. |
cgi | directive | file extensions | Defines the file types which will be treated as CGI scripts by the server. |
max_body | directive | size (bytes) | Sets the maximum body size that can be handled from a POST request for a specific location. |
methods | directive | method names (space separated) | Sets the allowed methods for a specific location. |
return | directive | path aliases | Adds aliases that will redirect to the location the return directive is in. |
Example config:
server {
listen 8080;
host 127.0.0.1;
root www/;
index index.html;
timeout 10;
location / {
methods GET;
}
location scripts/ {
index ../pages/scripts.html;
methods GET POST;
cgi .sh .py .php;
return cgi-bin/;
}
location uploads/ {
index ../pages/upload.html;
methods GET POST DELETE;
max_body 8000000;
}
}