2015-05-07 23:11:04 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-05-11 23:39:12 +00:00
|
|
|
"io/ioutil"
|
2015-05-07 23:11:04 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-05-15 20:29:44 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-05-07 23:11:04 +00:00
|
|
|
)
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
type httpBlobUpload struct {
|
2015-05-20 02:56:27 +00:00
|
|
|
statter distribution.BlobStatter
|
|
|
|
client *http.Client
|
2015-05-07 23:11:04 +00:00
|
|
|
|
|
|
|
uuid string
|
|
|
|
startedAt time.Time
|
|
|
|
|
|
|
|
location string // always the last value of the location header.
|
|
|
|
offset int64
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) handleErrorResponse(resp *http.Response) error {
|
2015-05-09 00:40:30 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
2015-05-15 23:34:00 +00:00
|
|
|
return distribution.ErrBlobUploadUnknown
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
2015-05-09 00:40:30 +00:00
|
|
|
return handleErrorResponse(resp)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
req, err := http.NewRequest("PATCH", hbu.location, ioutil.NopCloser(r))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
resp, err := hbu.client.Do(req)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusAccepted {
|
2015-05-15 20:29:44 +00:00
|
|
|
return 0, hbu.handleErrorResponse(resp)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
hbu.uuid = resp.Header.Get("Docker-Upload-UUID")
|
|
|
|
hbu.location, err = sanitizeLocation(resp.Header.Get("Location"), hbu.location)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
rng := resp.Header.Get("Range")
|
|
|
|
var start, end int64
|
|
|
|
if n, err := fmt.Sscanf(rng, "%d-%d", &start, &end); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if n != 2 || end < start {
|
|
|
|
return 0, fmt.Errorf("bad range format: %s", rng)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (end - start + 1), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) Write(p []byte) (n int, err error) {
|
|
|
|
req, err := http.NewRequest("PATCH", hbu.location, bytes.NewReader(p))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
req.Header.Set("Content-Range", fmt.Sprintf("%d-%d", hbu.offset, hbu.offset+int64(len(p)-1)))
|
2015-05-07 23:11:04 +00:00
|
|
|
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(p)))
|
|
|
|
req.Header.Set("Content-Type", "application/octet-stream")
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
resp, err := hbu.client.Do(req)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusAccepted {
|
2015-05-15 20:29:44 +00:00
|
|
|
return 0, hbu.handleErrorResponse(resp)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
hbu.uuid = resp.Header.Get("Docker-Upload-UUID")
|
|
|
|
hbu.location, err = sanitizeLocation(resp.Header.Get("Location"), hbu.location)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
rng := resp.Header.Get("Range")
|
|
|
|
var start, end int
|
|
|
|
if n, err := fmt.Sscanf(rng, "%d-%d", &start, &end); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if n != 2 || end < start {
|
|
|
|
return 0, fmt.Errorf("bad range format: %s", rng)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (end - start + 1), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
newOffset := hbu.offset
|
2015-05-07 23:11:04 +00:00
|
|
|
|
|
|
|
switch whence {
|
|
|
|
case os.SEEK_CUR:
|
|
|
|
newOffset += int64(offset)
|
|
|
|
case os.SEEK_END:
|
2015-05-20 02:18:30 +00:00
|
|
|
newOffset += int64(offset)
|
2015-05-07 23:11:04 +00:00
|
|
|
case os.SEEK_SET:
|
|
|
|
newOffset = int64(offset)
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
hbu.offset = newOffset
|
2015-05-07 23:11:04 +00:00
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
return hbu.offset, nil
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) ID() string {
|
|
|
|
return hbu.uuid
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) StartedAt() time.Time {
|
|
|
|
return hbu.startedAt
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) Commit(ctx context.Context, desc distribution.Descriptor) (distribution.Descriptor, error) {
|
2015-05-07 23:11:04 +00:00
|
|
|
// TODO(dmcgowan): Check if already finished, if so just fetch
|
2015-05-15 20:29:44 +00:00
|
|
|
req, err := http.NewRequest("PUT", hbu.location, nil)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values := req.URL.Query()
|
2015-05-15 20:29:44 +00:00
|
|
|
values.Set("digest", desc.Digest.String())
|
2015-05-07 23:11:04 +00:00
|
|
|
req.URL.RawQuery = values.Encode()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
resp, err := hbu.client.Do(req)
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
2015-05-20 02:18:30 +00:00
|
|
|
defer resp.Body.Close()
|
2015-05-07 23:11:04 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, hbu.handleErrorResponse(resp)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:56:27 +00:00
|
|
|
return hbu.statter.Stat(ctx, desc.Digest)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
func (hbu *httpBlobUpload) Cancel(ctx context.Context) error {
|
2015-05-20 02:18:30 +00:00
|
|
|
req, err := http.NewRequest("DELETE", hbu.location, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := hbu.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusNoContent, http.StatusNotFound:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return hbu.handleErrorResponse(resp)
|
|
|
|
}
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
func (hbu *httpBlobUpload) Close() error {
|
|
|
|
hbu.closed = true
|
2015-05-07 23:11:04 +00:00
|
|
|
return nil
|
|
|
|
}
|