2015-05-07 23:11:04 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-05-07 23:11:04 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-05-11 23:39:12 +00:00
|
|
|
"io/ioutil"
|
2015-05-07 23:11:04 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
)
|
|
|
|
|
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-07-29 18:12:01 +00:00
|
|
|
func (hbu *httpBlobUpload) Reader() (io.ReadCloser, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
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-12-08 22:24:03 +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()
|
|
|
|
|
2023-08-15 07:46:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if !SuccessStatus(resp.StatusCode) {
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if !SuccessStatus(resp.StatusCode) {
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
func (hbu *httpBlobUpload) Size() int64 {
|
|
|
|
return hbu.offset
|
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
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if !SuccessStatus(resp.StatusCode) {
|
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()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound || SuccessStatus(resp.StatusCode) {
|
2015-05-20 02:18:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-24 23:14:04 +00:00
|
|
|
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
|
|
|
|
}
|