2014-11-11 04:58:10 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
2014-12-10 06:25:54 +01:00
|
|
|
"github.com/docker/docker-registry/api/errors"
|
2014-11-20 03:52:09 +01:00
|
|
|
"github.com/docker/docker-registry/digest"
|
2014-11-22 04:29:08 +01:00
|
|
|
"github.com/docker/docker-registry/storage"
|
2014-11-11 04:58:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client implements the client interface to the registry http api
|
|
|
|
type Client interface {
|
|
|
|
// GetImageManifest returns an image manifest for the image at the given
|
2014-11-20 03:06:54 +01:00
|
|
|
// name, tag pair.
|
2014-11-22 04:29:08 +01:00
|
|
|
GetImageManifest(name, tag string) (*storage.SignedManifest, error)
|
2014-11-11 04:58:10 +01:00
|
|
|
|
|
|
|
// PutImageManifest uploads an image manifest for the image at the given
|
2014-11-20 03:06:54 +01:00
|
|
|
// name, tag pair.
|
2014-11-22 04:29:08 +01:00
|
|
|
PutImageManifest(name, tag string, imageManifest *storage.SignedManifest) error
|
2014-11-11 04:58:10 +01:00
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
// DeleteImage removes the image at the given name, tag pair.
|
2014-11-11 04:58:10 +01:00
|
|
|
DeleteImage(name, tag string) error
|
|
|
|
|
|
|
|
// ListImageTags returns a list of all image tags with the given repository
|
2014-11-20 03:06:54 +01:00
|
|
|
// name.
|
2014-11-11 04:58:10 +01:00
|
|
|
ListImageTags(name string) ([]string, error)
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
// BlobLength returns the length of the blob stored at the given name,
|
|
|
|
// digest pair.
|
|
|
|
// Returns a length value of -1 on error or if the blob does not exist.
|
2014-11-20 03:52:09 +01:00
|
|
|
BlobLength(name string, dgst digest.Digest) (int, error)
|
2014-11-20 03:06:54 +01:00
|
|
|
|
|
|
|
// GetBlob returns the blob stored at the given name, digest pair in the
|
|
|
|
// form of an io.ReadCloser with the length of this blob.
|
|
|
|
// A nonzero byteOffset can be provided to receive a partial blob beginning
|
|
|
|
// at the given offset.
|
2014-11-20 03:52:09 +01:00
|
|
|
GetBlob(name string, dgst digest.Digest, byteOffset int) (io.ReadCloser, int, error)
|
2014-11-20 03:06:54 +01:00
|
|
|
|
|
|
|
// InitiateBlobUpload starts a blob upload in the given repository namespace
|
|
|
|
// and returns a unique location url to use for other blob upload methods.
|
|
|
|
InitiateBlobUpload(name string) (string, error)
|
|
|
|
|
|
|
|
// GetBlobUploadStatus returns the byte offset and length of the blob at the
|
|
|
|
// given upload location.
|
|
|
|
GetBlobUploadStatus(location string) (int, int, error)
|
|
|
|
|
|
|
|
// UploadBlob uploads a full blob to the registry.
|
2014-11-20 03:52:09 +01:00
|
|
|
UploadBlob(location string, blob io.ReadCloser, length int, dgst digest.Digest) error
|
2014-11-20 03:06:54 +01:00
|
|
|
|
|
|
|
// UploadBlobChunk uploads a blob chunk with a given length and startByte to
|
|
|
|
// the registry.
|
|
|
|
// FinishChunkedBlobUpload must be called to finalize this upload.
|
|
|
|
UploadBlobChunk(location string, blobChunk io.ReadCloser, length, startByte int) error
|
|
|
|
|
|
|
|
// FinishChunkedBlobUpload completes a chunked blob upload at a given
|
|
|
|
// location.
|
2014-11-20 03:52:09 +01:00
|
|
|
FinishChunkedBlobUpload(location string, length int, dgst digest.Digest) error
|
2014-11-20 03:06:54 +01:00
|
|
|
|
|
|
|
// CancelBlobUpload deletes all content at the unfinished blob upload
|
|
|
|
// location and invalidates any future calls to this blob upload.
|
|
|
|
CancelBlobUpload(location string) error
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Client which operates against a registry with the
|
|
|
|
// given base endpoint
|
2014-11-20 03:06:54 +01:00
|
|
|
// This endpoint should not include /v2/ or any part of the url after this.
|
2014-11-11 04:58:10 +01:00
|
|
|
func New(endpoint string) Client {
|
|
|
|
return &clientImpl{endpoint}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clientImpl is the default implementation of the Client interface
|
|
|
|
type clientImpl struct {
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bbland): use consistent route generation between server and client
|
|
|
|
|
2014-11-22 04:29:08 +01:00
|
|
|
func (r *clientImpl) GetImageManifest(name, tag string) (*storage.SignedManifest, error) {
|
2014-11-18 00:44:07 +01:00
|
|
|
response, err := http.Get(r.imageManifestURL(name, tag))
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusOK:
|
|
|
|
break
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &ImageManifestNotFoundError{Name: name, Tag: tag}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
|
|
|
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
|
|
|
2014-11-22 04:29:08 +01:00
|
|
|
manifest := new(storage.SignedManifest)
|
2014-11-11 04:58:10 +01:00
|
|
|
err = decoder.Decode(manifest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
2014-11-22 04:29:08 +01:00
|
|
|
func (r *clientImpl) PutImageManifest(name, tag string, manifest *storage.SignedManifest) error {
|
2014-11-11 04:58:10 +01:00
|
|
|
putRequest, err := http.NewRequest("PUT",
|
2014-12-10 06:25:54 +01:00
|
|
|
r.imageManifestURL(name, tag), bytes.NewReader(manifest.Raw))
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(putRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusOK:
|
|
|
|
return nil
|
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errors errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
|
|
err = decoder.Decode(&errors)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
|
|
|
|
return &errors
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *clientImpl) DeleteImage(name, tag string) error {
|
|
|
|
deleteRequest, err := http.NewRequest("DELETE",
|
2014-11-18 00:44:07 +01:00
|
|
|
r.imageManifestURL(name, tag), nil)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(deleteRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusNoContent:
|
|
|
|
break
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &ImageManifestNotFoundError{Name: name, Tag: tag}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *clientImpl) ListImageTags(name string) ([]string, error) {
|
2014-11-14 03:42:49 +01:00
|
|
|
response, err := http.Get(fmt.Sprintf("%s/v2/%s/tags/list", r.Endpoint, name))
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusOK:
|
|
|
|
break
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &RepositoryNotFoundError{Name: name}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tags := struct {
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(response.Body)
|
|
|
|
err = decoder.Decode(&tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags.Tags, nil
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:52:09 +01:00
|
|
|
func (r *clientImpl) BlobLength(name string, dgst digest.Digest) (int, error) {
|
2014-12-11 07:29:58 +01:00
|
|
|
response, err := http.Head(fmt.Sprintf("%s/v2/%s/blobs/%s", r.Endpoint, name, dgst))
|
2014-11-20 03:06:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusOK:
|
|
|
|
lengthHeader := response.Header.Get("Content-Length")
|
2014-12-10 06:25:54 +01:00
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
2014-11-20 03:06:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return int(length), nil
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
|
|
|
return -1, nil
|
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-20 03:06:54 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-20 03:06:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return -1, &errs
|
2014-11-20 03:06:54 +01:00
|
|
|
default:
|
|
|
|
response.Body.Close()
|
2014-12-10 06:25:54 +01:00
|
|
|
return -1, &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-20 03:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:52:09 +01:00
|
|
|
func (r *clientImpl) GetBlob(name string, dgst digest.Digest, byteOffset int) (io.ReadCloser, int, error) {
|
2014-11-11 04:58:10 +01:00
|
|
|
getRequest, err := http.NewRequest("GET",
|
2014-12-11 07:29:58 +01:00
|
|
|
fmt.Sprintf("%s/v2/%s/blobs/%s", r.Endpoint, name, dgst), nil)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequest.Header.Add("Range", fmt.Sprintf("%d-", byteOffset))
|
|
|
|
response, err := http.DefaultClient.Do(getRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusOK:
|
|
|
|
lengthHeader := response.Header.Get("Content-Length")
|
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return response.Body, int(length), nil
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
|
|
|
response.Body.Close()
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, 0, &BlobNotFoundError{Name: name, Digest: dgst}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, 0, &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
|
|
|
response.Body.Close()
|
2014-12-10 06:25:54 +01:00
|
|
|
return nil, 0, &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (r *clientImpl) InitiateBlobUpload(name string) (string, error) {
|
2014-11-11 04:58:10 +01:00
|
|
|
postRequest, err := http.NewRequest("POST",
|
2014-12-11 07:29:58 +01:00
|
|
|
fmt.Sprintf("%s/v2/%s/blobs/uploads/", r.Endpoint, name), nil)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(postRequest)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusAccepted:
|
|
|
|
return response.Header.Get("Location"), nil
|
2014-11-20 03:06:54 +01:00
|
|
|
// case response.StatusCode == http.StatusNotFound:
|
|
|
|
// return
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return "", &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return "", &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (r *clientImpl) GetBlobUploadStatus(location string) (int, int, error) {
|
2014-12-10 06:25:54 +01:00
|
|
|
response, err := http.Get(location)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusNoContent:
|
|
|
|
return parseRangeHeader(response.Header.Get("Range"))
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return 0, 0, &BlobUploadNotFoundError{Location: location}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return 0, 0, &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return 0, 0, &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:52:09 +01:00
|
|
|
func (r *clientImpl) UploadBlob(location string, blob io.ReadCloser, length int, dgst digest.Digest) error {
|
2014-11-20 03:06:54 +01:00
|
|
|
defer blob.Close()
|
2014-11-11 04:58:10 +01:00
|
|
|
|
2014-12-10 06:25:54 +01:00
|
|
|
putRequest, err := http.NewRequest("PUT", location, blob)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-14 03:42:49 +01:00
|
|
|
queryValues := url.Values{}
|
2014-12-10 06:25:54 +01:00
|
|
|
queryValues.Set("size", fmt.Sprint(length))
|
2014-11-20 03:52:09 +01:00
|
|
|
queryValues.Set("digest", dgst.String())
|
2014-11-11 04:58:10 +01:00
|
|
|
putRequest.URL.RawQuery = queryValues.Encode()
|
|
|
|
|
|
|
|
putRequest.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
putRequest.Header.Set("Content-Length", fmt.Sprint(length))
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(putRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusCreated:
|
|
|
|
return nil
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &BlobUploadNotFoundError{Location: location}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (r *clientImpl) UploadBlobChunk(location string, blobChunk io.ReadCloser, length, startByte int) error {
|
|
|
|
defer blobChunk.Close()
|
2014-11-11 04:58:10 +01:00
|
|
|
|
|
|
|
putRequest, err := http.NewRequest("PUT",
|
2014-11-20 03:06:54 +01:00
|
|
|
fmt.Sprintf("%s%s", r.Endpoint, location), blobChunk)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
endByte := startByte + length
|
|
|
|
|
|
|
|
putRequest.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
putRequest.Header.Set("Content-Length", fmt.Sprint(length))
|
|
|
|
putRequest.Header.Set("Content-Range",
|
|
|
|
fmt.Sprintf("%d-%d/%d", startByte, endByte, endByte))
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(putRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusAccepted:
|
|
|
|
return nil
|
|
|
|
case response.StatusCode == http.StatusRequestedRangeNotSatisfiable:
|
2014-11-20 03:06:54 +01:00
|
|
|
lastValidRange, blobSize, err := parseRangeHeader(response.Header.Get("Range"))
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &BlobUploadInvalidRangeError{
|
2014-11-17 22:07:58 +01:00
|
|
|
Location: location,
|
|
|
|
LastValidRange: lastValidRange,
|
2014-11-20 03:06:54 +01:00
|
|
|
BlobSize: blobSize,
|
2014-11-17 22:07:58 +01:00
|
|
|
}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &BlobUploadNotFoundError{Location: location}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:52:09 +01:00
|
|
|
func (r *clientImpl) FinishChunkedBlobUpload(location string, length int, dgst digest.Digest) error {
|
2014-11-11 04:58:10 +01:00
|
|
|
putRequest, err := http.NewRequest("PUT",
|
|
|
|
fmt.Sprintf("%s%s", r.Endpoint, location), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queryValues := new(url.Values)
|
2014-12-10 06:25:54 +01:00
|
|
|
queryValues.Set("size", fmt.Sprint(length))
|
2014-11-20 03:52:09 +01:00
|
|
|
queryValues.Set("digest", dgst.String())
|
2014-11-11 04:58:10 +01:00
|
|
|
putRequest.URL.RawQuery = queryValues.Encode()
|
|
|
|
|
|
|
|
putRequest.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
putRequest.Header.Set("Content-Length", "0")
|
|
|
|
putRequest.Header.Set("Content-Range",
|
|
|
|
fmt.Sprintf("%d-%d/%d", length, length, length))
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(putRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusCreated:
|
|
|
|
return nil
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &BlobUploadNotFoundError{Location: location}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 03:06:54 +01:00
|
|
|
func (r *clientImpl) CancelBlobUpload(location string) error {
|
2014-11-11 04:58:10 +01:00
|
|
|
deleteRequest, err := http.NewRequest("DELETE",
|
|
|
|
fmt.Sprintf("%s%s", r.Endpoint, location), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(deleteRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// TODO(bbland): handle other status codes, like 5xx errors
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusNoContent:
|
|
|
|
return nil
|
|
|
|
case response.StatusCode == http.StatusNotFound:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &BlobUploadNotFoundError{Location: location}
|
2014-11-11 04:58:10 +01:00
|
|
|
case response.StatusCode >= 400 && response.StatusCode < 500:
|
2014-12-10 06:25:54 +01:00
|
|
|
var errs errors.Errors
|
2014-11-11 04:58:10 +01:00
|
|
|
decoder := json.NewDecoder(response.Body)
|
2014-12-10 06:25:54 +01:00
|
|
|
err = decoder.Decode(&errs)
|
2014-11-11 04:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 06:25:54 +01:00
|
|
|
return &errs
|
2014-11-11 04:58:10 +01:00
|
|
|
default:
|
2014-12-10 06:25:54 +01:00
|
|
|
return &UnexpectedHTTPStatusError{Status: response.Status}
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// imageManifestURL is a helper method for returning the full url to an image
|
2014-11-11 04:58:10 +01:00
|
|
|
// manifest
|
2014-11-18 00:44:07 +01:00
|
|
|
func (r *clientImpl) imageManifestURL(name, tag string) string {
|
2014-12-11 07:29:58 +01:00
|
|
|
return fmt.Sprintf("%s/v2/%s/manifests/%s", r.Endpoint, name, tag)
|
2014-11-11 04:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseRangeHeader parses out the offset and length from a returned Range
|
|
|
|
// header
|
|
|
|
func parseRangeHeader(byteRangeHeader string) (int, int, error) {
|
|
|
|
r := regexp.MustCompile("bytes=0-(\\d+)/(\\d+)")
|
|
|
|
submatches := r.FindStringSubmatch(byteRangeHeader)
|
|
|
|
offset, err := strconv.ParseInt(submatches[1], 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
length, err := strconv.ParseInt(submatches[2], 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return int(offset), int(length), nil
|
|
|
|
}
|