From 51a77759c1534778d9afd14ea9356908fd244caa Mon Sep 17 00:00:00 2001 From: Nikolay Date: Tue, 3 May 2022 12:31:31 +0200 Subject: [PATCH] lib/promscrape: adds correct http status codes for redirect (#2530) standard http client accepts multiple http status codes as redirect it should fix issue with incorrect redirects https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2482 --- lib/promscrape/client.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/promscrape/client.go b/lib/promscrape/client.go index 0826d20f1..de1fe09c3 100644 --- a/lib/promscrape/client.go +++ b/lib/promscrape/client.go @@ -201,6 +201,15 @@ func (c *client) GetStreamReader() (*streamReader, error) { }, nil } +// checks fasthttp status code for redirect as standard http/client does. +func isStatusRedirect(statusCode int) bool { + switch statusCode { + case 301, 302, 303, 307, 308: + return true + } + return false +} + func (c *client) ReadData(dst []byte) ([]byte, error) { deadline := time.Now().Add(c.hc.ReadTimeout) req := fasthttp.AcquireRequest() @@ -237,7 +246,7 @@ func (c *client) ReadData(dst []byte) ([]byte, error) { err := doRequestWithPossibleRetry(c.hc, req, resp, deadline) statusCode := resp.StatusCode() redirectsCount := 0 - for err == nil && (statusCode == fasthttp.StatusMovedPermanently || statusCode == fasthttp.StatusFound) { + for err == nil && isStatusRedirect(statusCode) { if redirectsCount > 5 { err = fmt.Errorf("too many redirects") break