mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-26 01:52:16 +08:00
Go: add context (#17314)
### Summary As title. --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user