TASK
Implementation
Build an HTTP-aware reverse proxy with caching:
- Parse incoming HTTP requests
- Check cache for valid response
- On cache miss, forward to backend
- Parse response headers for caching policy
- Cache response according to Cache-Control
- Return response to client
Support ETag and If-Modified-Since for cache validation.
Sample Test Cases
Cache with max-ageTimeout: 5000ms
Input
{
"src": "c0",
"dest": "n1",
"body": {
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": [
"n1"
]
}
}Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}Honor no-storeTimeout: 5000ms
Input
{
"src": "c0",
"dest": "n1",
"body": {
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": [
"n1"
]
}
}Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}Hints
Hint 1▾
Parse HTTP headers for caching hints
Hint 2▾
Honor Cache-Control directives
Hint 3▾
Implement ETag validation
OVERVIEW
Theoretical Hub
HTTP Caching Headers
HTTP provides rich cache control: Cache-Control sets freshness duration, ETag enables validation without full fetch, Vary handles content negotiation. A good proxy respects all of these.
Cache Validation
Instead of fetching a full response, validation asks "has this changed?" using If-None-Match (with ETag) or If-Modified-Since. A 304 Not Modified response confirms the cached copy is still valid.
Key Concepts
reverse proxyHTTP cachingCache-Control
main.py
python
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
#!/usr/bin/env python3
import sys
import json
import time
import re
class HTTPCachingProxy:
def __init__(self, backend):
self.backend = backend
self.cache = {} # key -> (response, expiry, etag)
def cache_key(self, request):
return f"{request['method']}:{request['path']}"
def parse_cache_control(self, headers):
# TODO: Parse max-age, no-cache, no-store
pass
def is_fresh(self, cached_entry):
# TODO: Check if cached response is still valid
pass
def validate_cache(self, key, etag):
# TODO: Send conditional request with If-None-Match
pass
def handle_request(self, request):
# TODO: Check cache, validate, or fetch fresh
pass
if __name__ == "__main__":
pass