MCPcopy
hub / github.com/caddyserver/caddy / redirect

Function redirect

modules/caddyhttp/fileserver/staticfiles.go:798–809  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

796// redirect performs a redirect to a given path. The 'toPath' parameter
797// MUST be solely a path, and MUST NOT include a query.
798func 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

Callers 2

serveBrowseMethod · 0.85
ServeHTTPMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected