...
1
15 package remote
16
17 import (
18 "context"
19 "errors"
20 "fmt"
21 "io"
22 "net/http"
23 "strings"
24
25 "oras.land/oras-go/pkg/registry"
26 "oras.land/oras-go/pkg/registry/remote/auth"
27 )
28
29
30
31
32 var defaultMaxMetadataBytes int64 = 4 * 1024 * 1024
33
34
35 var errNoLink = errors.New("no Link header in response")
36
37
38 func parseLink(resp *http.Response) (string, error) {
39 link := resp.Header.Get("Link")
40 if link == "" {
41 return "", errNoLink
42 }
43 if link[0] != '<' {
44 return "", fmt.Errorf("invalid next link %q: missing '<'", link)
45 }
46 if i := strings.IndexByte(link, '>'); i == -1 {
47 return "", fmt.Errorf("invalid next link %q: missing '>'", link)
48 } else {
49 link = link[1:i]
50 }
51
52 linkURL, err := resp.Request.URL.Parse(link)
53 if err != nil {
54 return "", err
55 }
56 return linkURL.String(), nil
57 }
58
59
60
61 func limitReader(r io.Reader, n int64) io.Reader {
62 if n == 0 {
63 n = defaultMaxMetadataBytes
64 }
65 return io.LimitReader(r, n)
66 }
67
68
69 func withScopeHint(ctx context.Context, ref registry.Reference, actions ...string) context.Context {
70 scope := auth.ScopeRepository(ref.Repository, actions...)
71 return auth.AppendScopes(ctx, scope)
72 }
73
View as plain text