redirect performs a redirect to a given path. The 'toPath' parameter MUST be solely a path, and MUST NOT include a query.
(w http.ResponseWriter, r *http.Request, toPath string)
| 796 | // redirect performs a redirect to a given path. The 'toPath' parameter |
| 797 | // MUST be solely a path, and MUST NOT include a query. |
| 798 | func redirect(w http.ResponseWriter, r *http.Request, toPath string) error { |
| 799 | for strings.HasPrefix(toPath, "//") { |
| 800 | // prevent path-based open redirects |
| 801 | toPath = strings.TrimPrefix(toPath, "/") |
| 802 | } |
| 803 | // preserve the query string if present |
| 804 | if r.URL.RawQuery != "" { |
| 805 | toPath += "?" + r.URL.RawQuery |
| 806 | } |
| 807 | http.Redirect(w, r, toPath, http.StatusPermanentRedirect) //nolint:gosec // toPath is a same-origin path and leading // is stripped above |
| 808 | return nil |
| 809 | } |
| 810 | |
| 811 | // statusOverrideResponseWriter intercepts WriteHeader calls |
| 812 | // to instead write the HTTP status code we want instead |
no outgoing calls
no test coverage detected