mirror of
https://github.com/1f349/lavender.git
synced 2025-02-23 06:05:08 +00:00
25 lines
630 B
Go
25 lines
630 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
)
|
||
|
|
||
|
func SafeRedirect(rw http.ResponseWriter, req *http.Request) {
|
||
|
redirectUrl := req.FormValue("redirect")
|
||
|
if redirectUrl == "" {
|
||
|
http.Redirect(rw, req, "/", http.StatusFound)
|
||
|
return
|
||
|
}
|
||
|
parse, err := url.Parse(redirectUrl)
|
||
|
if err != nil {
|
||
|
http.Error(rw, "Failed to parse redirect url: "+redirectUrl, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
if parse.Scheme != "" && parse.Opaque != "" && parse.User != nil && parse.Host != "" {
|
||
|
http.Error(rw, "Invalid redirect url: "+redirectUrl, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
http.Redirect(rw, req, parse.String(), http.StatusFound)
|
||
|
}
|