From 93e84a1c576dca96e9ca33a3dfb0742967d4a1ad Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
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 0f7567509c..0cdff3a2c7 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 0045e0c9b8..194d0a30f7 100644
--- a/app/vmselect/main.go
+++ b/app/vmselect/main.go
@@ -296,7 +296,7 @@ func selectHandler(qt *querytracer.Tracer, startTime time.Time, w http.ResponseW
 		_ = r.ParseForm()
 		suffix := strings.Replace(p.Suffix, "prometheus/", "../prometheus/", 1)
 		newURL := suffix + "/?" + r.Form.Encode()
-		httpserver.RedirectPermanent(w, newURL)
+		httpserver.Redirect(w, newURL)
 		return true
 	}
 	if strings.HasPrefix(p.Suffix, "vmui/") || strings.HasPrefix(p.Suffix, "prometheus/vmui/") {
@@ -350,7 +350,7 @@ func selectHandler(qt *querytracer.Tracer, startTime time.Time, w http.ResponseW
 
 	if p.Suffix == "prometheus/vmalert" {
 		path := "../" + p.Suffix + "/"
-		httpserver.RedirectPermanent(w, path)
+		httpserver.Redirect(w, path)
 		return true
 	}
 	if strings.HasPrefix(p.Suffix, "prometheus/vmalert/") {
diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go
index 5beeef1e2d..14ad40ba4d 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)
 }