Files
pages/internal/server/middleware/normalize.go
T
Anya Tokugawa 19e12f96a8
Build and publish container images / images (push) Successful in 13m39s
Release container images / release (push) Successful in 18s
Add private CardDAV synchronization and contact revision recovery
2026-11-02 08:41:15 +09:00

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)
},
)
}