A lightweight, multithreaded HTTP/1.1 web server built from scratch in C++ using POSIX sockets.
TitanHTTP is a custom HTTP server implemented in C++ to explore how web servers work at the TCP and HTTP layers.
The server accepts TCP connections, parses incoming HTTP requests, validates them, serves static files, constructs HTTP responses, and sends them reliably to clients.
It uses a fixed-size thread pool to handle multiple client connections concurrently.
git clone https://github.com/shineyrbl-byte/TitanHTTP.git cd TitanHTTP
TitanHTTP can be compiled using Clang: clang++ src/main.cpp ThreadPool.cpp HTTPRequest.cpp HTTPResponse.cpp Logger.cpp -o server
./server The server starts listening on port 8080.
Visit: http://localhost:8080 You can also test the server using tools such as curl and netcat.
- C++17 or later
- Clang or another C++17-compatible compiler
- POSIX-compatible operating system
-
TCP socket-based HTTP server using POSIX sockets
-
HTTP/1.1 request parsing
-
HTTP request header parsing
-
GETrequest handling -
Static file serving
-
MIME type detection
-
Content-Lengthresponse header -
Content-Typeresponse header -
Connection: closeresponse handling -
Reliable transmission using a
sendAll()loop -
Request size limit to prevent excessively large requests
-
HTTP error responses:
400 Bad Request404 Not Found405 Method Not Allowed505 HTTP Version Not Supported
-
Fixed-size thread pool for concurrent clients
-
Thread-safe request logging
-
Support for HTML, CSS, JavaScript, PNG, JPEG, GIF, SVG and text files
Client
|
| TCP connection
v
+-------------+
| Server |
| accept() |
+-------------+
|
v
+-------------+
| Thread Pool |
+-------------+
|
+--------+--------+
| | |
v v v
Worker Worker Worker
|
v
HTTP Request
|
v
Request Parser
|
v
Validation
|
v
Static File
Lookup
|
+-----+-----+
| |
404 File
|
v
HTTP Response
|
v
sendAll()
|
v
Client
A typical request follows this flow:
Client
|
v
TCP connection
|
v
recv()
|
v
HTTP request parser
|
+--> Validate request
|
+--> Check Host header
|
+--> Check HTTP method
|
+--> Check HTTP version
|
v
Map URL to file
|
v
Read static file
|
v
Build HTTP response
|
v
sendAll()
|
v
Close connection
TitanHTTP uses a fixed number of worker threads rather than creating a new thread for every connection.
Incoming client sockets are added to a shared queue. Worker threads wait for connections and process them when available.
Client connections
|
v
Shared Queue
/ | \
/ | \
v v v
Worker 1 Worker 2 Worker 3
\ | /
\ | /
HTTP request
This allows multiple clients to be processed concurrently while avoiding the overhead of continuously creating and destroying threads.
The server was also tested with a deliberately stalled TCP client using netcat while browser requests were being processed. Other worker threads continued serving requests, demonstrating the benefit of the thread-pool architecture.
TitanHTTP/
├── src/
│ └── main.cpp
│
├── public/
│ ├── index.html
│ ├── about.html
│ ├── style.css
│ └── script.js
│
├── ThreadPool.h
├── ThreadPool.cpp
├── HTTPRequest.h
├── HTTPRequest.cpp
├── HTTPResponse.h
├── HTTPResponse.cpp
├── Logger.h
├── Logger.cpp
├── .gitignore
└── README.md
main.cpp
- Creates and configures the TCP server socket
- Accepts client connections
- Receives HTTP requests
- Coordinates request processing
ThreadPool
- Maintains worker threads
- Stores incoming client connections in a queue
- Synchronizes workers using mutexes and condition variables
HTTPRequest
- Parses the HTTP request line
- Extracts method, path and HTTP version
- Parses request headers
HTTPResponse
- Builds valid HTTP responses
- Calculates
Content-Length - Sets appropriate
Content-Type - Adds connection handling headers
Logger
- Records incoming HTTP requests
- Uses synchronization to prevent concurrent log output from interfering with each other
TitanHTTP can be compiled using Clang on macOS:
clang++ src/main.cpp ThreadPool.cpp HTTPRequest.cpp HTTPResponse.cpp Logger.cpp -o serverStart the server with:
./serverThe server listens on:
http://localhost:8080
You can then open the URL in a web browser.
GET / HTTP/1.1
Host: localhost:8080
GET /about HTTP/1.1
Host: localhost:8080
Static assets such as CSS and JavaScript files can also be served from the public directory.
TitanHTTP handles several invalid or unsupported requests.
| Situation | Response |
|---|---|
| Malformed HTTP request | 400 Bad Request |
Missing Host header |
400 Bad Request |
| Requested file does not exist | 404 Not Found |
| Unsupported request method | 405 Method Not Allowed |
| Unsupported HTTP version | 505 HTTP Version Not Supported |
| Request exceeds 8192 bytes | Connection closed |
The server was tested using browser requests, curl, and netcat.
Tests included:
- Normal
GETrequests - Static HTML, CSS and JavaScript files
- Missing files
- Unsupported HTTP methods
- Malformed HTTP requests
- Oversized requests
- Large HTTP responses
- Multiple simultaneous clients
- A deliberately stalled TCP client
- Thread-pool concurrency
- C++
- POSIX sockets
- TCP/IP
- HTTP/1.1
- C++ threads
- Mutexes
- Condition variables
- Clang
- macOS
Possible future improvements include:
- Persistent HTTP connections
- Additional HTTP methods
- More complete HTTP header handling
- Socket timeouts
- Improved security validation
- Configurable server ports
- More comprehensive automated tests
- Graceful server shutdown
- Request and response metrics