61 lines
965 B
Go
61 lines
965 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func NormalizePath(
|
|
next http.Handler,
|
|
) http.Handler {
|
|
|
|
return http.HandlerFunc(
|
|
func(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
p := r.URL.Path
|
|
// Hosted adapter assets use directory-relative module and worker URLs.
|
|
// DAV collections also require the slash: removing it conflicts with
|
|
// ServeMux's collection redirect and causes an infinite redirect loop.
|
|
if strings.HasPrefix(p, "/_/adaptor/") || strings.HasPrefix(p, "/_/api/v1/caldav/") || strings.HasPrefix(p, "/_/api/v1/carddav/") {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
if p != "/" {
|
|
|
|
for strings.HasSuffix(
|
|
p,
|
|
"/",
|
|
) {
|
|
|
|
p = strings.TrimSuffix(
|
|
p,
|
|
"/",
|
|
)
|
|
}
|
|
|
|
if p == "" {
|
|
p = "/"
|
|
}
|
|
|
|
if p != r.URL.Path {
|
|
|
|
http.Redirect(
|
|
w,
|
|
r,
|
|
p,
|
|
http.StatusMovedPermanently,
|
|
)
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
},
|
|
)
|
|
}
|