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)
| 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. |
| 55 | func 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 | } |
no outgoing calls
no test coverage detected