Files
any-hub/specs/002-fiber-single-proxy/contracts/http-proxy.md
2025-11-14 12:11:44 +08:00

35 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Contract: Host 路由 + 缓存代理
## Request Flow
1. Client sends HTTP request to `http://<listen-host>:<port>/<path>` with `Host: <hub-domain>`.
2. Fiber middleware resolves HubRoute; if missing → 404 JSON `{ "error": "host_unmapped" }`.
3. Proxy handler checks disk cache key = `<hub-name>/<path>`.
4. Cache hit (Valid): stream file to client with stored headers.
5. Cache stale/miss: build upstream request `Upstream + path`, attach `If-None-Match`/`If-Modified-Since` when available, stream response back; on 200 store body to cache.
## Required Headers
- Host (required) → determines Hub.
- `X-Any-Hub-Upstream` (response) actual upstream URL (debugging).
- `X-Any-Hub-Cache-Hit` (response) `true/false`.
- `X-Request-ID` (response) correlation id for logs.
## Error Codes
| Scenario | Status | Body |
|----------|--------|------|
| Host 未配置 | 404 | `{ "error": "host_unmapped" }` |
| 上游 4xx/5xx | mirror | 上游 body 原样透传 |
| 缓存写入失败 | 502 | `{ "error": "cache_write_failed" }` |
## Logging Fields
```
{
"action": "proxy",
"hub": "docker",
"domain": "docker.hub.local",
"cache_hit": true,
"upstream_status": 200,
"elapsed_ms": 123,
"error": ""
}
```