From 89447cbc1522324cd13c20a2a6266602b7858beb Mon Sep 17 00:00:00 2001 From: mkelvers Date: Fri, 3 Jul 2026 02:57:07 +0200 Subject: [PATCH] feat: add compression writer struct and basic methods --- internal/server/static_delivery.go | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/internal/server/static_delivery.go b/internal/server/static_delivery.go index 51f015f2..9e7aa890 100644 --- a/internal/server/static_delivery.go +++ b/internal/server/static_delivery.go @@ -2,6 +2,8 @@ package server import ( "bufio" + "bytes" + "compress/gzip" "mime" "net" "net/http" @@ -156,6 +158,9 @@ const ( compressionGzip //nolint:unused ) +//nolint:unused +const compressionMinLength = 1024 + //nolint:unused func acceptsGzip(value string) bool { wildcard := false @@ -198,3 +203,94 @@ func canNegotiateCompression(r *http.Request) bool { } return !isCompressedPath(r.URL.Path) } + +//nolint:unused +type compressionWriter struct { + gin.ResponseWriter + buffer bytes.Buffer + gzip *gzip.Writer + mode compressionMode + status int + wroteBody bool + acceptsGzip bool +} + +//nolint:unused +func (w *compressionWriter) WriteHeader(status int) { + if status > 0 && w.mode == compressionUndecided && !w.wroteBody { + w.status = status + } +} + +//nolint:unused +func (w *compressionWriter) WriteHeaderNow() { + if w.mode == compressionUndecided { + w.startPlain() + _ = w.flushBuffer() + } +} + +//nolint:unused +func (w *compressionWriter) WriteString(data string) (int, error) { + return w.Write([]byte(data)) +} + +//nolint:unused +func (w *compressionWriter) Status() int { + if w.status != 0 { + return w.status + } + return w.ResponseWriter.Status() +} + +//nolint:unused +func (w *compressionWriter) Size() int { + if size := w.ResponseWriter.Size(); size >= 0 { + return size + } + if w.buffer.Len() > 0 { + return w.buffer.Len() + } + return -1 +} + +//nolint:unused +func (w *compressionWriter) Written() bool { + return w.wroteBody || w.ResponseWriter.Written() +} + +//nolint:unused +func (w *compressionWriter) startPlain() { + if w.mode != compressionUndecided { + return + } + w.mode = compressionPlain + w.ResponseWriter.WriteHeader(w.Status()) +} + +//nolint:unused +func (w *compressionWriter) flushBuffer() error { + if w.buffer.Len() == 0 { + return nil + } + data := w.buffer.Bytes() + w.buffer.Reset() + var err error + if w.mode == compressionGzip { + _, err = w.gzip.Write(data) + } else { + //nolint:staticcheck + _, err = w.ResponseWriter.Write(data) + } + return err +} + +//nolint:unused +func (w *compressionWriter) writePlain(data []byte) (int, error) { + w.startPlain() + if err := w.flushBuffer(); err != nil { + return 0, err + } + //nolint:staticcheck + return w.ResponseWriter.Write(data) +}