Fix retries on req body reads

This commit is contained in:
Ben Sarmiento
2024-02-17 06:34:36 +01:00
parent b87d99b1e6
commit 46e07f71c4
4 changed files with 27 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
@@ -144,8 +145,15 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
attempt := 0
var origBody []byte
if req.Method == "POST" {
origBody, _ = io.ReadAll(req.Body)
}
for {
if origBody != nil {
req.Body = io.NopCloser(bytes.NewReader(origBody))
}
if resp != nil && resp.Body != nil {
resp.Body.Close()
}

View File

@@ -29,9 +29,7 @@ func NewRealDebrid(client, unrestrictClient *zurghttp.HTTPClient, log *logutil.L
func (rd *RealDebrid) UnrestrictCheck(link string) (*Download, error) {
data := url.Values{}
data.Set("link", link)
bodyReader := strings.NewReader(data.Encode())
requestBody := io.NopCloser(bodyReader)
requestBody := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/check", requestBody)
if err != nil {
@@ -67,9 +65,7 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*Download, error) {
func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Download, error) {
data := url.Values{}
data.Set("link", link)
bodyReader := strings.NewReader(data.Encode())
requestBody := io.NopCloser(bodyReader)
requestBody := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/link", requestBody)
if err != nil {
@@ -218,9 +214,7 @@ func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) {
func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
data := url.Values{}
data.Set("files", files)
bodyReader := strings.NewReader(data.Encode())
requestBody := io.NopCloser(bodyReader)
requestBody := strings.NewReader(data.Encode())
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id)
req, err := http.NewRequest("POST", reqURL, requestBody)
@@ -269,9 +263,7 @@ func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
// Prepare request data
data := url.Values{}
data.Set("magnet", fmt.Sprintf("magnet:?xt=urn:btih:%s", magnet))
bodyReader := strings.NewReader(data.Encode())
requestBody := io.NopCloser(bodyReader)
requestBody := strings.NewReader(data.Encode())
// Construct request URL
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet"