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 +}