Use status code constants

This commit is contained in:
Ben Adrian Sarmiento
2024-06-10 17:17:40 +02:00
parent 0fde9a6856
commit 59c15ebb0a
3 changed files with 9 additions and 8 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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