From fbbee456505b77ef4c26b9441003dae456208b9b Mon Sep 17 00:00:00 2001 From: mkelvers Date: Fri, 3 Jul 2026 02:57:39 +0200 Subject: [PATCH] feat: add compression feasibility check --- internal/server/static_delivery.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/server/static_delivery.go b/internal/server/static_delivery.go index d801a72e..259d821e 100644 --- a/internal/server/static_delivery.go +++ b/internal/server/static_delivery.go @@ -25,7 +25,6 @@ func addVary(header http.Header, value string) { header.Add("Vary", value) } -//nolint:unused func isCompressibleContentType(value string) bool { mediaType, _, err := mime.ParseMediaType(value) if err != nil { @@ -322,3 +321,25 @@ func (w *compressionWriter) knownContentLength() (compress, known bool) { } return length >= compressionMinLength, true } + +//nolint:unused +func (w *compressionWriter) canCompress(data []byte) bool { + status := w.Status() + if status < http.StatusOK || status == http.StatusNoContent || status == http.StatusPartialContent || status == http.StatusNotModified { + return false + } + if w.Header().Get("Content-Encoding") != "" { + return false + } + + contentType := w.Header().Get("Content-Type") + if contentType == "" && len(data) > 0 { + contentType = http.DetectContentType(data) + w.Header().Set("Content-Type", contentType) + } + if !isCompressibleContentType(contentType) { + return false + } + addVary(w.Header(), "Accept-Encoding") + return w.acceptsGzip +}