MCPcopy Index your code
hub / github.com/filebrowser/filebrowser / stripPrefix

Function stripPrefix

http/utils.go:55–80  ·  view source on GitHub ↗

This is an adaptation if http.StripPrefix in which we don't return 404 if the page doesn't have the needed prefix.

(prefix string, h http.Handler)

Source from the content-addressed store, hash-verified

53// This is an adaptation if http.StripPrefix in which we don't
54// return 404 if the page doesn't have the needed prefix.
55func stripPrefix(prefix string, h http.Handler) http.Handler {
56 if prefix == "" || prefix == "/" {
57 return h
58 }
59
60 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61 p := strings.TrimPrefix(r.URL.Path, prefix)
62 rp := strings.TrimPrefix(r.URL.RawPath, prefix)
63
64 // If the path is exactly the prefix (no trailing slash), redirect to
65 // the prefix with a trailing slash so the router receives "/" instead
66 // of "", which would otherwise cause a redirect to the site root.
67 if p == "" {
68 http.Redirect(w, r, prefix+"/", http.StatusMovedPermanently)
69 return
70 }
71
72 r2 := new(http.Request)
73 *r2 = *r
74 r2.URL = new(url.URL)
75 *r2.URL = *r.URL
76 r2.URL.Path = p
77 r2.URL.RawPath = rp
78 h.ServeHTTP(w, r2)
79 })
80}

Callers 2

NewHandlerFunction · 0.85
handleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected