A lightweight HTTP server implemented in C++ supporting GET and POST requests, file handling, echo functionality, and gzip compression. Built for learning and experimentation with socket programming and HTTP protocols.
g++ -std=c++17 -pthread -lz -o server server.cpp
./server [--directory /path/to/base]
- Supports GET and POST methods.
- Parses HTTP request headers and body.
- Supports standard HTTP headers including
Content-Length,Accept-Encoding, andConnection.
/: Returns a simple200 OKresponse./echo/<message>: Returns the message part of the path as the response body./user-agent: Returns the value of theUser-Agentheader./files/<filename>:- GET: Reads and returns file content from the server directory.
- POST: Writes request body to a file in the server directory.
- Supports reading and writing files in the server’s base directory.
- Uses binary-safe I/O for file content.
- Returns
404 Not Foundif a file is missing.
- Automatically compresses response body if client includes
Accept-Encoding: gzip. - Adds
Content-Encoding: gzipheader for compressed responses. - Uses zlib for GZIP compression.
- Handles multiple clients concurrently using threads.
- Supports
Connection: closeheader to properly close client connections. - Safely handles client disconnects and socket errors.
- Default base directory is the current directory (
.). - Can be overridden using command line argument:
./server --directory /path/to/base