Skip to content

Commit f42ff95

Browse files
Refactor http client properly (#481)
Signed-off-by: Adrian Cole <[email protected]>
1 parent f8c7edc commit f42ff95

File tree

7 files changed

+18
-16
lines changed

7 files changed

+18
-16
lines changed

internal/envoy/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
)
1414

1515
// httpGet adds the userAgent header to the request, so that we can tell what is a dev build vs release.
16-
func httpGet(ctx context.Context, url string, p version.Platform, v string) (*http.Response, error) {
16+
func httpGet(ctx context.Context, client *http.Client, url string, p version.Platform, v string) (*http.Response, error) {
1717
// #nosec -> url can be anywhere by design
1818
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
1919
if err != nil {
2020
return nil, err
2121
}
2222
req.Header.Add("User-Agent", userAgent(p, v))
23-
return http.DefaultClient.Do(req)
23+
return client.Do(req)
2424
}
2525

2626
// userAgent returns the 'User-Agent' header value used in HTTP requests. This is useful in log, metrics, analytics, or

internal/envoy/http_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package envoy
55

66
import (
7-
"context"
87
"net/http"
98
"net/http/httptest"
109
"testing"
@@ -22,7 +21,7 @@ func TestHttpGet_AddsDefaultHeaders(t *testing.T) {
2221
}))
2322
defer ts.Close()
2423

25-
res, err := httpGet(context.Background(), ts.URL, globals.DefaultPlatform, "dev")
24+
res, err := httpGet(t.Context(), ts.Client(), ts.URL, globals.DefaultPlatform, "dev")
2625
require.NoError(t, err)
2726

2827
defer res.Body.Close() //nolint:errcheck

internal/envoy/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func verifyEnvoy(installPath string) (string, error) {
8282
func untarEnvoy(ctx context.Context, dst string, src version.TarballURL, // dst, src order like io.Copy
8383
sha256Sum version.SHA256Sum, p version.Platform, v string,
8484
) error {
85-
res, err := httpGet(ctx, string(src), p, v)
85+
res, err := httpGet(ctx, http.DefaultClient, string(src), p, v)
8686
if err != nil {
8787
return err
8888
}

internal/envoy/runtime.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net"
11+
"net/http"
1112
"os"
1213
"os/exec"
1314
"path/filepath"
@@ -48,9 +49,11 @@ const (
4849
// opts allows a user running envoy to control the working directory by ID or path, allowing explicit cleanup.
4950
func NewRuntime(opts *globals.RunOpts, logf LogFunc) *Runtime {
5051
safeHook := &safeStartupHook{
51-
delegate: collectConfigDump,
52-
logf: logf,
53-
timeout: 3 * time.Second,
52+
delegate: func(ctx context.Context, runDir, adminAddress string) error {
53+
return collectConfigDump(ctx, http.DefaultClient, runDir, adminAddress)
54+
},
55+
logf: logf,
56+
timeout: 3 * time.Second,
5457
}
5558
return &Runtime{o: opts, logf: logf, startupHook: safeHook.Hook}
5659
}

internal/envoy/startup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ func (s *safeStartupHook) Hook(ctx context.Context, runDir, adminAddress string)
5252
// - Endpoints (EDS)
5353
// - Secrets (SDS)
5454
// This provides a comprehensive snapshot of Envoy's dynamic configuration state.
55-
func collectConfigDump(ctx context.Context, runDir, adminAddress string) error {
55+
func collectConfigDump(ctx context.Context, client *http.Client, runDir, adminAddress string) error {
5656
url := fmt.Sprintf("http://%s/config_dump?include_eds", adminAddress)
5757
file := filepath.Join(runDir, "config_dump.json")
58-
return copyURLToFile(ctx, url, file)
58+
return copyURLToFile(ctx, client, url, file)
5959
}
6060

61-
func copyURLToFile(ctx context.Context, url, fullPath string) error {
61+
func copyURLToFile(ctx context.Context, client *http.Client, url, fullPath string) error {
6262
// #nosec -> runDir is allowed to be anywhere
6363
f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_WRONLY, 0o600)
6464
if err != nil {
@@ -71,7 +71,7 @@ func copyURLToFile(ctx context.Context, url, fullPath string) error {
7171
if err != nil {
7272
return fmt.Errorf("could not create request %v: %w", url, err)
7373
}
74-
res, err := http.DefaultClient.Do(req)
74+
res, err := client.Do(req)
7575
if err != nil {
7676
return fmt.Errorf("could not read %v: %w", url, err)
7777
}

internal/envoy/startup_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestCollectConfigDump(t *testing.T) {
133133
ctx, cancel = context.WithTimeout(ctx, 3*time.Second)
134134
defer cancel()
135135
}
136-
err := collectConfigDump(ctx, tempDir, adminAddress)
136+
err := collectConfigDump(ctx, ts.Client(), tempDir, adminAddress)
137137

138138
if tt.wantErr != "" {
139139
require.Error(t, err)
@@ -211,7 +211,7 @@ func TestCopyURLToFile(t *testing.T) {
211211
filePath = "/invalid\x00path/test.txt"
212212
}
213213

214-
err := copyURLToFile(ctx, ts.URL, filePath)
214+
err := copyURLToFile(ctx, ts.Client(), ts.URL, filePath)
215215

216216
if tt.wantErr != "" {
217217
require.Error(t, err)
@@ -236,7 +236,7 @@ func TestCopyURLToFile_InvalidURL(t *testing.T) {
236236
filePath := filepath.Join(tempDir, "test.txt")
237237

238238
// Test with invalid URL
239-
err := copyURLToFile(t.Context(), "://invalid-url", filePath)
239+
err := copyURLToFile(t.Context(), http.DefaultClient, "://invalid-url", filePath)
240240
require.Error(t, err)
241241
require.Contains(t, err.Error(), "could not create request")
242242
}

internal/envoy/versions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func NewGetVersions(envoyVersionsURL string, p version.Platform, v string) version.GetReleaseVersions {
1919
return func(ctx context.Context) (*version.ReleaseVersions, error) {
2020
// #nosec => This is by design, users can call out to wherever they like!
21-
resp, err := httpGet(ctx, envoyVersionsURL, p, v)
21+
resp, err := httpGet(ctx, http.DefaultClient, envoyVersionsURL, p, v)
2222
if err != nil {
2323
return nil, err
2424
}

0 commit comments

Comments
 (0)