mirror of
https://github.com/adnanh/webhook.git
synced 2026-07-26 17:19:17 +08:00
0fa8bbf710
* Update go-chi dependency to v5 * Update gofrs/uuid dependency to v5 * Update gorilla/mux dependency to v1.8.1 * Update go-humanize dependency to v1.0.1 * Update mxj dependency to v2.7.0 * Update fsnotify dependency to v1.7.0 * Update Go versions in GH build workflow * Update gopkg.in/yaml.v2 indirect dependency to v2.4.0 * Bump GH actions
40 lines
977 B
Go
40 lines
977 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
// GetHead automatically route undefined HEAD requests to GET handlers.
|
|
func GetHead(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "HEAD" {
|
|
rctx := chi.RouteContext(r.Context())
|
|
routePath := rctx.RoutePath
|
|
if routePath == "" {
|
|
if r.URL.RawPath != "" {
|
|
routePath = r.URL.RawPath
|
|
} else {
|
|
routePath = r.URL.Path
|
|
}
|
|
}
|
|
|
|
// Temporary routing context to look-ahead before routing the request
|
|
tctx := chi.NewRouteContext()
|
|
|
|
// Attempt to find a HEAD handler for the routing path, if not found, traverse
|
|
// the router as through its a GET route, but proceed with the request
|
|
// with the HEAD method.
|
|
if !rctx.Routes.Match(tctx, "HEAD", routePath) {
|
|
rctx.RouteMethod = "GET"
|
|
rctx.RoutePath = routePath
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|