This project is a simple HTTP/1.1 server written in C++. The goal is to implement a fully-functional HTTP server that can handle multiple client connections, parse HTTP requests, and serve responses accordingly.
- C++ Compiler (g++ or clang++)
- CMake (for building the project)
- VCPKG (for dependency management, if needed)
-
Clone the repository:
git clone https://github.com/frustratedProton/http-server cd http-server
-
Build the project using the provided
runner.sh
script. This will set up everything locally for you:./runner.sh
This script will:
- Run
cmake
to configure the project. - Build the project using
cmake --build
. - Ensure that the necessary dependencies are correctly managed.
- Run
-
Once built, run the server locally (optionally specifying a files directory):
./runner.sh --directory /absolute/path/to/files
-
The server listens on port 8080 by default.
Responds with the text following /echo/
in the path. If the request includes Accept-Encoding: gzip
, the response will be gzip-compressed.
Example (plain text):
curl http://localhost:8080/echo/hello
Response:
hello
Example (gzip):
curl -v -H "Accept-Encoding: gzip" http://localhost:8080/echo/hello --output -
Response headers:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Encoding: gzip
Content-Length: 23
Use --compressed
to see decompressed output:
curl --compressed http://localhost:8080/echo/hello
Output:
hello
Responds with the value of the User-Agent
header sent by the client.
Example:
curl -i http://localhost:8080/user-agent
Returns the contents of the file {filename}
located in the specified directory.
Example:
curl http://localhost:8080/files/example.txt
Creates or overwrites a file named {filename}
in the files directory with the request body.
-
The request must include:
Content-Type: application/octet-stream
Content-Length
header specifying the body size.- The request body containing the file contents.
Example:
curl -v -X POST http://localhost:8080/files/myfile.txt \
-H "Content-Type: application/octet-stream" \
--data "This is the file content."
Response:
HTTP/1.1 201 Created
The file will be saved as /absolute/path/to/files/myfile.txt
.