nginx-api-key-proxy / demo_upstream.py
AnthropicallyHuggingFaces's picture
Add demo upstream that echoes received headers
015a9df verified
Raw History Blame Contribute Delete
1.17 kB
#!/usr/bin/env python3
"""Minimal upstream that echoes the request headers it received.
Used to make the reverse-proxy key injection observable: any request to
/model-api/ is proxied here, and the JSON response shows the
`Authorization` header nginx attached server-side.
"""
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
HOST, PORT = "127.0.0.1", 5000
class EchoHandler(BaseHTTPRequestHandler):
def _respond(self):
payload = {
"method": self.command,
"path": self.path,
"received_headers": {k: v for k, v in self.headers.items()},
"note": "Authorization here is injected by nginx, not by the client.",
}
body = json.dumps(payload, indent=2).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = _respond
def log_message(self, *_args):
pass
if __name__ == "__main__":
HTTPServer((HOST, PORT), EchoHandler).serve_forever()