Repository navigation
Expand file tree
/
Copy pathhttp.go
More file actions
115 lines (103 loc) · 3.4 KB
/
Copy pathhttp.go
File metadata and controls
115 lines (103 loc) · 3.4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package debuginfod
import (
"context"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"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, Metadata, error) {
endpoint := s.serverURL + buildURLPath(k)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, Metadata{}, 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, Metadata{}, 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, Metadata{}, 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, Metadata{}, fmt.Errorf("%s: server returned %d: %w", s.serverURL, resp.StatusCode, sentinel)
}
return resp.Body, parseMetadata(resp.Header), nil
}
// parseMetadata extracts the X-DEBUGINFOD-* response headers into a Metadata.
// Parsing is best-effort: malformed values leave their field zero rather than failing the fetch.
func parseMetadata(h http.Header) Metadata {
meta := Metadata{
File: h.Get("X-DEBUGINFOD-FILE"),
Archive: h.Get("X-DEBUGINFOD-ARCHIVE"),
}
if size, err := strconv.ParseInt(h.Get("X-DEBUGINFOD-SIZE"), 10, 64); err == nil {
meta.Size = size
}
if sig, err := hex.DecodeString(h.Get("X-DEBUGINFOD-IMASIGNATURE")); err == nil && len(sig) > 0 {
meta.IMASignature = sig
}
return meta
}
// 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
}