Go: add context (#17314)

### Summary

As title.

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-24 16:47:12 +08:00
committed by GitHub
parent 008fa3e10e
commit bdfc3ada41
78 changed files with 1031 additions and 904 deletions

View File

@@ -17,6 +17,7 @@
package utility
import (
"context"
"fmt"
"html"
"io"
@@ -37,7 +38,7 @@ var (
// FetchRemoteFileSafely downloads rawURL with SSRF protection, connect/overall
// timeouts, and a hard size cap that rejects (rather than truncates) oversized
// bodies.
func FetchRemoteFileSafely(rawURL string, maxSize int64) ([]byte, http.Header, string, error) {
func FetchRemoteFileSafely(ctx context.Context, rawURL string, maxSize int64) ([]byte, http.Header, string, error) {
currentURL := rawURL
for redirects := 0; redirects < 10; redirects++ {
hostname, resolvedIP, err := AssertURLSafe(currentURL)

View File

@@ -55,7 +55,8 @@ func TestFetchRemoteFileSafely_PDFAddsExtension(t *testing.T) {
PinnedHTTPClient = origPinned
})
data, headers, _, err := FetchRemoteFileSafely(server.URL+"/report", 100<<20)
ctx := t.Context()
data, headers, _, err := FetchRemoteFileSafely(ctx, server.URL+"/report", 100<<20)
if err != nil {
t.Fatalf("FetchRemoteFileSafely failed: %v", err)
}
@@ -87,7 +88,8 @@ func TestFetchRemoteFileSafely_ReturnsContentAndHeaders(t *testing.T) {
PinnedHTTPClient = origPinned
})
data, headers, finalURL, err := FetchRemoteFileSafely(server.URL+"/page", 100<<20)
ctx := t.Context()
data, headers, finalURL, err := FetchRemoteFileSafely(ctx, server.URL+"/page", 100<<20)
if err != nil {
t.Fatalf("FetchRemoteFileSafely failed: %v", err)
}