github.com/opcr-io/oras-go/v2@v2.0.0-20231122155130-eb4260d8a0ae/registry/remote/utils.go (about)

     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 remote
    17  
    18  import (
    19  	"encoding/json"
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"strings"
    25  
    26  	"github.com/opcr-io/oras-go/v2/content"
    27  	"github.com/opcr-io/oras-go/v2/errdef"
    28  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    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 defaultMaxMetadataBytes int64 = 4 * 1024 * 1024 // 4 MiB
    35  
    36  // errNoLink is returned by parseLink() when no Link header is present.
    37  var errNoLink = errors.New("no Link header in response")
    38  
    39  // parseLink returns the URL of the response's "Link" header, if present.
    40  func parseLink(resp *http.Response) (string, error) {
    41  	link := resp.Header.Get("Link")
    42  	if link == "" {
    43  		return "", errNoLink
    44  	}
    45  	if link[0] != '<' {
    46  		return "", fmt.Errorf("invalid next link %q: missing '<'", link)
    47  	}
    48  	if i := strings.IndexByte(link, '>'); i == -1 {
    49  		return "", fmt.Errorf("invalid next link %q: missing '>'", link)
    50  	} else {
    51  		link = link[1:i]
    52  	}
    53  
    54  	linkURL, err := resp.Request.URL.Parse(link)
    55  	if err != nil {
    56  		return "", err
    57  	}
    58  	return linkURL.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 limitReader(r io.Reader, n int64) io.Reader {
    64  	if n <= 0 {
    65  		n = defaultMaxMetadataBytes
    66  	}
    67  	return 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 limitSize(desc ocispec.Descriptor, n int64) error {
    73  	if n <= 0 {
    74  		n = defaultMaxMetadataBytes
    75  	}
    76  	if desc.Size > n {
    77  		return fmt.Errorf(
    78  			"content size %v exceeds MaxMetadataBytes %v: %w",
    79  			desc.Size,
    80  			n,
    81  			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 decodeJSON(r io.Reader, desc ocispec.Descriptor, v any) error {
    89  	jsonBytes, err := content.ReadAll(r, desc)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	return json.Unmarshal(jsonBytes, v)
    94  }