diff --git a/cache.go b/cache.go index 723a46a..f3a6819 100644 --- a/cache.go +++ b/cache.go @@ -94,7 +94,7 @@ func (a *goBlog) cacheMiddleware(next http.Handler) http.Handler { } func cacheable(r *http.Request) bool { - if !(r.Method == http.MethodGet || r.Method == http.MethodHead) { + if r.Method != http.MethodGet && r.Method != http.MethodHead { return false } if r.URL.Query().Get("cache") == "0" || r.URL.Query().Get("cache") == "false" { diff --git a/http.go b/http.go index 1598bb1..06ccd9e 100644 --- a/http.go +++ b/http.go @@ -46,7 +46,7 @@ func (a *goBlog) startServer() (err error) { compressor.SetEncoder("br", func(w io.Writer, _ int) io.Writer { return brotli.NewWriter(w) }) - h = h.Append(middleware.Recoverer, compressor.Handler, middleware.Heartbeat("/ping"), headAsGetHandler) + h = h.Append(middleware.Recoverer, compressor.Handler, middleware.Heartbeat("/ping")) if a.httpsConfigured(false) { h = h.Append(a.securityHeaders) } @@ -225,7 +225,7 @@ func (a *goBlog) buildRouter() (http.Handler, error) { r.MethodNotAllowed(a.serveNotAllowed) mapRouter.DefaultHandler = r - return mapRouter, nil + return alice.New(headAsGetHandler).Then(mapRouter), nil } func (a *goBlog) servePostsAliasesRedirects() http.HandlerFunc { diff --git a/httpMiddlewares.go b/httpMiddlewares.go index b34de89..2702a6b 100644 --- a/httpMiddlewares.go +++ b/httpMiddlewares.go @@ -23,7 +23,13 @@ func fixHTTPHandler(next http.Handler) http.Handler { func headAsGetHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodHead { - r.Method = http.MethodGet + // Clone request and change method + newReq := new(http.Request) + *newReq = *r + newReq.Method = http.MethodGet + // Serve new request + next.ServeHTTP(w, newReq) + return } next.ServeHTTP(w, r) })