返回 DeepSeek-Reasonix
http.go
1 package installsource
2
3 import (
4 "context"
5 "io"
6 "net/http"
7 "time"
8 )
9
10 // defaultFetchTimeout caps the lifetime of a single HTTP fetch. Without it a
11 // slow CDN can hold the agent tool call open until the user gives up. The
12 // value is generous (30s) so large SKILL.md bodies still load, but bounded so
13 // a hung server is not an open-ended wait.
14 const defaultFetchTimeout = 30 * time.Second
15
16 // defaultFetchLimit is the maximum body size we will accept from a remote
17 // manifest. SKILL.md / .mcp.json files are normally a few KB; 2 MiB is a
18 // safety cap that prevents an untrusted mirror from streaming gigabytes into
19 // our parser.
20 const defaultFetchLimit = 2 << 20
21
22 // fetchText performs a bounded GET on sourceURL using the tool's HTTP client.
23 // It applies defaultFetchTimeout unless the caller's context already has a
24 // tighter deadline, and never reads more than defaultFetchLimit bytes.
25 func (t *installSourceTool) fetchText(ctx context.Context, sourceURL string) (string, error) {
26 if _, hasDeadline := ctx.Deadline(); !hasDeadline {
27 var cancel context.CancelFunc
28 ctx, cancel = context.WithTimeout(ctx, defaultFetchTimeout)
29 defer cancel()
30 }
31 req, err := http.NewRequestWithContext(ctx, http.MethodGet, sourceURL, nil)
32 if err != nil {
33 return "", newErr(ErrSourceUnreadable, "%s: %v", sourceURL, err)
34 }
35 if req.Header.Get("User-Agent") == "" {
36 req.Header.Set("User-Agent", "reasonix-install/1.0")
37 }
38 resp, err := t.httpClient.Do(req)
39 if err != nil {
40 return "", newErr(ErrSourceUnreadable, "%s: %v", sourceURL, err)
41 }
42 defer resp.Body.Close()
43 if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
44 return "", newErr(ErrAuthRequired, "%s: HTTP %d", sourceURL, resp.StatusCode)
45 }
46 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
47 return "", newErr(ErrSourceUnreadable, "%s: HTTP %d", sourceURL, resp.StatusCode)
48 }
49 limited := io.LimitReader(resp.Body, defaultFetchLimit)
50 body, err := io.ReadAll(limited)
51 if err != nil {
52 return "", newErr(ErrSourceUnreadable, "%s: read body: %v", sourceURL, err)
53 }
54 return string(body), nil
55 }
56
56 lines GO