mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/httpserver: use 302 redirects instead of 301 redirects
Incorrect 301 redirects can be cached by user agents such as web browsers. This can complicate recovery procedure after the incorrect redirect is fixed, e.g. web browser cache must be reset. The related issue - https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1752
This commit is contained in:
parent
a296994fed
commit
725dfb0ed6
3 changed files with 10 additions and 7 deletions
|
@ -168,7 +168,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/v1/") {
|
if strings.HasPrefix(r.URL.Path, "/api/v1/") {
|
||||||
redirectURL = alert.APILink()
|
redirectURL = alert.APILink()
|
||||||
}
|
}
|
||||||
httpserver.RedirectPermanent(w, "/"+redirectURL)
|
httpserver.Redirect(w, "/"+redirectURL)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,7 +168,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
_ = r.ParseForm()
|
_ = r.ParseForm()
|
||||||
path = strings.TrimPrefix(path, "/")
|
path = strings.TrimPrefix(path, "/")
|
||||||
newURL := path + "/?" + r.Form.Encode()
|
newURL := path + "/?" + r.Form.Encode()
|
||||||
httpserver.RedirectPermanent(w, newURL)
|
httpserver.Redirect(w, newURL)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(path, "/vmui/") {
|
if strings.HasPrefix(path, "/vmui/") {
|
||||||
|
@ -217,7 +217,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
// vmalert access via incomplete url without `/` in the end. Redirecto to complete url.
|
// vmalert access via incomplete url without `/` in the end. Redirecto to complete url.
|
||||||
// Use relative redirect, since, since the hostname and path prefix may be incorrect if VictoriaMetrics
|
// Use relative redirect, since, since the hostname and path prefix may be incorrect if VictoriaMetrics
|
||||||
// is hidden behind vmauth or similar proxy.
|
// is hidden behind vmauth or similar proxy.
|
||||||
httpserver.RedirectPermanent(w, "vmalert/")
|
httpserver.Redirect(w, "vmalert/")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(path, "/vmalert/") {
|
if strings.HasPrefix(path, "/vmalert/") {
|
||||||
|
|
|
@ -248,7 +248,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
|
||||||
// This is needed for proper handling of relative urls in web browsers.
|
// This is needed for proper handling of relative urls in web browsers.
|
||||||
// Intentionally ignore query args, since it is expected that the requested url
|
// Intentionally ignore query args, since it is expected that the requested url
|
||||||
// is composed by a human, so it doesn't contain query args.
|
// is composed by a human, so it doesn't contain query args.
|
||||||
RedirectPermanent(w, prefix)
|
Redirect(w, prefix)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(path, prefix) {
|
if !strings.HasPrefix(path, prefix) {
|
||||||
|
@ -681,11 +681,14 @@ func GetRequestURI(r *http.Request) string {
|
||||||
return requestURI + delimiter + queryArgs
|
return requestURI + delimiter + queryArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectPermanent redirects to the given url using 301 status code.
|
// Redirect redirects to the given url.
|
||||||
func RedirectPermanent(w http.ResponseWriter, url string) {
|
func Redirect(w http.ResponseWriter, url string) {
|
||||||
// Do not use http.Redirect, since it breaks relative redirects
|
// Do not use http.Redirect, since it breaks relative redirects
|
||||||
// if the http.Request.URL contains unexpected url.
|
// if the http.Request.URL contains unexpected url.
|
||||||
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2918
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2918
|
||||||
w.Header().Set("Location", url)
|
w.Header().Set("Location", url)
|
||||||
w.WriteHeader(http.StatusMovedPermanently)
|
// Use http.StatusFound instead of http.StatusMovedPermanently,
|
||||||
|
// since browsers can cache incorrect redirects returned with StatusMovedPermanently.
|
||||||
|
// This may require browser cache cleaning after the incorrect redirect is fixed.
|
||||||
|
w.WriteHeader(http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue