From 725dfb0ed69195c297b087b528e300b0f26dcd66 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 1 Oct 2022 16:53:33 +0300 Subject: [PATCH] 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 --- app/vmalert/web.go | 2 +- app/vmselect/main.go | 4 ++-- lib/httpserver/httpserver.go | 11 +++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/vmalert/web.go b/app/vmalert/web.go index 0f7567509..0cdff3a2c 100644 --- a/app/vmalert/web.go +++ b/app/vmalert/web.go @@ -168,7 +168,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { if strings.HasPrefix(r.URL.Path, "/api/v1/") { redirectURL = alert.APILink() } - httpserver.RedirectPermanent(w, "/"+redirectURL) + httpserver.Redirect(w, "/"+redirectURL) return true } } diff --git a/app/vmselect/main.go b/app/vmselect/main.go index cd7cfaec9..9363a966d 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -168,7 +168,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool { _ = r.ParseForm() path = strings.TrimPrefix(path, "/") newURL := path + "/?" + r.Form.Encode() - httpserver.RedirectPermanent(w, newURL) + httpserver.Redirect(w, newURL) return true } 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. // Use relative redirect, since, since the hostname and path prefix may be incorrect if VictoriaMetrics // is hidden behind vmauth or similar proxy. - httpserver.RedirectPermanent(w, "vmalert/") + httpserver.Redirect(w, "vmalert/") return true } if strings.HasPrefix(path, "/vmalert/") { diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index 5beeef1e2..14ad40ba4 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -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. // Intentionally ignore query args, since it is expected that the requested url // is composed by a human, so it doesn't contain query args. - RedirectPermanent(w, prefix) + Redirect(w, prefix) return } if !strings.HasPrefix(path, prefix) { @@ -681,11 +681,14 @@ func GetRequestURI(r *http.Request) string { return requestURI + delimiter + queryArgs } -// RedirectPermanent redirects to the given url using 301 status code. -func RedirectPermanent(w http.ResponseWriter, url string) { +// Redirect redirects to the given url. +func Redirect(w http.ResponseWriter, url string) { // Do not use http.Redirect, since it breaks relative redirects // if the http.Request.URL contains unexpected url. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2918 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) }