chore (refactoring): http client utilities

This commit is contained in:
MickaelK
2026-03-30 17:46:57 +11:00
parent bb9e3fbc38
commit cef0367536
12 changed files with 50 additions and 46 deletions
+20 -4
View File
@@ -16,9 +16,18 @@ func init() {
}
}
var HTTPClient = http.Client{
Timeout: 5 * time.Hour,
Transport: NewTransformedTransport(&http.Transport{
type HTTPClientOption func(*http.Transport)
func WithInsecure(t *http.Transport) {
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
func WithoutTimeout(t *http.Transport) {
t.ResponseHeaderTimeout = 0
}
func HTTPClient(opts ...HTTPClientOption) *http.Client {
t := &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
@@ -26,7 +35,14 @@ var HTTPClient = http.Client{
TLSHandshakeTimeout: 5 * time.Second,
IdleConnTimeout: 60 * time.Second,
ResponseHeaderTimeout: 60 * time.Second,
}),
}
for _, opt := range opts {
opt(t)
}
return &http.Client{
Timeout: 5 * time.Hour,
Transport: NewTransformedTransport(t),
}
}
var HTTP = http.Client{
+2 -1
View File
@@ -1,5 +1,6 @@
package sdk
type Filestash struct {
Token string
Token string
Insecure bool
}
+6 -6
View File
@@ -26,12 +26,12 @@ func (this Filestash) request(method string, url string, body io.Reader) (io.Rea
req.Host = Config.Get("general.host").String()
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", this.Token))
req.Header.Set("X-Requested-With", "XmlHttpRequest")
t := HTTPClient.Transport.(*TransformedTransport).Orig.(*http.Transport).Clone()
t.ResponseHeaderTimeout = 0
resp, err := (&http.Client{
Timeout: 0,
Transport: NewTransformedTransport(t),
}).Do(req)
opts := []HTTPClientOption{WithoutTimeout}
if this.Insecure {
opts = append(opts, WithInsecure)
}
resp, err := HTTPClient(opts...).Do(req)
if err != nil {
return nil, nil, err
}
@@ -88,7 +88,7 @@ func (this Wordpress) Callback(formData map[string]string, idpParams map[string]
return nil, NewError("Failed to create request: "+err.Error(), 500)
}
req.Header.Set("Content-Type", "text/xml")
resp, err := HTTPClient.Do(req)
resp, err := HTTPClient().Do(req)
if err != nil {
return nil, err
}
@@ -157,7 +157,7 @@ func (this ArtifactoryStorage) Ls(path string) ([]os.FileInfo, error) {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return nil, err
}
@@ -189,7 +189,7 @@ func (this ArtifactoryStorage) Cat(path string) (io.ReadCloser, error) {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return nil, err
}
@@ -231,7 +231,7 @@ func (this ArtifactoryStorage) Cat(path string) (io.ReadCloser, error) {
}
req, err = http.NewRequest("GET", artifactoryResponse.DownloadLink, nil)
req.Header.Add("Authorization", "Bearer "+this.token)
res, err = HTTPClient.Do(req)
res, err = HTTPClient().Do(req)
return res.Body, err
}
@@ -268,7 +268,7 @@ func (this ArtifactoryStorage) Mkdir(path string) error {
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return err
}
@@ -306,7 +306,7 @@ func (this ArtifactoryStorage) Rm(path string) error {
return err
}
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return err
}
@@ -335,7 +335,7 @@ func (this ArtifactoryStorage) Mv(from, to string) error {
return err
}
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return err
}
@@ -366,7 +366,7 @@ func (this ArtifactoryStorage) Save(path string, content io.Reader) error {
return err
}
req.Header.Add("Authorization", "Bearer "+this.token)
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return err
}
+1 -1
View File
@@ -291,7 +291,7 @@ func (this *AzureBlob) Mv(from string, to string) error {
// CASE 2: Move a directory
pager := this.client.NewListBlobsFlatPager(apFrom.containerName, &container.ListBlobsFlatOptions{
Include: container.ListBlobsInclude{Snapshots: true, Versions: true},
Prefix: &apFrom.blobName,
Prefix: &apFrom.blobName,
})
for pager.More() {
resp, err := pager.NextPage(this.ctx)
+1 -1
View File
@@ -526,7 +526,7 @@ func (this Backblaze) request(method string, url string, body io.Reader, fn func
defer req.Body.Close()
}
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return res, err
}
+1 -1
View File
@@ -376,7 +376,7 @@ func (this Dav) request(method string, url string, body io.Reader, fn func(req *
if fn != nil {
fn(req)
}
res, err := HTTPClient.Do(req)
res, err := HTTPClient().Do(req)
if err != nil {
return nil, err
} else if res.StatusCode > 400 {
+1 -1
View File
@@ -215,7 +215,7 @@ func (d Dropbox) request(method string, url string, body io.Reader, fn func(*htt
if req.Body != nil {
defer req.Body.Close()
}
return HTTPClient.Do(req)
return HTTPClient().Do(req)
}
func (d Dropbox) toReader(a interface{}) io.Reader {
+2 -2
View File
@@ -220,7 +220,7 @@ func (this *Perkeep) query(searchRequest any) (*SearchResponse, error) {
if err != nil {
return nil, NewError("Failed to create request: "+err.Error(), 500)
}
resp, err := HTTPClient.Do(req)
resp, err := HTTPClient().Do(req)
if err != nil {
return nil, NewError("Failed to query perkeep: "+err.Error(), 500)
}
@@ -249,7 +249,7 @@ func (this *Perkeep) describe(blobRef string) (*DescribeResponse, error) {
if err != nil {
return nil, NewError("Failed to create request: "+err.Error(), 500)
}
resp, err := HTTPClient.Do(req)
resp, err := HTTPClient().Do(req)
if err != nil {
return nil, NewError("Failed to describe perkeep: "+err.Error(), 500)
}
+1 -15
View File
@@ -2,10 +2,8 @@ package plg_backend_url
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
@@ -272,19 +270,7 @@ func request(ctx context.Context, method string, url string, rangeHeader string)
if err != nil {
return nil, err
}
return (&http.Client{
Timeout: 5 * time.Hour,
Transport: NewTransformedTransport(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
IdleConnTimeout: 60 * time.Second,
ResponseHeaderTimeout: 60 * time.Second,
}),
}).Do(r)
return HTTPClient(WithInsecure).Do(r)
}
var extractASPNetList = extract(
+7 -6
View File
@@ -2,7 +2,6 @@ package plg_backend_webdav
import (
"encoding/xml"
. "github.com/mickael-kerjean/filestash/server/common"
"io"
"net/http"
"net/url"
@@ -11,6 +10,8 @@ import (
"regexp"
"strings"
"time"
. "github.com/mickael-kerjean/filestash/server/common"
)
type WebDav struct {
@@ -35,10 +36,10 @@ func (w WebDav) Init(params map[string]string, app *App) (IBackend, error) {
}
backend := WebDav{
params: &WebDavParams{
strings.ReplaceAll(params["url"], "%{username}", url.PathEscape(params["username"])),
params["username"],
params["password"],
params["path"],
url: strings.ReplaceAll(params["url"], "%{username}", url.PathEscape(params["username"])),
username: params["username"],
password: params["password"],
path: params["path"],
},
}
return backend, nil
@@ -283,7 +284,7 @@ func (w WebDav) request(method string, url string, body io.Reader, fn func(req *
if fn != nil {
fn(req)
}
return HTTPClient.Do(req)
return HTTPClient().Do(req)
}
type WebDavResp struct {