Fix headAsGetHandler

This commit is contained in:
Jan-Lukas Else 2022-01-31 11:31:16 +01:00
parent 1c7195a135
commit da3b9d5c76
3 changed files with 10 additions and 4 deletions

View File

@ -94,7 +94,7 @@ func (a *goBlog) cacheMiddleware(next http.Handler) http.Handler {
} }
func cacheable(r *http.Request) bool { 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 return false
} }
if r.URL.Query().Get("cache") == "0" || r.URL.Query().Get("cache") == "false" { if r.URL.Query().Get("cache") == "0" || r.URL.Query().Get("cache") == "false" {

View File

@ -46,7 +46,7 @@ func (a *goBlog) startServer() (err error) {
compressor.SetEncoder("br", func(w io.Writer, _ int) io.Writer { compressor.SetEncoder("br", func(w io.Writer, _ int) io.Writer {
return brotli.NewWriter(w) 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) { if a.httpsConfigured(false) {
h = h.Append(a.securityHeaders) h = h.Append(a.securityHeaders)
} }
@ -225,7 +225,7 @@ func (a *goBlog) buildRouter() (http.Handler, error) {
r.MethodNotAllowed(a.serveNotAllowed) r.MethodNotAllowed(a.serveNotAllowed)
mapRouter.DefaultHandler = r mapRouter.DefaultHandler = r
return mapRouter, nil return alice.New(headAsGetHandler).Then(mapRouter), nil
} }
func (a *goBlog) servePostsAliasesRedirects() http.HandlerFunc { func (a *goBlog) servePostsAliasesRedirects() http.HandlerFunc {

View File

@ -23,7 +23,13 @@ func fixHTTPHandler(next http.Handler) http.Handler {
func headAsGetHandler(next http.Handler) http.Handler { func headAsGetHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead { 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) next.ServeHTTP(w, r)
}) })