Skip to content

A complete HTTP server implementation. Built entirely from scratch using Python's standard library

Notifications You must be signed in to change notification settings

Ujjwaljain16/HttpServer

Repository files navigation

πŸš€ Multi-threaded HTTP Server

Python Tests Security Performance

A production-ready, multi-threaded HTTP/1.1 server built from scratch
Demonstrating advanced Python networking, concurrent programming, security implementation


🎯 Project Overview

This project showcases a complete HTTP server implementation. Built entirely from scratch using Python's standard library, it demonstrates :

  • System Programming - Low-level socket programming and network protocols
  • Concurrent Programming - Multi-threading with bounded thread pools
  • Security Engineering - Comprehensive security measures and attack prevention
  • Production Operations - Monitoring, logging, and deployment automation
  • Modern DevOps - Production deployment and monitoring practices

🎯 Live Demo

See it in action! Run the comprehensive test suite to showcase all features:

# Start the server
python server.py 8080 127.0.0.1 4

# Run the comprehensive test suite
python final_demo_test.py

Demo Output: Screenshots: Screenshot 2025-09-20 165751 Screenshot 2025-09-20 165806 Screenshot 2025-09-20 165817

✨ Key Features

🌐 HTTP/1.1 Compliance

  • βœ… Persistent Connections - Keep-alive with 30-second idle timeout
  • βœ… Request Limits - Maximum 100 requests per connection
  • βœ… Proper Headers - Connection, Keep-Alive, Content-Type, Content-Length
  • βœ… Status Codes - 200, 201, 400, 403, 404, 405, 415, 503

πŸ”§ Multi-threading & Performance

  • βœ… Thread Pool - Fixed-size worker thread pool with bounded queue
  • βœ… Graceful Degradation - 503 responses when thread pool is saturated
  • βœ… Connection Management - Efficient socket handling and cleanup
  • βœ… Resource Optimization - Bounded queue prevents memory exhaustion
  • βœ… High Throughput - 500+ requests/second capability

πŸ”’ Enterprise Security

  • βœ… Path Traversal Protection - Blocks ../ and percent-encoded attacks
  • βœ… Host Header Validation - Prevents Host header injection attacks
  • βœ… Input Sanitization - JSON validation and content-type checking
  • βœ… Security Logging - Comprehensive audit trail for security violations
  • βœ… Rate Limiting - Per-IP rate limiting with burst protection
  • βœ… Request Size Limiting - DoS protection with configurable limits
  • βœ… CORS Support - Cross-origin resource sharing for web applications
  • βœ… Security Dashboard - Real-time attack monitoring and visualization

πŸ“Š Advanced Monitoring & Observability

  • βœ… Thread Tracking - Real-time thread status monitoring
  • βœ… JSON Logging - Structured logging for analysis
  • βœ… Security Audit - Detailed security violation logging
  • βœ… Performance Metrics - Request timing and thread utilization
  • βœ… Response Time Tracking - Per-request performance monitoring
  • βœ… Memory Monitoring - System resource usage tracking
  • βœ… Prometheus Metrics - /metrics endpoint for monitoring systems
  • βœ… Connection Pooling - Efficient resource management

πŸ§ͺ Comprehensive Testing

  • βœ… Unit Tests - 28+ tests with 100% pass rate
  • βœ… Integration Tests - Complete end-to-end testing
  • βœ… Load Testing - Thread pool saturation and 503 behavior
  • βœ… Security Testing - Path traversal and Host header validation
  • βœ… Performance Testing - Concurrent request handling

πŸš€ Quick Start

Prerequisites

  • Python 3.11 or higher
  • No external dependencies (uses only standard library)

Installation

# Clone the repository
git clone https://github.com/ujjwaljain16/multi-threaded-http-server.git


# Install dependencies (optional - for monitoring features)
pip install -r requirements.txt

Running the Server

# Basic usage (default: 127.0.0.1:8080, 10 threads)
python server.py

# Custom configuration
python server.py 9090 0.0.0.0 20

Example Usage

# Get performance metrics
curl http://127.0.0.1:8080/metrics

# View security dashboard
curl http://127.0.0.1:8080/security-dashboard

# Test CORS
curl -H "Origin: http://localhost:3000" http://127.0.0.1:8080/

# Test advanced features
python test_advanced_features.py

πŸ“– Documentation

This project includes comprehensive documentation:

πŸ“ Project Structure

β”œβ”€β”€ server.py                      # Main server entry point
β”œβ”€β”€ server_lib/                    # Core server modules
β”‚   β”œβ”€β”€ __init__.py               # Package initialization
β”‚   β”œβ”€β”€ http_parser.py            # HTTP request parsing
β”‚   β”œβ”€β”€ threadpool.py             # Thread pool implementation
β”‚   β”œβ”€β”€ security.py               # Security and validation
β”‚   β”œβ”€β”€ response.py               # HTTP response building
β”‚   β”œβ”€β”€ logger.py                 # Enhanced logging system
β”‚   β”œβ”€β”€ metrics.py                # Performance metrics collection
β”‚   β”œβ”€β”€ metrics_endpoint.py       # Metrics API endpoint
β”‚   β”œβ”€β”€ rate_limiter.py           # Rate limiting implementation
β”‚   β”œβ”€β”€ request_limiter.py        # Request size limiting
β”‚   β”œβ”€β”€ cors.py                   # CORS support
β”‚   β”œβ”€β”€ security_dashboard.py     # Security monitoring dashboard
β”‚   β”œβ”€β”€ connection_pool.py        # Connection pooling
β”‚   └── utils.py                  # Utility functions
β”œβ”€β”€ resources/                     # Static files and samples
β”‚   β”œβ”€β”€ index.html                # Professional homepage
β”‚   β”œβ”€β”€ about.html                # Technical documentation
β”‚   β”œβ”€β”€ contact.html              # Interactive API testing
β”‚   β”œβ”€β”€ logo.png                  # Logo image
β”‚   β”œβ”€β”€ photo.jpg, photo2.jpg     # Sample images
β”‚   β”œβ”€β”€ big.png                   # Large image for testing
β”‚   β”œβ”€β”€ readme.txt                # Text file for testing
β”‚   β”œβ”€β”€ sample.txt                # Sample text file
β”‚   β”œβ”€β”€ sample_data.json          # Sample JSON data
β”‚   β”œβ”€β”€ simple_test.json          # Test JSON file
β”‚   β”œβ”€β”€ test_payload.json         # Test payload
β”‚   └── uploads/                  # POST upload directory
β”‚       └── upload_*.json         # Uploaded files
β”œβ”€β”€ final_demo_test.py            # Comprehensive test suite (28 tests)
β”œβ”€β”€ quick_test.py                 # Quick functionality test
β”œβ”€β”€ requirements.txt              # Python dependencies
β”œβ”€β”€ REQUIREMENTS_CHECKLIST.md     # Requirements verification
β”œβ”€β”€ TECHNICAL_DOCUMENTATION.md    # Technical implementation docs
β”œβ”€β”€ THEORETICAL_DOCUMENTATION.md  # Theoretical concepts docs
β”œβ”€β”€ security.log                  # Security violation logs
└── README.md                     # This file

🌐 API Endpoints

Method Endpoint Description Status
GET / Homepage with features showcase 200 OK
GET /about.html Technical documentation 200 OK
GET /contact.html Interactive API testing 200 OK
GET /readme.txt Text file download 200 OK
GET /sample.txt Sample text file 200 OK
GET /logo.png Logo image file 200 OK
GET /photo.jpg Sample image 200 OK
GET /photo2.jpg Sample image 200 OK
GET /big.png Large image for testing 200 OK
GET /sample_data.json Sample JSON data 200 OK
GET /simple_test.json Test JSON file 200 OK
GET /test_payload.json Test payload 200 OK
GET /metrics Performance metrics 200 OK
GET /security-dashboard Security monitoring 200 OK
POST /upload JSON data upload 201 Created

Example Usage

# Get homepage
curl http://127.0.0.1:8080/

# Download text file
curl -O http://127.0.0.1:8080/readme.txt

# Download image
curl -O http://127.0.0.1:8080/logo.png

# Get JSON data
curl http://127.0.0.1:8080/sample_data.json

# Upload JSON
curl -X POST -H "Content-Type: application/json" \
     -d '{"test": "data"}' http://127.0.0.1:8080/upload

# Get metrics
curl http://127.0.0.1:8080/metrics

# View security dashboard
curl http://127.0.0.1:8080/security-dashboard

# Test security (should return 403)
curl http://127.0.0.1:8080/../etc/passwd

πŸ”§ Configuration

Command Line Arguments

python server.py [port] [host] [thread_pool_size]
  • port: Server port (default: 8080)

  • host: Server host (default: 127.0.0.1)

  • thread_pool_size: Number of worker threads (default: 10


GitHub Python

About

A complete HTTP server implementation. Built entirely from scratch using Python's standard library

Resources

Stars

Watchers

Forks