--- description: HTTP proxy and server patterns --- # HTTP Proxy and Server Patterns ## Request Handling - Only support GET requests (Steam doesn't use other methods) - Reject non-GET requests with 405 Method Not Allowed - Handle health checks at "/" endpoint - Support LanCache heartbeat at "/lancache-heartbeat" ## Upstream Communication - Use optimized HTTP transport with connection pooling - Set appropriate timeouts (10s dial, 15s header, 60s total) - Enable HTTP/2 and keep-alives for better performance - Use large buffers (64KB) for better throughput ## Response Streaming - Stream responses directly to clients for better performance - Support both full file and range request streaming - Preserve original HTTP headers (excluding hop-by-hop headers) - Add cache-specific headers (X-LanCache-Status, X-LanCache-Processed-By) ## Error Handling - Implement retry logic with exponential backoff - Handle upstream server errors gracefully - Return appropriate HTTP status codes - Log errors with sufficient context for debugging ## Concurrency Control - Use semaphores to limit concurrent requests globally - Implement per-client rate limiting - Clean up old client limiters to prevent memory leaks - Use proper synchronization for shared data structures ## Header Management - Copy relevant headers from upstream responses - Exclude hop-by-hop headers (Connection, Keep-Alive, etc.) - Add cache status headers for monitoring - Preserve Content-Type and Content-Length headers ## Client IP Detection - Check X-Forwarded-For header first (for proxy setups) - Fall back to X-Real-IP header - Use RemoteAddr as final fallback - Handle comma-separated IP lists in X-Forwarded-For ## Performance Optimizations - Set keep-alive headers for better connection reuse - Use appropriate server timeouts - Implement request coalescing for duplicate requests - Use buffered I/O for better performance ## Security Considerations - Validate request URLs and paths - Implement rate limiting to prevent abuse - Log suspicious activity - Handle malformed requests gracefully