Skip to content

Commit 595eb4e

Browse files
committed
Refine redirect limit handling
1 parent 38b741e commit 595eb4e

1 file changed

Lines changed: 6 additions & 28 deletions

File tree

client/transport.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -271,35 +271,9 @@ type redirectClient interface {
271271
func doRedirectsWithClient(req *fasthttp.Request, resp *fasthttp.Response, maxRedirects int, client redirectClient) error {
272272
currentURL := req.URI().String()
273273
redirects := 0
274+
originalLimit := maxRedirects
274275

275-
if maxRedirects <= 0 {
276-
req.SetRequestURI(currentURL)
277-
if err := client.Do(req, resp); err != nil {
278-
return err
279-
}
280-
281-
statusCode := resp.Header.StatusCode()
282-
if !fasthttp.StatusCodeIsRedirect(statusCode) || maxRedirects == 0 {
283-
return nil
284-
}
285-
286-
location := resp.Header.Peek("Location")
287-
if len(location) == 0 {
288-
return fasthttp.ErrMissingLocation
289-
}
290-
291-
nextURL, err := composeRedirectURL(currentURL, location, req.DisableRedirectPathNormalizing)
292-
if err != nil {
293-
return err
294-
}
295-
currentURL = nextURL
296-
redirects = 1
297-
298-
if req.Header.IsPost() && (statusCode == fasthttp.StatusMovedPermanently || statusCode == fasthttp.StatusFound) {
299-
req.Header.SetMethod(fasthttp.MethodGet)
300-
req.SetBody(nil)
301-
}
302-
276+
if maxRedirects < 0 {
303277
maxRedirects = defaultRedirectLimit
304278
}
305279

@@ -315,6 +289,10 @@ func doRedirectsWithClient(req *fasthttp.Request, resp *fasthttp.Response, maxRe
315289
return nil
316290
}
317291

292+
if originalLimit == 0 {
293+
return nil
294+
}
295+
318296
redirects++
319297
if redirects > maxRedirects {
320298
return fasthttp.ErrTooManyRedirects

0 commit comments

Comments
 (0)