2015-04-17 20:32:51 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-04-17 20:32:51 +00:00
|
|
|
"encoding/json"
|
2015-08-21 04:50:15 +00:00
|
|
|
"errors"
|
2015-04-17 20:32:51 +00:00
|
|
|
"fmt"
|
2015-05-15 20:29:44 +00:00
|
|
|
"io"
|
2015-04-17 20:32:51 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
"path"
|
2015-04-17 20:32:51 +00:00
|
|
|
"strconv"
|
2016-06-28 21:44:51 +00:00
|
|
|
"strings"
|
2015-04-17 20:32:51 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/reference"
|
|
|
|
v2 "github.com/distribution/distribution/v3/registry/api/v2"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/transport"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/cache"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/cache/memory"
|
2016-12-17 00:28:34 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-04-17 20:32:51 +00:00
|
|
|
)
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
// Registry provides an interface for calling Repositories, which returns a catalog of repositories.
|
|
|
|
type Registry interface {
|
|
|
|
Repositories(ctx context.Context, repos []string, last string) (n int, err error)
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:57:58 +00:00
|
|
|
// checkHTTPRedirect is a callback that can manipulate redirected HTTP
|
|
|
|
// requests. It is used to preserve Accept and Range headers.
|
|
|
|
func checkHTTPRedirect(req *http.Request, via []*http.Request) error {
|
|
|
|
if len(via) >= 10 {
|
|
|
|
return errors.New("stopped after 10 redirects")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(via) > 0 {
|
|
|
|
for headerName, headerVals := range via[0].Header {
|
2016-02-03 21:19:44 +00:00
|
|
|
if headerName != "Accept" && headerName != "Range" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, val := range headerVals {
|
|
|
|
// Don't add to redirected request if redirected
|
|
|
|
// request already has a header with the same
|
|
|
|
// name and value.
|
|
|
|
hasValue := false
|
|
|
|
for _, existingVal := range req.Header[headerName] {
|
|
|
|
if existingVal == val {
|
|
|
|
hasValue = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasValue {
|
2016-01-27 18:57:58 +00:00
|
|
|
req.Header.Add(headerName, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
// NewRegistry creates a registry namespace which can be used to get a listing of repositories
|
2017-09-02 15:37:34 +00:00
|
|
|
func NewRegistry(baseURL string, transport http.RoundTripper) (Registry, error) {
|
2016-02-23 01:49:23 +00:00
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL, false)
|
2015-07-22 22:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
2016-01-27 18:57:58 +00:00
|
|
|
Transport: transport,
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
CheckRedirect: checkHTTPRedirect,
|
2015-07-22 22:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry{
|
2017-09-05 20:14:43 +00:00
|
|
|
client: client,
|
|
|
|
ub: ub,
|
2015-07-22 22:18:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type registry struct {
|
2018-08-06 20:55:26 +00:00
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
2015-07-22 22:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repositories returns a lexigraphically sorted catalog given a base URL. The 'entries' slice will be filled up to the size
|
|
|
|
// of the slice, starting at the value provided in 'last'. The number of entries will be returned along with io.EOF if there
|
|
|
|
// are no more entries
|
|
|
|
func (r *registry) Repositories(ctx context.Context, entries []string, last string) (int, error) {
|
|
|
|
var numFilled int
|
|
|
|
var returnErr error
|
|
|
|
|
|
|
|
values := buildCatalogValues(len(entries), last)
|
|
|
|
u, err := r.ub.BuildCatalogURL(values)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
2020-02-25 11:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
resp, err := r.client.Do(req)
|
2015-07-22 22:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-07-22 22:18:03 +00:00
|
|
|
var ctlg struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&ctlg); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
copy(entries, ctlg.Repositories)
|
2015-07-22 22:18:03 +00:00
|
|
|
numFilled = len(ctlg.Repositories)
|
|
|
|
|
|
|
|
link := resp.Header.Get("Link")
|
|
|
|
if link == "" {
|
|
|
|
returnErr = io.EOF
|
|
|
|
}
|
2015-07-24 23:14:04 +00:00
|
|
|
} else {
|
2015-12-08 22:24:03 +00:00
|
|
|
return 0, HandleErrorResponse(resp)
|
2015-07-22 22:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return numFilled, returnErr
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// NewRepository creates a new Repository for the given repository name and base URL.
|
2017-09-02 15:37:34 +00:00
|
|
|
func NewRepository(name reference.Named, baseURL string, transport http.RoundTripper) (distribution.Repository, error) {
|
2016-02-23 01:49:23 +00:00
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL, false)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
client := &http.Client{
|
2016-01-27 18:57:58 +00:00
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: checkHTTPRedirect,
|
2015-05-14 16:54:23 +00:00
|
|
|
// TODO(dmcgowan): create cookie jar
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &repository{
|
2017-09-05 20:14:43 +00:00
|
|
|
client: client,
|
|
|
|
ub: ub,
|
|
|
|
name: name,
|
2015-04-17 20:32:51 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type repository struct {
|
2018-08-06 20:55:26 +00:00
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
name reference.Named
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:47:34 +00:00
|
|
|
func (r *repository) Named() reference.Named {
|
2015-04-17 20:32:51 +00:00
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
func (r *repository) Blobs(ctx context.Context) distribution.BlobStore {
|
|
|
|
statter := &blobStatter{
|
2015-12-15 22:35:23 +00:00
|
|
|
name: r.name,
|
2015-05-20 02:56:27 +00:00
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-05-15 23:25:00 +00:00
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
return &blobs{
|
2015-12-15 22:35:23 +00:00
|
|
|
name: r.name,
|
2015-05-20 02:56:27 +00:00
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2022-07-13 00:42:48 +00:00
|
|
|
statter: cache.NewCachedBlobStatter(memory.NewInMemoryBlobDescriptorCacheProvider(memory.UnlimitedSize), statter),
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:39:34 +00:00
|
|
|
func (r *repository) Manifests(ctx context.Context, options ...distribution.ManifestServiceOption) (distribution.ManifestService, error) {
|
|
|
|
// todo(richardscothern): options should be sent over the wire
|
2015-04-17 20:32:51 +00:00
|
|
|
return &manifests{
|
2015-12-15 22:35:23 +00:00
|
|
|
name: r.name,
|
2015-05-20 02:56:27 +00:00
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-07-14 23:25:37 +00:00
|
|
|
etags: make(map[string]string),
|
2015-06-15 17:39:34 +00:00
|
|
|
}, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (r *repository) Tags(ctx context.Context) distribution.TagService {
|
|
|
|
return &tags{
|
2017-09-05 20:14:43 +00:00
|
|
|
client: r.client,
|
|
|
|
ub: r.ub,
|
|
|
|
name: r.Named(),
|
2015-05-20 02:56:27 +00:00
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// tags implements remote tagging operations.
|
|
|
|
type tags struct {
|
2017-09-05 20:14:43 +00:00
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
name reference.Named
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// All returns all tags
|
|
|
|
func (t *tags) All(ctx context.Context) ([]string, error) {
|
|
|
|
var tags []string
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2017-11-19 05:50:22 +00:00
|
|
|
listURLStr, err := t.ub.BuildTagsURL(t.name)
|
|
|
|
if err != nil {
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listURL, err := url.Parse(listURLStr)
|
2015-05-09 00:40:30 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 21:44:51 +00:00
|
|
|
for {
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, listURL.String(), nil)
|
2020-02-25 11:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := t.client.Do(req)
|
2015-05-09 00:40:30 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2016-06-28 21:44:51 +00:00
|
|
|
defer resp.Body.Close()
|
2015-05-09 00:40:30 +00:00
|
|
|
|
2016-06-28 21:44:51 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2022-11-02 21:55:22 +00:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2016-06-28 21:44:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tagsResponse := struct {
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &tagsResponse); err != nil {
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
tags = append(tags, tagsResponse.Tags...)
|
|
|
|
if link := resp.Header.Get("Link"); link != "" {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
firsLink, _, _ := strings.Cut(link, ";")
|
|
|
|
linkURL, err := url.Parse(strings.Trim(firsLink, "<>"))
|
2017-11-19 05:50:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listURL = listURL.ResolveReference(linkURL)
|
2016-06-28 21:44:51 +00:00
|
|
|
} else {
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return tags, HandleErrorResponse(resp)
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 00:40:30 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func descriptorFromResponse(response *http.Response) (distribution.Descriptor, error) {
|
|
|
|
desc := distribution.Descriptor{}
|
|
|
|
headers := response.Header
|
|
|
|
|
|
|
|
ctHeader := headers.Get("Content-Type")
|
|
|
|
if ctHeader == "" {
|
|
|
|
return distribution.Descriptor{}, errors.New("missing or empty Content-Type header")
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
desc.MediaType = ctHeader
|
|
|
|
|
|
|
|
digestHeader := headers.Get("Docker-Content-Digest")
|
|
|
|
if digestHeader == "" {
|
2022-11-02 21:55:22 +00:00
|
|
|
data, err := io.ReadAll(response.Body)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2022-11-02 22:31:23 +00:00
|
|
|
_, desc, err := distribution.UnmarshalManifest(ctHeader, data)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
return desc, nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:07:42 +00:00
|
|
|
dgst, err := digest.Parse(digestHeader)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
desc.Digest = dgst
|
|
|
|
|
|
|
|
lengthHeader := headers.Get("Content-Length")
|
|
|
|
if lengthHeader == "" {
|
|
|
|
return distribution.Descriptor{}, errors.New("missing or empty Content-Length header")
|
|
|
|
}
|
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
desc.Size = length
|
|
|
|
|
|
|
|
return desc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a HEAD request for a Manifest against its named endpoint in order
|
|
|
|
// to construct a descriptor for the tag. If the registry doesn't support HEADing
|
|
|
|
// a manifest, fallback to GET.
|
|
|
|
func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithTag(t.name, tag)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
u, err := t.ub.BuildManifestURL(ref)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
2016-08-26 21:35:04 +00:00
|
|
|
newRequest := func(method string) (*http.Response, error) {
|
2020-02-25 11:40:15 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, u, nil)
|
2016-08-26 21:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-18 00:32:23 +00:00
|
|
|
|
2016-08-26 21:35:04 +00:00
|
|
|
for _, t := range distribution.ManifestMediaTypes() {
|
|
|
|
req.Header.Add("Accept", t)
|
|
|
|
}
|
|
|
|
resp, err := t.client.Do(req)
|
|
|
|
return resp, err
|
2016-02-18 00:32:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
resp, err := newRequest(http.MethodHead)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2016-03-16 18:46:40 +00:00
|
|
|
defer resp.Body.Close()
|
2015-08-21 04:50:15 +00:00
|
|
|
|
|
|
|
switch {
|
2017-08-25 21:15:10 +00:00
|
|
|
case resp.StatusCode >= 200 && resp.StatusCode < 400 && len(resp.Header.Get("Docker-Content-Digest")) > 0:
|
|
|
|
// if the response is a success AND a Docker-Content-Digest can be retrieved from the headers
|
2015-08-21 04:50:15 +00:00
|
|
|
return descriptorFromResponse(resp)
|
2016-08-26 21:35:04 +00:00
|
|
|
default:
|
|
|
|
// if the response is an error - there will be no body to decode.
|
|
|
|
// Issue a GET request:
|
|
|
|
// - for data from a server that does not handle HEAD
|
|
|
|
// - to get error details in case of a failure
|
2022-11-02 22:31:23 +00:00
|
|
|
resp, err = newRequest(http.MethodGet)
|
2016-02-18 00:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2016-08-26 21:35:04 +00:00
|
|
|
defer resp.Body.Close()
|
2016-02-18 00:32:23 +00:00
|
|
|
|
2016-08-26 21:35:04 +00:00
|
|
|
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
|
|
|
|
return descriptorFromResponse(resp)
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tags) Lookup(ctx context.Context, digest distribution.Descriptor) ([]string, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tags) Tag(ctx context.Context, tag string, desc distribution.Descriptor) error {
|
|
|
|
panic("not implemented")
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (t *tags) Untag(ctx context.Context, tag string) error {
|
2021-05-27 22:13:59 +00:00
|
|
|
ref, err := reference.WithTag(t.name, tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := t.ub.BuildManifestURL(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
2021-05-27 22:13:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := t.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if SuccessStatus(resp.StatusCode) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return HandleErrorResponse(resp)
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type manifests struct {
|
2015-12-15 22:35:23 +00:00
|
|
|
name reference.Named
|
2015-08-21 04:50:15 +00:00
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
|
|
|
etags map[string]string
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithDigest(ms.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
u, err := ms.ub.BuildManifestURL(ref)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil)
|
2020-02-25 11:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
resp, err := ms.client.Do(req)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
return true, nil
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if resp.StatusCode == http.StatusNotFound {
|
2015-04-17 20:32:51 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return false, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// AddEtagToTag allows a client to supply an eTag to Get which will be
|
2015-07-24 06:03:13 +00:00
|
|
|
// used for a conditional HTTP request. If the eTag matches, a nil manifest
|
2015-08-21 04:50:15 +00:00
|
|
|
// and ErrManifestNotModified error will be returned. etag is automatically
|
|
|
|
// quoted when added to this map.
|
2015-07-24 06:03:13 +00:00
|
|
|
func AddEtagToTag(tag, etag string) distribution.ManifestServiceOption {
|
2015-08-21 04:50:15 +00:00
|
|
|
return etagOption{tag, etag}
|
|
|
|
}
|
|
|
|
|
|
|
|
type etagOption struct{ tag, etag string }
|
|
|
|
|
|
|
|
func (o etagOption) Apply(ms distribution.ManifestService) error {
|
|
|
|
if ms, ok := ms.(*manifests); ok {
|
|
|
|
ms.etags[o.tag] = fmt.Sprintf(`"%s"`, o.etag)
|
|
|
|
return nil
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
return fmt.Errorf("etag options is a client-only option")
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 00:02:29 +00:00
|
|
|
// ReturnContentDigest allows a client to set a the content digest on
|
|
|
|
// a successful request from the 'Docker-Content-Digest' header. This
|
|
|
|
// returned digest is represents the digest which the registry uses
|
|
|
|
// to refer to the content and can be used to delete the content.
|
|
|
|
func ReturnContentDigest(dgst *digest.Digest) distribution.ManifestServiceOption {
|
|
|
|
return contentDigestOption{dgst}
|
|
|
|
}
|
|
|
|
|
|
|
|
type contentDigestOption struct{ digest *digest.Digest }
|
|
|
|
|
|
|
|
func (o contentDigestOption) Apply(ms distribution.ManifestService) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
var (
|
|
|
|
digestOrTag string
|
|
|
|
ref reference.Named
|
|
|
|
err error
|
2016-06-09 00:02:29 +00:00
|
|
|
contentDgst *digest.Digest
|
2017-09-20 01:25:37 +00:00
|
|
|
mediaTypes []string
|
2015-12-16 00:43:13 +00:00
|
|
|
)
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
for _, option := range options {
|
2017-09-20 01:25:37 +00:00
|
|
|
switch opt := option.(type) {
|
|
|
|
case distribution.WithTagOption:
|
2016-03-18 22:30:47 +00:00
|
|
|
digestOrTag = opt.Tag
|
|
|
|
ref, err = reference.WithTag(ms.name, opt.Tag)
|
2015-12-16 00:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-20 01:25:37 +00:00
|
|
|
case contentDigestOption:
|
2016-06-09 00:02:29 +00:00
|
|
|
contentDgst = opt.digest
|
2017-09-20 01:25:37 +00:00
|
|
|
case distribution.WithManifestMediaTypesOption:
|
|
|
|
mediaTypes = opt.MediaTypes
|
|
|
|
default:
|
2015-08-21 04:50:15 +00:00
|
|
|
err := option.Apply(ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 00:43:13 +00:00
|
|
|
if digestOrTag == "" {
|
|
|
|
digestOrTag = dgst.String()
|
|
|
|
ref, err = reference.WithDigest(ms.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 01:25:37 +00:00
|
|
|
if len(mediaTypes) == 0 {
|
|
|
|
mediaTypes = distribution.ManifestMediaTypes()
|
|
|
|
}
|
|
|
|
|
2015-12-16 00:43:13 +00:00
|
|
|
u, err := ms.ub.BuildManifestURL(ref)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
2015-07-14 23:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2017-09-20 01:25:37 +00:00
|
|
|
for _, t := range mediaTypes {
|
2015-08-21 04:50:15 +00:00
|
|
|
req.Header.Add("Accept", t)
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2015-12-16 00:43:13 +00:00
|
|
|
if _, ok := ms.etags[digestOrTag]; ok {
|
|
|
|
req.Header.Set("If-None-Match", ms.etags[digestOrTag])
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
resp, err := ms.client.Do(req)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-07-24 23:14:04 +00:00
|
|
|
if resp.StatusCode == http.StatusNotModified {
|
2015-09-18 18:00:44 +00:00
|
|
|
return nil, distribution.ErrManifestNotModified
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if SuccessStatus(resp.StatusCode) {
|
2016-06-09 00:02:29 +00:00
|
|
|
if contentDgst != nil {
|
2016-12-15 23:07:42 +00:00
|
|
|
dgst, err := digest.Parse(resp.Header.Get("Docker-Content-Digest"))
|
2016-06-09 00:02:29 +00:00
|
|
|
if err == nil {
|
|
|
|
*contentDgst = dgst
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
mt := resp.Header.Get("Content-Type")
|
2022-11-02 21:55:22 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m, _, err := distribution.UnmarshalManifest(mt, body)
|
|
|
|
if err != nil {
|
2015-04-17 20:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
return m, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return nil, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// Put puts a manifest. A tag can be specified using an options parameter which uses some shared state to hold the
|
2016-01-26 22:20:23 +00:00
|
|
|
// tag name in order to build the correct upload URL.
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Put(ctx context.Context, m distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref := ms.name
|
2016-01-26 22:20:23 +00:00
|
|
|
var tagged bool
|
2015-08-21 04:50:15 +00:00
|
|
|
|
|
|
|
for _, option := range options {
|
2016-03-18 22:30:47 +00:00
|
|
|
if opt, ok := option.(distribution.WithTagOption); ok {
|
2015-12-16 00:43:13 +00:00
|
|
|
var err error
|
2016-03-18 22:30:47 +00:00
|
|
|
ref, err = reference.WithTag(ref, opt.Tag)
|
2015-12-16 00:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-26 22:20:23 +00:00
|
|
|
tagged = true
|
2015-08-21 04:50:15 +00:00
|
|
|
} else {
|
|
|
|
err := option.Apply(ms)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 22:20:23 +00:00
|
|
|
mediaType, p, err := m.Payload()
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:20:23 +00:00
|
|
|
if !tagged {
|
|
|
|
// generate a canonical digest and Put by digest
|
|
|
|
_, d, err := distribution.UnmarshalManifest(mediaType, p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ref, err = reference.WithDigest(ref, d.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestURL, err := ms.ub.BuildManifestURL(ref)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
putRequest, err := http.NewRequestWithContext(ctx, http.MethodPut, manifestURL, bytes.NewReader(p))
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
putRequest.Header.Set("Content-Type", mediaType)
|
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
resp, err := ms.client.Do(putRequest)
|
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-08-21 04:50:15 +00:00
|
|
|
dgstHeader := resp.Header.Get("Docker-Content-Digest")
|
2016-12-15 23:07:42 +00:00
|
|
|
dgst, err := digest.Parse(dgstHeader)
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dgst, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2015-12-08 22:24:03 +00:00
|
|
|
return "", HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Delete(ctx context.Context, dgst digest.Digest) error {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithDigest(ms.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := ms.ub.BuildManifestURL(ref)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// todo(richardscothern): Restore interface and implementation with merge of #1050
|
|
|
|
/*func (ms *manifests) Enumerate(ctx context.Context, manifests []distribution.Manifest, last distribution.Manifest) (n int, err error) {
|
|
|
|
panic("not supported")
|
|
|
|
}*/
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
type blobs struct {
|
2015-12-15 22:35:23 +00:00
|
|
|
name reference.Named
|
2015-05-20 02:56:27 +00:00
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-15 23:25:00 +00:00
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
statter distribution.BlobDescriptorService
|
|
|
|
distribution.BlobDeleter
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:00:28 +00:00
|
|
|
func sanitizeLocation(location, base string) (string, error) {
|
|
|
|
baseURL, err := url.Parse(base)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:00:28 +00:00
|
|
|
locationURL, err := url.Parse(location)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-08-27 02:00:28 +00:00
|
|
|
|
|
|
|
return baseURL.ResolveReference(locationURL).String(), nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
return bs.statter.Stat(ctx, dgst)
|
2015-05-15 23:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
|
2015-12-11 23:13:03 +00:00
|
|
|
reader, err := bs.Open(ctx, dgst)
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
return io.ReadAll(reader)
|
2015-05-15 20:29:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithDigest(bs.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blobURL, err := bs.ub.BuildBlobURL(ref)
|
2015-05-15 23:25:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-19 17:35:16 +00:00
|
|
|
return transport.NewHTTPReadSeeker(ctx, bs.client, blobURL,
|
2015-12-01 02:35:19 +00:00
|
|
|
func(resp *http.Response) error {
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return distribution.ErrBlobUnknown
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-12-01 02:35:19 +00:00
|
|
|
}), nil
|
2015-05-15 20:29:44 +00:00
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error {
|
2019-05-10 10:07:50 +00:00
|
|
|
desc, err := bs.statter.Stat(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:08:31 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(desc.Size, 10))
|
|
|
|
w.Header().Set("Content-Type", desc.MediaType)
|
|
|
|
w.Header().Set("Docker-Content-Digest", dgst.String())
|
|
|
|
w.Header().Set("Etag", dgst.String())
|
|
|
|
|
|
|
|
if r.Method == http.MethodHead {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-10 10:07:50 +00:00
|
|
|
blob, err := bs.Open(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer blob.Close()
|
|
|
|
|
|
|
|
_, err = io.CopyN(w, blob, desc.Size)
|
|
|
|
return err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
|
|
|
writer, err := bs.Create(ctx)
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2016-12-17 00:28:34 +00:00
|
|
|
dgstr := digest.Canonical.Digester()
|
2015-05-21 06:44:08 +00:00
|
|
|
n, err := io.Copy(writer, io.TeeReader(bytes.NewReader(p), dgstr.Hash()))
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
if n < int64(len(p)) {
|
|
|
|
return distribution.Descriptor{}, fmt.Errorf("short copy: wrote %d of %d", n, len(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
desc := distribution.Descriptor{
|
|
|
|
MediaType: mediaType,
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: int64(len(p)),
|
2015-05-15 20:29:44 +00:00
|
|
|
Digest: dgstr.Digest(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer.Commit(ctx, desc)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:08:52 +00:00
|
|
|
type optionFunc func(interface{}) error
|
|
|
|
|
|
|
|
func (f optionFunc) Apply(v interface{}) error {
|
|
|
|
return f(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMountFrom returns a BlobCreateOption which designates that the blob should be
|
|
|
|
// mounted from the given canonical reference.
|
|
|
|
func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption {
|
|
|
|
return optionFunc(func(v interface{}) error {
|
2016-07-06 14:00:29 +00:00
|
|
|
opts, ok := v.(*distribution.CreateOptions)
|
2016-01-14 18:08:52 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected options type: %T", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Mount.ShouldMount = true
|
|
|
|
opts.Mount.From = ref
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-13 19:44:42 +00:00
|
|
|
func (bs *blobs) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
|
2016-07-06 14:00:29 +00:00
|
|
|
var opts distribution.CreateOptions
|
2015-12-15 02:34:18 +00:00
|
|
|
|
2016-01-13 19:44:42 +00:00
|
|
|
for _, option := range options {
|
|
|
|
err := option.Apply(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-15 02:34:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 19:44:42 +00:00
|
|
|
var values []url.Values
|
|
|
|
|
|
|
|
if opts.Mount.ShouldMount {
|
|
|
|
values = append(values, url.Values{"from": {opts.Mount.From.Name()}, "mount": {opts.Mount.From.Digest().String()}})
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := bs.ub.BuildBlobUploadURL(bs.name, values...)
|
2015-12-15 02:34:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, nil)
|
2020-11-03 11:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bs.client.Do(req)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-01-13 19:44:42 +00:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusCreated:
|
|
|
|
desc, err := bs.statter.Stat(ctx, opts.Mount.From.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, distribution.ErrBlobMounted{From: opts.Mount.From, Descriptor: desc}
|
|
|
|
case http.StatusAccepted:
|
2015-04-17 20:32:51 +00:00
|
|
|
// TODO(dmcgowan): Check for invalid UUID
|
|
|
|
uuid := resp.Header.Get("Docker-Upload-UUID")
|
2019-05-21 08:14:59 +00:00
|
|
|
if uuid == "" {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
// uuid is expected to be the last path element
|
|
|
|
_, uuid = path.Split(resp.Header.Get("Location"))
|
2019-05-21 08:14:59 +00:00
|
|
|
}
|
2019-05-21 08:22:29 +00:00
|
|
|
if uuid == "" {
|
|
|
|
return nil, errors.New("cannot retrieve docker upload UUID")
|
|
|
|
}
|
2019-05-21 08:14:59 +00:00
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
location, err := sanitizeLocation(resp.Header.Get("Location"), u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
return &httpBlobUpload{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: ctx,
|
2015-05-20 02:56:27 +00:00
|
|
|
statter: bs.statter,
|
2015-05-20 02:18:30 +00:00
|
|
|
client: bs.client,
|
2015-04-17 20:32:51 +00:00
|
|
|
uuid: uuid,
|
|
|
|
startedAt: time.Now(),
|
|
|
|
location: location,
|
|
|
|
}, nil
|
2016-01-13 19:44:42 +00:00
|
|
|
default:
|
|
|
|
return nil, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
2019-05-07 08:33:45 +00:00
|
|
|
location, err := bs.ub.BuildBlobUploadChunkURL(bs.name, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpBlobUpload{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: ctx,
|
2019-05-07 08:33:45 +00:00
|
|
|
statter: bs.statter,
|
|
|
|
client: bs.client,
|
|
|
|
uuid: id,
|
|
|
|
startedAt: time.Now(),
|
|
|
|
location: location,
|
|
|
|
}, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
func (bs *blobs) Delete(ctx context.Context, dgst digest.Digest) error {
|
|
|
|
return bs.statter.Clear(ctx, dgst)
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
type blobStatter struct {
|
2015-12-15 22:35:23 +00:00
|
|
|
name reference.Named
|
2015-05-20 02:56:27 +00:00
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-15 23:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithDigest(bs.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
u, err := bs.ub.BuildBlobURL(ref)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil)
|
2020-02-25 11:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
resp, err := bs.client.Do(req)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
lengthHeader := resp.Header.Get("Content-Length")
|
2015-12-07 18:17:49 +00:00
|
|
|
if lengthHeader == "" {
|
|
|
|
return distribution.Descriptor{}, fmt.Errorf("missing content-length header for request: %s", u)
|
|
|
|
}
|
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{
|
|
|
|
MediaType: resp.Header.Get("Content-Type"),
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: length,
|
2015-05-15 20:29:44 +00:00
|
|
|
Digest: dgst,
|
2015-04-17 20:32:51 +00:00
|
|
|
}, nil
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if resp.StatusCode == http.StatusNotFound {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, distribution.ErrBlobUnknown
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
func buildCatalogValues(maxEntries int, last string) url.Values {
|
|
|
|
values := url.Values{}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if maxEntries > 0 {
|
|
|
|
values.Add("n", strconv.Itoa(maxEntries))
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if last != "" {
|
|
|
|
values.Add("last", last)
|
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
return values
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
2015-05-27 17:52:22 +00:00
|
|
|
|
|
|
|
func (bs *blobStatter) Clear(ctx context.Context, dgst digest.Digest) error {
|
2015-12-16 00:43:13 +00:00
|
|
|
ref, err := reference.WithDigest(bs.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blobURL, err := bs.ub.BuildBlobURL(ref)
|
2015-05-27 17:52:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, blobURL, nil)
|
2015-05-27 17:52:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bs.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-05-27 17:52:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-05-27 17:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *blobStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc distribution.Descriptor) error {
|
|
|
|
return nil
|
|
|
|
}
|