A complete HTTP server implementation built from scratch using Python socket programming, demonstrating multi-threading, HTTP/1.1 protocol support, binary file transfers, and comprehensive security features.
- Multi-threaded Architecture: Configurable thread pool for concurrent request handling
- HTTP/1.1 Protocol Support: Full implementation with persistent connections and keep-alive
- Binary File Transfer: Efficient handling of images, text files, and other binary content
- JSON API Endpoint: POST endpoint for uploading and storing JSON data
- Static File Serving: HTML files served for browser viewing
- Path Traversal Protection: Prevents directory traversal attacks (
../,./, etc.) - Host Header Validation: Validates Host header to prevent injection attacks
- Input Validation: Comprehensive request format and content type validation
- Error Handling: Proper HTTP error responses with appropriate status codes
- Connection Management: Persistent connections with configurable timeouts
- Request Queue: Handles connection queuing when thread pool is saturated
- Resource Limits: Configurable request size limits and connection timeouts
- Comprehensive Logging: Detailed logging for monitoring and debugging
project/
├── server.py # Main HTTP server implementation
├── request.py # HTTP request parser
├── response.py # HTTP response formatter
├── methods.py # HTTP method handlers (GET, POST)
├── utils.py # Utility functions and security helpers
├── exceptions.py # Custom exception classes
├── README.md # This documentation
└── resources/ # Static files directory
├── index.html # Homepage
├── about.html # About page
├── contact.html # Contact/API documentation
├── sample.txt # Sample text file for download testing
├── sample1.txt # Additional sample file
├── image.png # Sample PNG image
├── http.png # Another sample image
└── uploads/ # Directory for uploaded JSON files
- Python 3.6 or higher
- No external dependencies required (uses only Python standard library)
-
Clone the repository (or download the files):
git clone <repository-url> cd http-server-from-scratch
-
Run the server with default settings:
python server.py
-
Access the server: Open your browser and navigate to
http://localhost:8080
The server accepts optional command-line arguments:
python server.py [port] [host] [max_workers]Examples:
# Run on default settings (port 8080, localhost, 10 workers)
python server.py
# Custom port
python server.py 8000
# Custom port and host
python server.py 8000 0.0.0.0
# Custom port, host, and thread pool size
python server.py 8000 0.0.0.0 20Parameters:
port(default: 8080): Server port numberhost(default: 127.0.0.1): Server bind addressmax_workers(default: 10): Maximum thread pool size
- Homepage:
http://localhost:8080/orhttp://localhost:8080/index.html - About Page:
http://localhost:8080/about.html - Contact/API Docs:
http://localhost:8080/contact.html - File Downloads:
http://localhost:8080/sample.txt(automatically downloads)
# Upload JSON data
curl -X POST http://localhost:8080/upload \
-H "Content-Type: application/json" \
-H "Host: localhost:8080" \
-d '{
"message": "Hello Server!",
"timestamp": "2024-03-15T10:30:00Z",
"user": "test_user"
}'
# Expected Response:
# {
# "status": "success",
# "message": "File created successfully",
# "filepath": "/uploads/upload_1710504600_a7b9.json"
# }# Download text file
curl -O http://localhost:8080/sample.txt
# Download image
curl -O http://localhost:8080/image.png
# View HTML content
curl http://localhost:8080/about.html# Test 5 simultaneous connections
for i in {1..5}; do
curl http://localhost:8080/ &
done
wait- Connection Accept: Main thread accepts incoming TCP connections
- Thread Assignment: Connection assigned to available worker thread from pool
- Request Parsing: HTTP request parsed into method, path, headers, and body
- Security Validation: Path traversal and Host header validation
- Method Routing: Request routed to appropriate handler (GET/POST/OTHER)
- Response Generation: HTTP response created with proper headers and content
- Connection Management: Connection kept alive or closed based on headers
Main Thread (Accept Loop)
├── Thread Pool (Configurable Size)
│ ├── Worker Thread 1 ─── Handle Connection 1
│ ├── Worker Thread 2 ─── Handle Connection 2
│ ├── ...
│ └── Worker Thread N ─── Handle Connection N
└── Connection Queue (When Pool Saturated)
- Input Validation: All requests validated before processing
- Path Sanitization: Prevents access outside resources directory
- Host Header Validation: Prevents host header injection attacks
- Error Handling: Secure error responses without information leakage
- Resource Limits: Prevents resource exhaustion attacks
# Blocked paths:
GET /../etc/passwd → 403 Forbidden
GET /./././../config → 403 Forbidden
GET //etc/hosts → 403 Forbidden# Valid hosts:
Host: localhost:8080 ✓
Host: 127.0.0.1:8080 ✓
# Invalid hosts:
Host: evil.com → 403 Forbidden
Host: <script>alert(1) → 403 Forbidden# POST requests:
Content-Type: application/json ✓ Accepted
Content-Type: text/plain → 415 Unsupported Media Type
Content-Type: multipart/form → 415 Unsupported Media TypeHTML File (200 OK):
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 1234
Date: Fri, 15 Mar 2024 10:30:00 GMT
Server: Multi-threaded HTTP Server
Connection: keep-alive
Keep-Alive: timeout=30, max=100
<!DOCTYPE html>
<html>...Binary File Download (200 OK):
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 45678
Content-Disposition: attachment; filename="image.png"
Date: Fri, 15 Mar 2024 10:30:00 GMT
Server: Multi-threaded HTTP Server
Connection: keep-alive
[binary file data]JSON Upload Success (201 Created):
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 123
Date: Fri, 15 Mar 2024 10:30:00 GMT
Server: Multi-threaded HTTP Server
Connection: keep-alive
{
"status": "success",
"message": "File created successfully",
"filepath": "/uploads/upload_1710504600_a7b9.json"
}- 400 Bad Request: Malformed request or missing Host header
- 403 Forbidden: Path traversal attempt or Host validation failure
- 404 Not Found: Requested file doesn't exist
- 405 Method Not Allowed: Unsupported HTTP method (only GET/POST supported)
- 415 Unsupported Media Type: Invalid Content-Type or file extension
- 500 Internal Server Error: Server-side processing error
# Test HTML serving
curl http://localhost:8080/ # Should return index.html
curl http://localhost:8080/about.html # Should return about page
# Test file downloads
curl -I http://localhost:8080/sample.txt # Check headers
curl -O http://localhost:8080/image.png # Download image
# Test JSON upload
curl -X POST http://localhost:8080/upload \
-H "Content-Type: application/json" \
-d '{"test": "data"}' # Should create file
# Test error cases
curl http://localhost:8080/nonexistent.txt # Should return 404
curl -X PUT http://localhost:8080/ # Should return 405
curl -X POST http://localhost:8080/upload \
-H "Content-Type: text/plain" \
-d "not json" # Should return 415# Test path traversal protection
curl http://localhost:8080/../etc/passwd # Should return 403
curl http://localhost:8080/./././../config # Should return 403
# Test host header validation (if implemented strictly)
curl -H "Host: evil.com" http://localhost:8080/ # May return 403
# Test malformed requests
curl http://localhost:8080/ --data-raw $'GET / HTTP/1.1\r\nInvalid: headers'# Test multiple simultaneous connections
for i in {1..10}; do
curl -s http://localhost:8080/ > /dev/null &
done
wait
# Test large file downloads concurrently
for i in {1..5}; do
curl -s -O http://localhost:8080/image.png &
done
waitThe server provides comprehensive logging for monitoring and debugging:
[2024-03-15 10:30:00] - [INFO] - Socket created
[2024-03-15 10:30:00] - [INFO] - HTTP Server started on http://127.0.0.1:8080
[2024-03-15 10:30:00] - [INFO] - Thread pool size: 10
[2024-03-15 10:30:00] - [INFO] - Serving files from 'resources' directory
[2024-03-15 10:30:00] - [INFO] - Press Ctrl+C to stop the server
[2024-03-15 10:30:15] - [INFO] - [ThreadPoolExecutor-0_1] Connection from 127.0.0.1:54321
[2024-03-15 10:30:15] - [INFO] - [ThreadPoolExecutor-0_1] Request: GET /image.png HTTP/1.1
[2024-03-15 10:30:15] - [INFO] - [ThreadPoolExecutor-0_1] Serving binary file: image.png (45678 bytes)
[2024-03-15 10:30:15] - [INFO] - [ThreadPoolExecutor-0_1] Response: 200 OK (45678 bytes transferred)
[2024-03-15 10:30:15] - [INFO] - [ThreadPoolExecutor-0_1] Connection: keep-alive
[2024-03-15 10:30:20] - [WARNING] - Path traversal attempt blocked: ../etc/passwd
[2024-03-15 10:30:25] - [WARNING] - Unsupported Content-Type for POST: text/plain
[2024-03-15 10:30:30] - [WARNING] - Request missing Host header
- Default Port: 8080
- Default Host: 127.0.0.1 (localhost)
- Thread Pool Size: 10 (configurable)
- Max Request Size: 8192 bytes
- Connection Timeout: 30 seconds
- Max Requests per Connection: 100
- HTML Files (.html, .htm): Served with
text/htmlContent-Type for browser viewing - Binary Files (.txt, .png, .jpg, .jpeg): Served with
application/octet-streamfor download - JSON Data: Accepted via POST with
application/jsonContent-Type
- File Size: Large files (>100MB) may impact performance due to in-memory processing
- SSL/HTTPS: Currently supports HTTP only (no encryption)
- Authentication: No built-in authentication or authorization mechanisms
- Caching: No HTTP caching headers or conditional requests support
- Compression: No gzip or other compression support
- Virtual Hosts: Single host serving only
This is an educational project demonstrating HTTP server implementation concepts. Suggestions and improvements are welcome!
This project is created for educational purposes. Feel free to use and modify as needed.
This implementation meets all specified requirements:
- ✅ Multi-threaded HTTP server using socket programming
- ✅ Configurable server parameters (port, host, thread pool size)
- ✅ GET request handling for HTML and binary files
- ✅ POST request handling for JSON data uploads
- ✅ Path traversal protection and Host header validation
- ✅ Proper HTTP response formatting with required headers
- ✅ Connection management with keep-alive support
- ✅ Comprehensive logging for all operations
- ✅ Error handling with appropriate HTTP status codes
- ✅ Binary file transfer with integrity preservation
Server Status: Ready for production testing and evaluation! 🎉