fix(tls): preserve early API rejection before forwarding upload bodies

This commit is contained in:
2026-11-22 19:16:23 +03:00
parent 37d40e4789
commit 2b6f99284d
3 changed files with 88 additions and 7 deletions
+19 -6
View File
@@ -359,18 +359,31 @@ func (l *boundedListener) Accept() (net.Conn, error) {
}
}
}
func runHTTP(c Config, b Boundary) error {
target, e := url.Parse("http://" + b.Target)
if e != nil {
return e
}
func loopbackProxy(c Config, target *url.URL) *httputil.ReverseProxy {
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = &http.Transport{MaxIdleConns: 32, MaxIdleConnsPerHost: 16, ResponseHeaderTimeout: 8 * time.Second, IdleConnTimeout: 30 * time.Second}
director := proxy.Director
proxy.Director = func(r *http.Request) {
director(r)
if c.Role == "api" && r.Body != nil && r.Body != http.NoBody {
// Uvicorn sends 100 only when ASGI receives the body. Preserve an
// early JSON 4xx without racing an unnecessary large body write.
r.Header.Set("Expect", "100-continue")
}
}
proxy.Transport = &http.Transport{MaxIdleConns: 32, MaxIdleConnsPerHost: 16, ResponseHeaderTimeout: 8 * time.Second, IdleConnTimeout: 30 * time.Second, ExpectContinueTimeout: time.Second}
proxy.ErrorLog = log.New(io.Discard, "", 0)
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, e error) {
w.Header().Set("Connection", "close")
http.Error(w, "Protected service unavailable", 503)
}
return proxy
}
func runHTTP(c Config, b Boundary) error {
target, e := url.Parse("http://" + b.Target)
if e != nil {
return e
}
proxy := loopbackProxy(c, target)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Force a fresh handshake after each response: no indefinitely cached
// certificate identity across rollover, expiry or serial revocation.