Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(ingress): record the final status after a 1xx response

Maohaod1f74a3f857a
diff --git a/components/ingress/pkg/proxy/proxy.go b/components/ingress/pkg/proxy/proxy.go
index 3041df7f8..320c61aaf 100644
--- a/components/ingress/pkg/proxy/proxy.go
+++ b/components/ingress/pkg/proxy/proxy.go
@@ -449,7 +449,10 @@ type statusCapturingResponseWriter struct {
}
func (w *statusCapturingResponseWriter) WriteHeader(code int) {
- if !w.written {
+ // A 1xx other than 101, such as 103 Early Hints forwarded by
+ // ReverseProxy, is informational: the final status is still to come.
+ informational := code >= 100 && code < 200 && code != http.StatusSwitchingProtocols
+ if !w.written && !informational {
w.statusCode = code
w.written = true
}
diff --git a/components/ingress/pkg/proxy/proxy_test.go b/components/ingress/pkg/proxy/proxy_test.go
index ec2f05fed..39a8a5959 100644
--- a/components/ingress/pkg/proxy/proxy_test.go
+++ b/components/ingress/pkg/proxy/proxy_test.go
@@ -19,6 +19,8 @@ import (
"net"
"net/http"
"net/http/httptest"
+ "net/http/httputil"
+ "net/url"
"testing"
"github.com/alibaba/opensandbox/ingress/pkg/sandbox"
@@ -162,3 +164,35 @@ func findAvailablePort() (int, error) {
port := listener.Addr().(*net.TCPAddr).Port
return port, nil
}
+
+func TestStatusCapturingResponseWriterRecordsFinalStatusAfterEarlyHints(t *testing.T) {
+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.Header().Set("Link", "</app.css>; rel=preload")
+ w.WriteHeader(http.StatusEarlyHints)
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer upstream.Close()
+ target, err := url.Parse(upstream.URL)
+ assert.NoError(t, err)
+
+ sw := &statusCapturingResponseWriter{ResponseWriter: httptest.NewRecorder(), statusCode: http.StatusOK}
+ httputil.NewSingleHostReverseProxy(target).ServeHTTP(sw, httptest.NewRequest(http.MethodGet, "/", nil))
+
+ assert.Equal(t, http.StatusNotFound, sw.statusCode)
+ assert.True(t, sw.written)
+}
+
+func TestStatusCapturingResponseWriterInformationalStatusDoesNotCommit(t *testing.T) {
+ for _, code := range []int{http.StatusContinue, http.StatusEarlyHints} {
+ sw := &statusCapturingResponseWriter{ResponseWriter: httptest.NewRecorder(), statusCode: http.StatusOK}
+ sw.WriteHeader(code)
+ assert.False(t, sw.written, "status %d", code)
+ assert.Equal(t, http.StatusOK, sw.statusCode, "status %d", code)
+ }
+
+ // 101 is the final response of a protocol upgrade.
+ sw := &statusCapturingResponseWriter{ResponseWriter: httptest.NewRecorder(), statusCode: http.StatusOK}
+ sw.WriteHeader(http.StatusSwitchingProtocols)
+ assert.True(t, sw.written)
+ assert.Equal(t, http.StatusSwitchingProtocols, sw.statusCode)
+}