...

Source file src/oras.land/oras-go/pkg/registry/remote/utils.go

Documentation: oras.land/oras-go/pkg/registry/remote

     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  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  // defaultMaxMetadataBytes specifies the default limit on how many response
    30  // bytes are allowed in the server's response to the metadata APIs.
    31  // See also: Repository.MaxMetadataBytes
    32  var defaultMaxMetadataBytes int64 = 4 * 1024 * 1024 // 4 MiB
    33  
    34  // errNoLink is returned by parseLink() when no Link header is present.
    35  var errNoLink = errors.New("no Link header in response")
    36  
    37  // parseLink returns the URL of the response's "Link" header, if present.
    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  // limitReader returns a Reader that reads from r but stops with EOF after n
    60  // bytes. If n is zero, defaultMaxMetadataBytes is used.
    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  // withScopeHint adds a hinted scope to the context.
    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