-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
97 lines (86 loc) · 2.73 KB
/
Copy pathhttp.go
File metadata and controls
97 lines (86 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package debuginfod
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"go.kacmar.sk/debuginfod/key"
)
// upstream fetches artifacts from a single debuginfod server.
// 200 returns the response body, 401/403 produce ErrAuthRequired, other 4xx produce ErrNotFound, 5xx/429/transport failures bubble up as retryable errors.
type upstream struct {
serverURL string
httpClient *http.Client
userAgent string
}
func (s *upstream) Fetch(ctx context.Context, k key.Key) (io.ReadCloser, error) {
endpoint := s.serverURL + buildURLPath(k)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", s.serverURL, err)
}
req.Header.Set("User-Agent", s.userAgent)
resp, err := s.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: %w", s.serverURL, err)
}
if resp.StatusCode != http.StatusOK {
_ = resp.Body.Close()
if resp.StatusCode >= 500 || resp.StatusCode == http.StatusTooManyRequests {
return nil, fmt.Errorf("%s: server returned %d", s.serverURL, resp.StatusCode)
}
sentinel := ErrNotFound
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
sentinel = ErrAuthRequired
}
return nil, fmt.Errorf("%s: server returned %d: %w", s.serverURL, resp.StatusCode, sentinel)
}
return resp.Body, nil
}
// buildURLPath returns the debuginfod URL path for k. It assumes k is already validated.
func buildURLPath(k key.Key) string {
base := "/buildid/" + k.BuildID() + "/" + k.Kind().String()
switch k.Kind() {
case key.KindSource:
return base + escapeSourcePath(k.Qualifier())
case key.KindSection:
return base + "/" + url.PathEscape(k.Qualifier())
}
return base
}
func escapeSourcePath(path string) string {
parts := strings.Split(path, "/")
for i, segment := range parts {
parts[i] = url.PathEscape(segment)
}
return strings.Join(parts, "/")
}
func normalizeServerURLs(urls []string) ([]string, error) {
seen := make(map[string]struct{}, len(urls))
out := make([]string, 0, len(urls))
for _, raw := range urls {
parsed, err := url.Parse(raw)
if err != nil {
return nil, fmt.Errorf("debuginfod: invalid server URL %q: %w", raw, err)
}
scheme := strings.ToLower(parsed.Scheme)
if scheme != "http" && scheme != "https" {
return nil, fmt.Errorf("debuginfod: server URL %q must use http or https", raw)
}
if parsed.Host == "" {
return nil, fmt.Errorf("debuginfod: server URL %q has no host", raw)
}
parsed.Scheme = scheme
parsed.Host = strings.ToLower(parsed.Host)
parsed.Path = strings.TrimRight(parsed.Path, "/")
normalized := parsed.String()
if _, ok := seen[normalized]; ok {
continue
}
seen[normalized] = struct{}{}
out = append(out, normalized)
}
return out, nil
}