| 2558 | var multipleSlashesRe = regexp.MustCompile(`/+`) |
| 2559 | |
| 2560 | func singleSlashMW(next http.Handler) http.Handler { |
| 2561 | fn := func(w http.ResponseWriter, r *http.Request) { |
| 2562 | var path string |
| 2563 | rctx := chi.RouteContext(r.Context()) |
| 2564 | if rctx != nil && rctx.RoutePath != "" { |
| 2565 | path = rctx.RoutePath |
| 2566 | } else { |
| 2567 | path = r.URL.Path |
| 2568 | } |
| 2569 | |
| 2570 | // Normalize multiple slashes to a single slash |
| 2571 | newPath := multipleSlashesRe.ReplaceAllString(path, "/") |
| 2572 | |
| 2573 | // Apply the cleaned path |
| 2574 | // The approach is consistent with: https://github.com/go-chi/chi/blob/e846b8304c769c4f1a51c9de06bebfaa4576bd88/middleware/strip.go#L24-L28 |
| 2575 | if rctx != nil { |
| 2576 | rctx.RoutePath = newPath |
| 2577 | } else { |
| 2578 | r.URL.Path = newPath |
| 2579 | } |
| 2580 | |
| 2581 | next.ServeHTTP(w, r) |
| 2582 | } |
| 2583 | return http.HandlerFunc(fn) |
| 2584 | } |