From 59c15ebb0aef5aac75d64d2e6309c14bd4a6b666 Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Mon, 10 Jun 2024 17:17:40 +0200 Subject: [PATCH] Use status code constants --- internal/universal/downloader.go | 2 +- pkg/http/client.go | 13 +++++++------ pkg/realdebrid/api.go | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index 33e7d03..f3eed3b 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -163,7 +163,7 @@ func (dl *Downloader) streamFileToResponse( defer downloadResp.Body.Close() // Check if the download was not successful - if downloadResp.StatusCode/100 != 2 { + if downloadResp.StatusCode != http.StatusOK && downloadResp.StatusCode != http.StatusPartialContent { log.Warnf("Received a %s status code for file %s", downloadResp.Status, unrestrict.Filename) if file != nil { if err := file.State.Event(context.Background(), "break_file"); err != nil { diff --git a/pkg/http/client.go b/pkg/http/client.go index 4a000ea..cfcaf9a 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -181,7 +181,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { resp, err = r.client.Do(req) // http 4xx and 5xx errors - if resp != nil && resp.StatusCode/100 >= 4 { + if resp != nil && resp.StatusCode >= http.StatusBadRequest { body, _ := io.ReadAll(resp.Body) if body != nil { var errResp ApiErrorResponse @@ -285,22 +285,23 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err return 1 } if resp != nil { - if resp.StatusCode == 429 { + if resp.StatusCode == http.StatusTooManyRequests { // Too many requests: retry infinitely, default: 4 secs time.Sleep(time.Duration(rateLimitSleep) * time.Second) return 0 } - if resp.StatusCode/100 == 4 { + if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError { // other client errors: retry return 1 } - if resp.StatusCode/100 == 5 { + if resp.StatusCode >= http.StatusInternalServerError { // server errors: don't retry return -1 } + okResponseCode := resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusPartialContent // if the request has a Range header but the server doesn't respond with a Content-Range header hasRangeHeader := req.Header.Get("Range") != "" && !strings.HasPrefix(req.Header.Get("Range"), "bytes=0-") - if hasRangeHeader && resp.StatusCode/100 == 2 && resp.Header.Get("Content-Range") == "" { + if hasRangeHeader && okResponseCode && resp.Header.Get("Content-Range") == "" { time.Sleep(10 * time.Millisecond) return 0 } @@ -331,7 +332,7 @@ func (r *HTTPClient) CanFetchFirstByte(url string) bool { resp, _ := r.Do(req) if resp != nil { defer resp.Body.Close() - return resp.StatusCode == 200 || resp.StatusCode == 206 + return resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusPartialContent } return false } diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 789c392..02d024e 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -452,7 +452,7 @@ func (rd *RealDebrid) fetchPageOfDownloads(page, limit int) ([]Download, int, er return downloads, 0, nil } - if resp.StatusCode < 200 || resp.StatusCode >= 300 { + if resp.StatusCode != http.StatusOK { err := fmt.Errorf("unexpected status code: %d", resp.StatusCode) rd.log.Errorf("Error when executing the get downloads request: %v", err) return nil, 0, err