2015-04-17 22:32:51 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-05-15 22:29:44 +02:00
|
|
|
"io"
|
2015-05-09 02:40:30 +02:00
|
|
|
"io/ioutil"
|
2015-04-17 22:32:51 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-05-09 01:33:27 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-05-20 04:18:30 +02:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-04-17 22:32:51 +02:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-05-16 01:50:17 +02:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-05-16 01:25:00 +02:00
|
|
|
"github.com/docker/distribution/registry/storage/cache"
|
2015-05-21 02:12:40 +02:00
|
|
|
"github.com/docker/distribution/registry/storage/cache/memory"
|
2015-04-17 22:32:51 +02:00
|
|
|
)
|
|
|
|
|
2015-05-16 01:50:17 +02:00
|
|
|
// NewRepository creates a new Repository for the given repository name and base URL
|
|
|
|
func NewRepository(ctx context.Context, name, baseURL string, transport http.RoundTripper) (distribution.Repository, error) {
|
2015-05-21 03:06:13 +02:00
|
|
|
if err := v2.ValidateRepositoryName(name); err != nil {
|
2015-04-17 22:32:51 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:50:17 +02:00
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:54:23 +02:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
// TODO(dmcgowan): create cookie jar
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &repository{
|
|
|
|
client: client,
|
|
|
|
ub: ub,
|
|
|
|
name: name,
|
|
|
|
context: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type repository struct {
|
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
context context.Context
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repository) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:25:00 +02:00
|
|
|
func (r *repository) Blobs(ctx context.Context) distribution.BlobStore {
|
|
|
|
statter := &blobStatter{
|
2015-05-20 04:56:27 +02:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
2015-05-15 22:29:44 +02:00
|
|
|
return &blobs{
|
2015-05-20 04:56:27 +02:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-05-21 02:12:40 +02:00
|
|
|
statter: cache.NewCachedBlobStatter(memory.NewInMemoryBlobDescriptorCacheProvider(), statter),
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:39:34 +02: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 22:32:51 +02:00
|
|
|
return &manifests{
|
2015-05-20 04:56:27 +02:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-07-15 01:25:37 +02:00
|
|
|
etags: make(map[string]string),
|
2015-06-15 19:39:34 +02:00
|
|
|
}, nil
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repository) Signatures() distribution.SignatureService {
|
2015-06-15 19:39:34 +02:00
|
|
|
ms, _ := r.Manifests(r.context)
|
2015-04-17 22:32:51 +02:00
|
|
|
return &signatures{
|
2015-06-15 19:39:34 +02:00
|
|
|
manifests: ms,
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type signatures struct {
|
2015-05-20 04:56:27 +02:00
|
|
|
manifests distribution.ManifestService
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signatures) Get(dgst digest.Digest) ([][]byte, error) {
|
2015-05-20 04:56:27 +02:00
|
|
|
m, err := s.manifests.Get(dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m.Signatures()
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signatures) Put(dgst digest.Digest, signatures ...[]byte) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifests struct {
|
2015-05-20 04:56:27 +02:00
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-07-15 01:25:37 +02:00
|
|
|
etags map[string]string
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) Tags() ([]string, error) {
|
2015-05-09 02:40:30 +02:00
|
|
|
u, err := ms.ub.BuildTagsURL(ms.name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-14 19:18:21 +02:00
|
|
|
defer resp.Body.Close()
|
2015-05-09 02:40:30 +02:00
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
2015-05-09 02:40:30 +02:00
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tagsResponse := struct {
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &tagsResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagsResponse.Tags, nil
|
2015-05-20 04:18:30 +02:00
|
|
|
case http.StatusNotFound:
|
2015-05-09 02:40:30 +02:00
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, handleErrorResponse(resp)
|
|
|
|
}
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) Exists(dgst digest.Digest) (bool, error) {
|
2015-05-20 04:18:30 +02:00
|
|
|
// Call by Tag endpoint since the API uses the same
|
|
|
|
// URL endpoint for tags and digests.
|
2015-04-17 22:32:51 +02:00
|
|
|
return ms.ExistsByTag(dgst.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) ExistsByTag(tag string) (bool, error) {
|
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, tag)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Head(u)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
2015-04-17 22:32:51 +02:00
|
|
|
return true, nil
|
2015-05-20 04:18:30 +02:00
|
|
|
case http.StatusNotFound:
|
2015-04-17 22:32:51 +02:00
|
|
|
return false, nil
|
|
|
|
default:
|
2015-05-09 02:40:30 +02:00
|
|
|
return false, handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) Get(dgst digest.Digest) (*manifest.SignedManifest, error) {
|
2015-05-20 04:18:30 +02:00
|
|
|
// Call by Tag endpoint since the API uses the same
|
|
|
|
// URL endpoint for tags and digests.
|
2015-04-17 22:32:51 +02:00
|
|
|
return ms.GetByTag(dgst.String())
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:25:37 +02:00
|
|
|
// AddEtagToTag allows a client to supply an eTag to GetByTag which will
|
|
|
|
// be used for a conditional HTTP request. If the eTag matches, a nil
|
|
|
|
// manifest and nil error will be returned.
|
|
|
|
func AddEtagToTag(tagName, dgst string) distribution.ManifestServiceOption {
|
|
|
|
return func(ms distribution.ManifestService) error {
|
|
|
|
if ms, ok := ms.(*manifests); ok {
|
|
|
|
ms.etags[tagName] = dgst
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("etag options is a client-only option")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) GetByTag(tag string, options ...distribution.ManifestServiceOption) (*manifest.SignedManifest, error) {
|
|
|
|
for _, option := range options {
|
|
|
|
err := option(ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:32:51 +02:00
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-15 01:25:37 +02:00
|
|
|
req, err := http.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 22:32:51 +02:00
|
|
|
|
2015-07-15 01:25:37 +02:00
|
|
|
if _, ok := ms.etags[tag]; ok {
|
|
|
|
req.Header.Set("eTag", ms.etags[tag])
|
|
|
|
}
|
|
|
|
resp, err := ms.client.Do(req)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
2015-04-17 22:32:51 +02:00
|
|
|
var sm manifest.SignedManifest
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&sm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &sm, nil
|
2015-07-15 01:25:37 +02:00
|
|
|
case http.StatusNotModified:
|
|
|
|
return nil, nil
|
2015-04-17 22:32:51 +02:00
|
|
|
default:
|
2015-05-09 02:40:30 +02:00
|
|
|
return nil, handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) Put(m *manifest.SignedManifest) error {
|
|
|
|
manifestURL, err := ms.ub.BuildManifestURL(ms.name, m.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:39:34 +02:00
|
|
|
// todo(richardscothern): do something with options here when they become applicable
|
|
|
|
|
2015-04-17 22:32:51 +02:00
|
|
|
putRequest, err := http.NewRequest("PUT", manifestURL, bytes.NewReader(m.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Do(putRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusAccepted:
|
|
|
|
// TODO(dmcgowan): make use of digest header
|
2015-04-17 22:32:51 +02:00
|
|
|
return nil
|
|
|
|
default:
|
2015-05-09 02:40:30 +02:00
|
|
|
return handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *manifests) Delete(dgst digest.Digest) error {
|
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, dgst.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
2015-04-17 22:32:51 +02:00
|
|
|
return nil
|
|
|
|
default:
|
2015-05-09 02:40:30 +02:00
|
|
|
return handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:29:44 +02:00
|
|
|
type blobs struct {
|
2015-05-20 04:56:27 +02:00
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-16 01:25:00 +02:00
|
|
|
|
|
|
|
statter distribution.BlobStatter
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizeLocation(location, source string) (string, error) {
|
|
|
|
locationURL, err := url.Parse(location)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if locationURL.Scheme == "" {
|
|
|
|
sourceURL, err := url.Parse(source)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
locationURL = &url.URL{
|
|
|
|
Scheme: sourceURL.Scheme,
|
|
|
|
Host: sourceURL.Host,
|
|
|
|
Path: location,
|
|
|
|
}
|
|
|
|
location = locationURL.String()
|
|
|
|
}
|
|
|
|
return location, nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
return bs.statter.Stat(ctx, dgst)
|
2015-05-16 01:25:00 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
|
|
|
|
desc, err := bs.Stat(ctx, dgst)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
2015-05-15 22:29:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-20 04:18:30 +02:00
|
|
|
reader, err := bs.Open(ctx, desc.Digest)
|
2015-05-15 22:29:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
2015-05-15 22:29:44 +02:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
return ioutil.ReadAll(reader)
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) {
|
|
|
|
stat, err := bs.statter.Stat(ctx, dgst)
|
2015-05-16 01:25:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:56:27 +02:00
|
|
|
blobURL, err := bs.ub.BuildBlobURL(bs.name, stat.Digest)
|
2015-05-16 01:25:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:07:11 +02:00
|
|
|
return transport.NewHTTPReadSeeker(bs.client, blobURL, stat.Size), nil
|
2015-05-15 22:29:44 +02:00
|
|
|
}
|
2015-04-17 22:32:51 +02:00
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error {
|
|
|
|
panic("not implemented")
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
|
|
|
writer, err := bs.Create(ctx)
|
2015-05-15 22:29:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2015-05-22 03:44:08 +02:00
|
|
|
dgstr := digest.Canonical.New()
|
2015-05-21 08:44:08 +02:00
|
|
|
n, err := io.Copy(writer, io.TeeReader(bytes.NewReader(p), dgstr.Hash()))
|
2015-05-15 22:29:44 +02: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 02:07:11 +02:00
|
|
|
Size: int64(len(p)),
|
2015-05-15 22:29:44 +02:00
|
|
|
Digest: dgstr.Digest(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer.Commit(ctx, desc)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Create(ctx context.Context) (distribution.BlobWriter, error) {
|
|
|
|
u, err := bs.ub.BuildBlobUploadURL(bs.name)
|
2015-04-17 22:32:51 +02:00
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
resp, err := bs.client.Post(u, "", nil)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusAccepted:
|
2015-04-17 22:32:51 +02:00
|
|
|
// TODO(dmcgowan): Check for invalid UUID
|
|
|
|
uuid := resp.Header.Get("Docker-Upload-UUID")
|
|
|
|
location, err := sanitizeLocation(resp.Header.Get("Location"), u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:29:44 +02:00
|
|
|
return &httpBlobUpload{
|
2015-05-20 04:56:27 +02:00
|
|
|
statter: bs.statter,
|
2015-05-20 04:18:30 +02:00
|
|
|
client: bs.client,
|
2015-04-17 22:32:51 +02:00
|
|
|
uuid: uuid,
|
|
|
|
startedAt: time.Now(),
|
|
|
|
location: location,
|
|
|
|
}, nil
|
|
|
|
default:
|
2015-05-09 02:40:30 +02:00
|
|
|
return nil, handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobs) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
2015-04-17 22:32:51 +02:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:25:00 +02:00
|
|
|
type blobStatter struct {
|
2015-05-20 04:56:27 +02:00
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-16 01:25:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
u, err := bs.ub.BuildBlobURL(bs.name, dgst)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
resp, err := bs.client.Head(u)
|
2015-04-17 22:32:51 +02:00
|
|
|
if err != nil {
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-05-20 04:18:30 +02:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
2015-04-17 22:32:51 +02:00
|
|
|
lengthHeader := resp.Header.Get("Content-Length")
|
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
|
|
|
if err != nil {
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{
|
|
|
|
MediaType: resp.Header.Get("Content-Type"),
|
2015-07-18 02:07:11 +02:00
|
|
|
Size: length,
|
2015-05-15 22:29:44 +02:00
|
|
|
Digest: dgst,
|
2015-04-17 22:32:51 +02:00
|
|
|
}, nil
|
2015-05-20 04:18:30 +02:00
|
|
|
case http.StatusNotFound:
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{}, distribution.ErrBlobUnknown
|
2015-04-17 22:32:51 +02:00
|
|
|
default:
|
2015-05-15 22:29:44 +02:00
|
|
|
return distribution.Descriptor{}, handleErrorResponse(resp)
|
2015-04-17 22:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 22:08:13 +02:00
|
|
|
|
|
|
|
// NewCatalog can be used to get a list of repositories
|
|
|
|
func NewCatalog(ctx context.Context, baseURL string, transport http.RoundTripper) (distribution.CatalogService, error) {
|
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &catalog{
|
|
|
|
client: client,
|
|
|
|
ub: ub,
|
|
|
|
context: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type catalog struct {
|
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *catalog) Get(maxEntries int, last string) ([]string, bool, error) {
|
|
|
|
var repos []string
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
|
|
|
|
if maxEntries > 0 {
|
|
|
|
values.Add("n", strconv.Itoa(maxEntries))
|
|
|
|
}
|
|
|
|
|
|
|
|
if last != "" {
|
|
|
|
values.Add("last", last)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := c.ub.BuildCatalogURL(values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.client.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
var ctlg struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&ctlg); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repos = ctlg.Repositories
|
|
|
|
default:
|
|
|
|
return nil, false, handleErrorResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repos, false, nil
|
|
|
|
}
|