Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Decide gzip eligibility before a pre-write flush commits headers
rcourtman71347eac4c36
diff --git a/pkg/server/gzip_middleware.go b/pkg/server/gzip_middleware.goindex a800a08539..4f7591704a 100644--- a/pkg/server/gzip_middleware.go+++ b/pkg/server/gzip_middleware.go@@ -153,6 +153,18 @@ func (w *gzipResponseWriter) Write(data []byte) (int, error) {}func (w *gzipResponseWriter) Flush() {+ // A flush commits the response headers (implicit 200), so the+ // compress-or-not decision must be made now. Deciding later, after the+ // headers are on the wire, would gzip the body without ever sending+ // Content-Encoding.+ if !w.decided {+ w.decide(http.StatusOK)+ }+ if w.compressing && w.gz == nil {+ gz := gzipWriterPool.Get().(*gzip.Writer)+ gz.Reset(w.ResponseWriter)+ w.gz = gz+ }if w.gz != nil {if err := w.gz.Flush(); err != nil {returndiff --git a/pkg/server/gzip_middleware_test.go b/pkg/server/gzip_middleware_test.goindex 6def93aafe..f56583e1fa 100644--- a/pkg/server/gzip_middleware_test.go+++ b/pkg/server/gzip_middleware_test.go@@ -166,6 +166,51 @@ func TestWithGzipSkipsNonCompressibleContentTypes(t *testing.T) {}}+func TestWithGzipFlushBeforeFirstWriteKeepsEncodingCoherent(t *testing.T) {+ payload := strings.Repeat(`{"tick":1}`, 512)+ handler := withGzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ w.(http.Flusher).Flush()+ _, _ = w.Write([]byte(payload))+ }))++ req := httptest.NewRequest(http.MethodGet, "/api/stream-ish", nil)+ req.Header.Set("Accept-Encoding", "gzip")+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, req)++ // The flush commits the headers, so whatever the headers said must match+ // the body encoding. Compressing here is fine only because the decision+ // ran before the flush.+ if got := rec.Header().Get("Content-Encoding"); got != "gzip" {+ t.Fatalf("Content-Encoding = %q, want gzip decided at flush time", got)+ }+ if decoded := gunzipBody(t, rec.Body); decoded != payload {+ t.Fatalf("decompressed body does not match payload")+ }+}++func TestWithGzipFlushOnEventStreamStaysIdentity(t *testing.T) {+ handler := withGzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "text/event-stream")+ w.(http.Flusher).Flush()+ _, _ = w.Write([]byte("data: {\"tick\":1}\n\n"))+ w.(http.Flusher).Flush()+ }))++ req := httptest.NewRequest(http.MethodGet, "/api/ai/chat/stream", nil)+ req.Header.Set("Accept-Encoding", "gzip")+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, req)++ if got := rec.Header().Get("Content-Encoding"); got != "" {+ t.Fatalf("Content-Encoding = %q, want empty for flushed SSE", got)+ }+ if !strings.HasPrefix(rec.Body.String(), "data:") {+ t.Fatalf("SSE body altered: %q", rec.Body.String())+ }+}+func TestWithGzipPreservesStatusAndEmptyBodies(t *testing.T) {handler := withGzip(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "application/json")