From 897b213468d8313259112ff72a6d29766093a345 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Tue, 20 Feb 2024 15:34:47 -0800 Subject: [PATCH] use http.DefaultClient (#2530) default client already handles proxy --- api/client.go | 25 ++++--------------------- app/lifecycle/updater.go | 23 ++++------------------- server/images.go | 13 +------------ 3 files changed, 9 insertions(+), 52 deletions(-) diff --git a/api/client.go b/api/client.go index 4fe8668a..36019206 100644 --- a/api/client.go +++ b/api/client.go @@ -21,7 +21,7 @@ import ( type Client struct { base *url.URL - http http.Client + http *http.Client } func checkError(resp *http.Response, body []byte) error { @@ -66,30 +66,13 @@ func ClientFromEnvironment() (*Client, error) { } } - client := Client{ + return &Client{ base: &url.URL{ Scheme: scheme, Host: net.JoinHostPort(host, port), }, - } - - mockRequest, err := http.NewRequest(http.MethodHead, client.base.String(), nil) - if err != nil { - return nil, err - } - - proxyURL, err := http.ProxyFromEnvironment(mockRequest) - if err != nil { - return nil, err - } - - client.http = http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(proxyURL), - }, - } - - return &client, nil + http: http.DefaultClient, + }, nil } func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error { diff --git a/app/lifecycle/updater.go b/app/lifecycle/updater.go index da69c327..e0d23299 100644 --- a/app/lifecycle/updater.go +++ b/app/lifecycle/updater.go @@ -34,20 +34,6 @@ type UpdateResponse struct { UpdateVersion string `json:"version"` } -func getClient(req *http.Request) http.Client { - proxyURL, err := http.ProxyFromEnvironment(req) - if err != nil { - slog.Warn(fmt.Sprintf("failed to handle proxy: %s", err)) - return http.Client{} - } - - return http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(proxyURL), - }, - } -} - func IsNewReleaseAvailable(ctx context.Context) (bool, UpdateResponse) { var updateResp UpdateResponse @@ -83,10 +69,9 @@ func IsNewReleaseAvailable(ctx context.Context) (bool, UpdateResponse) { } req.Header.Set("Authorization", signature) req.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version())) - client := getClient(req) slog.Debug("checking for available update", "requestURL", requestURL) - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { slog.Warn(fmt.Sprintf("failed to check for update: %s", err)) return false, updateResp @@ -119,8 +104,8 @@ func DownloadNewRelease(ctx context.Context, updateResp UpdateResponse) error { if err != nil { return err } - client := getClient(req) - resp, err := client.Do(req) + + resp, err := http.DefaultClient.Do(req) if err != nil { return fmt.Errorf("error checking update: %w", err) } @@ -151,7 +136,7 @@ func DownloadNewRelease(ctx context.Context, updateResp UpdateResponse) error { cleanupOldDownloads() req.Method = http.MethodGet - resp, err = client.Do(req) + resp, err = http.DefaultClient.Do(req) if err != nil { return fmt.Errorf("error checking update: %w", err) } diff --git a/server/images.go b/server/images.go index 391809cb..a000c5c5 100644 --- a/server/images.go +++ b/server/images.go @@ -1103,18 +1103,7 @@ func makeRequest(ctx context.Context, method string, requestURL *url.URL, header req.ContentLength = contentLength } - proxyURL, err := http.ProxyFromEnvironment(req) - if err != nil { - return nil, err - } - - client := http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(proxyURL), - }, - } - - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err }