Repository: Jagrit3500/multi-threaded-http-server
This project is a multi-threaded HTTP server implemented from scratch using low-level socket programming. The server is designed to handle multiple concurrent clients, serve static and binary files via GET requests, process JSON data via POST requests, and enforce security measures such as path traversal protection and host validation. It supports HTTP/1.1 features like persistent connections and connection timeouts.
-
Configurable server:
- Default host:
localhost(127.0.0.1) - Default port:
8080 - Configurable thread pool size (default: 10)
- Command-line arguments:
python server.py [port] [host] [max_threads]
- Default host:
-
TCP socket communication:
- Handles client connections using TCP sockets
- Queue size for incoming connections: ≥ 50
-
Multi-threading & concurrency:
- Thread pool with configurable size
- Queues connections if all threads are busy
- Proper synchronization of shared resources
-
HTTP request handling:
- Supports
GETandPOSTmethods - Parses request method, path, version, and headers
- Returns appropriate error codes for invalid requests
- Supports
-
File serving:
- Serves
.htmlfiles for browser rendering - Serves
.png,.jpg/.jpeg,.txtfiles as binary downloads - Only these file types allowed; others return error
- Serves
-
POST uploads:
- Accepts only
application/jsonfor uploads - Saves JSON files in
resources/uploads/with timestamped filenames
- Accepts only
-
Security:
- Prevents directory traversal and access to unauthorized paths
- Validates Host header for incoming requests
-
Connection management:
- Supports keep-alive/persistent connections
- 30 second timeout for persistent connections
- Maximum 100 requests per connection
-
Comprehensive logging:
- Logs startup, request handling, thread pool status, file transfers, and security events, all with timestamps
project/
├── server.py
├── resources/
│ ├── index.html
│ ├── about.html
│ ├── contact.html
│ ├── sample.txt
│ ├── logo.png
│ ├── logo2.png
│ ├── photo.jpg
│ ├── photo2.jpg
│ └── uploads/
Prerequisites:
- Python 3.x (no external dependencies required)
Commands:
python server.py # Runs on localhost:8080, 10 threads
python server.py 8000 # Runs on localhost:8000, 10 threads
python server.py 8000 0.0.0.0 20 # Runs on 0.0.0.0:8000, 20 threadsPrepare the resources/ directory as shown above.
Recommended files:
- At least 3 HTML files (
index.html,about.html,contact.html) - At least 2 PNG images (one >1MB)
- At least 2 JPEG images
- At least 2 text files
- Sample JSON files for POST testing
Example requests:
GET /→ Servesindex.htmlGET /about.html→ Serves HTMLGET /logo.png,/photo.jpg,/sample.txt→ Downloads filePOST /upload(with JSON) → Uploads JSON file
Error handling:
GET /nonexistent.png→ 404 Not FoundPUT /index.html→ 405 Method Not AllowedPOST /upload(non-JSON) → 415 Unsupported Media Type
Security tests:
- Path traversal attempts (
/../etc/passwd,/./././../config) → 403 Forbidden - Invalid Host / missing Host → 400/403
Concurrency:
- Handles multiple simultaneous downloads
- Queues connections when thread pool is full
Example POST using curl:
curl -X POST -H "Content-Type: application/json" -d "{\"key\":\"value\"}" http://127.0.0.1:8080/upload
- Files sent in binary mode with 8KB buffer for efficient transfer
- Uses Content-Disposition header for downloads
- Strict path validation and host checking for security
- Thread pool managed with Python's
queue.Queueandthreading - All server activity logged with timestamps
- Only supports GET and POST methods
- Only serves files from
resources/ - Only
.html,.txt,.png,.jpg/.jpegfiles supported - No HTTPS/SSL, no authentication
- POST uploads limited to JSON
[2025-10-10 13:30:00] HTTP Server started on http://127.0.0.1:8080
[2025-10-10 13:30:00] Thread pool size: 10
[2025-10-10 13:30:15] [Thread-1] Connection from 127.0.0.1:54321
[2025-10-10 13:30:15] [Thread-1] Request: GET /image.png HTTP/1.1
[2025-10-10 13:30:15] [Thread-1] Sending binary file: image.png (45678 bytes)
[2025-10-10 13:35:00] Thread pool status: 8/10 active
[2025-10-10 13:35:30] Warning: Thread pool saturated, queuing connection
For questions or problems, please open an issue.