-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
167 lines (139 loc) · 7.84 KB
/
__init__.py
File metadata and controls
167 lines (139 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
=============================================================================
HTTPSERVER - Production-Grade HTTP/1.1 Server Built From Scratch
=============================================================================
This package implements a complete HTTP server using raw Python sockets,
demonstrating deep understanding of networking fundamentals, concurrency
patterns, and clean architecture principles.
=============================================================================
PROJECT OVERVIEW
=============================================================================
┌─────────────────────────────────────────────────────────────────────┐
│ PYHTTP SERVER ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ This HTTP server is built entirely from scratch, implementing: │
│ │
│ 1. RAW SOCKET PROGRAMMING │
│ - TCP socket creation and binding │
│ - Accept loop for incoming connections │
│ - Buffered reading and writing │
│ │
│ 2. HTTP/1.1 PROTOCOL │
│ - Request parsing (method, path, headers, body) │
│ - Response building (status, headers, body) │
│ - Keep-alive connection support │
│ │
│ 3. CONCURRENT REQUEST HANDLING │
│ - Thread pool with min/max workers │
│ - Task queue for pending connections │
│ - Graceful shutdown │
│ │
│ 4. MIDDLEWARE ARCHITECTURE │
│ - Chain of Responsibility pattern │
│ - Logging, CORS, rate limiting, compression │
│ │
│ 5. URL ROUTING │
│ - Pattern matching with regex │
│ - Dynamic path parameters (:id, *wildcard) │
│ - Method-based routing (GET, POST, etc.) │
│ │
└─────────────────────────────────────────────────────────────────────┘
=============================================================================
PACKAGE STRUCTURE
=============================================================================
httpserver/
├── __init__.py # This file - package exports
├── __main__.py # CLI entry point (python -m httpserver)
├── server.py # Main HTTPServer class
├── config.py # ServerConfig dataclass
├── core/ # Low-level components
│ ├── socket_server.py # TCP socket handling
│ ├── connection.py # Connection wrapper
│ └── thread_pool.py # Thread pool implementation
├── http/ # HTTP protocol components
│ ├── request.py # HTTP request parsing
│ ├── response.py # HTTP response building
│ ├── router.py # URL routing
│ ├── status_codes.py # HTTP status enums
│ └── mime_types.py # MIME type detection
├── middleware/ # Middleware components
│ ├── base.py # Base middleware classes
│ ├── logging.py # Request logging
│ ├── cors.py # CORS handling
│ ├── compression.py # gzip compression
│ └── rate_limit.py # Rate limiting
└── handlers/ # Request handlers
├── static.py # Static file serving
└── health.py # Health check endpoints
=============================================================================
QUICK START
=============================================================================
from httpserver import HTTPServer, ServerConfig
from httpserver.http.response import ok, created
from httpserver.middleware import LoggingMiddleware
# Create server
server = HTTPServer(ServerConfig(port=8080))
# Add middleware
server.use(LoggingMiddleware())
# Define routes
@server.get("/")
def index(request):
return ok({"message": "Hello, World!"})
@server.get("/users/:id")
def get_user(request):
user_id = request.path_params["id"]
return ok({"id": user_id})
@server.post("/users")
def create_user(request):
data = request.json
return created({"id": 1, **data})
# Run server
server.run()
=============================================================================
FEATURES FOR SDE2 RESUME
=============================================================================
This project demonstrates:
1. SYSTEMS PROGRAMMING
- Raw socket programming without high-level frameworks
- Understanding of TCP/IP networking fundamentals
- Buffer management and byte handling
2. CONCURRENCY PATTERNS
- Thread pool pattern for scalable request handling
- Thread-safe data structures (queues, locks)
- Graceful shutdown with proper cleanup
3. DESIGN PATTERNS
- Chain of Responsibility (middleware)
- Builder (response building)
- Factory (handlers)
- Strategy (routing)
4. PROTOCOL IMPLEMENTATION
- HTTP/1.1 message parsing (RFC 7230)
- Keep-alive connection management
- Content negotiation (encoding, compression)
5. PRODUCTION FEATURES
- Health checks for Kubernetes
- Structured logging for observability
- Rate limiting for protection
- CORS for API security
=============================================================================
INTERVIEW TALKING POINTS
=============================================================================
"I built a production-grade HTTP server from scratch to deeply understand
how web servers work at the protocol level. The project handles:
- TCP connections using raw sockets
- Multi-threaded request processing with a custom thread pool
- Full HTTP/1.1 parsing and response generation
- A middleware pipeline using the Chain of Responsibility pattern
- Rate limiting with the Token Bucket algorithm
- Static file serving with ETag caching
Building this gave me deep insight into the abstractions that frameworks
like Flask and Django provide, and helped me understand networking
concepts that come up in distributed systems design."
=============================================================================
"""
__version__ = "1.0.0"
__author__ = "Your Name"
from .server import HTTPServer
from .config import ServerConfig
__all__ = ["HTTPServer", "ServerConfig", "__version__"]