| 1 | /* |
| 2 | Copyright The ORAS Authors. |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package remotescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/ |
| 17 | |
| 18 | import ( |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 25 | |
| 26 | ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/ "github.com/opencontainers/image-spec/specs-go/v1scip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/" |
| 27 | "oras.land/oras-go/v2/contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/" |
| 28 | "oras.land/oras-go/v2/errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/" |
| 29 | ) |
| 30 | |
| 31 | // defaultMaxMetadataBytes specifies the default limit on how many response |
| 32 | // bytes are allowed in the server's response to the metadata APIs. |
| 33 | // See also: Repository.MaxMetadataBytes |
| 34 | var defaultMaxMetadataBytesscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/defaultMaxMetadataBytes. int64 = 4 * 1024 * 1024 // 4 MiB |
| 35 | |
| 36 | // errNoLink is returned by parseLink() when no Link header is present. |
| 37 | var errNoLinkscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/errNoLink. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("no Link header in response") |
| 38 | |
| 39 | // parseLink returns the URL of the response's "Link" header, if present. |
| 40 | func parseLinkscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/parseLink().(resp *httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#) (string, error) { |
| 41 | link := resp.Headerscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Header#Get().("Link") |
| 42 | if link == "" { |
| 43 | return "", errNoLinkscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/errNoLink. |
| 44 | } |
| 45 | if link[0] != '<' { |
| 46 | return "", fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("invalid next link %q: missing '<'", link) |
| 47 | } |
| 48 | if i := stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.23.0 strings/IndexByte().(link, '>'); i == -1 { |
| 49 | return "", fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("invalid next link %q: missing '>'", link) |
| 50 | } else { |
| 51 | link = link[1:i] |
| 52 | } |
| 53 | |
| 54 | linkURL, err := resp.Requestscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Request..URLscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Request#URL..Parsescip-go gomod github.com/golang/go/src go1.23.0 `net/url`/URL#Parse().(link) |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | return linkURL.Stringscip-go gomod github.com/golang/go/src go1.23.0 `net/url`/URL#String().(), nil |
| 59 | } |
| 60 | |
| 61 | // limitReader returns a Reader that reads from r but stops with EOF after n |
| 62 | // bytes. If n is less than or equal to zero, defaultMaxMetadataBytes is used. |
| 63 | func limitReaderscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/limitReader().(r ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader#, n int64) ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader# { |
| 64 | if n <= 0 { |
| 65 | n = defaultMaxMetadataBytesscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/defaultMaxMetadataBytes. |
| 66 | } |
| 67 | return ioscip-go gomod github.com/golang/go/src go1.23.0 io/.LimitReaderscip-go gomod github.com/golang/go/src go1.23.0 io/LimitReader().(r, n) |
| 68 | } |
| 69 | |
| 70 | // limitSize returns ErrSizeExceedsLimit if the size of desc exceeds the limit n. |
| 71 | // If n is less than or equal to zero, defaultMaxMetadataBytes is used. |
| 72 | func limitSizescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/limitSize().(desc ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#, n int64) error { |
| 73 | if n <= 0 { |
| 74 | n = defaultMaxMetadataBytesscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/defaultMaxMetadataBytes. |
| 75 | } |
| 76 | if desc.Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size. > n { |
| 77 | return fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().( |
| 78 | "content size %v exceeds MaxMetadataBytes %v: %w", |
| 79 | desc.Sizescip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#Size., |
| 80 | n, |
| 81 | errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrSizeExceedsLimitscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrSizeExceedsLimit.) |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // decodeJSON safely reads the JSON content described by desc, and |
| 87 | // decodes it into v. |
| 88 | func decodeJSONscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote`/decodeJSON().(r ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader#, desc ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#, v any) error { |
| 89 | jsonBytes, err := contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.ReadAllscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadAll().(r, desc) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | return jsonscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/Unmarshal().(jsonBytes, v) |
| 94 | } |