From 2b6bf4551362f278d44ff7f04b1570a6ef173765 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Tue, 17 Dec 2024 16:39:44 +0000 Subject: [PATCH] fix: use byte.Buffer as the Write method can be called multiple times --- cache.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cache.go b/cache.go index 3bf46cf..5e647bb 100644 --- a/cache.go +++ b/cache.go @@ -138,12 +138,11 @@ func (c *Client) Middleware(next http.Handler) http.Handler { next.ServeHTTP(rw, r) statusCode := rw.statusCode - value := rw.body now := time.Now() expires := now.Add(c.ttl) if statusCode < 400 { response := Response{ - Value: value, + Value: rw.body.Bytes(), Header: rw.Header(), Expiration: expires, LastAccess: now, @@ -297,7 +296,7 @@ func ClientWithExpiresHeader() ClientOption { type responseWriter struct { http.ResponseWriter statusCode int - body []byte + body bytes.Buffer } func (w *responseWriter) WriteHeader(statusCode int) { @@ -306,6 +305,6 @@ func (w *responseWriter) WriteHeader(statusCode int) { } func (w *responseWriter) Write(b []byte) (int, error) { - w.body = b + w.body.Write(b) return w.ResponseWriter.Write(b) }