2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-18 11:05:18 +00:00
restic/internal/backend/rest/rest.go

419 lines
10 KiB
Go
Raw Normal View History

package rest
import (
2017-06-03 15:39:57 +00:00
"context"
"encoding/json"
"fmt"
"hash"
"io"
"net/http"
"net/url"
"path"
2016-02-21 15:35:25 +00:00
"strings"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
)
// make sure the rest backend implements restic.Backend
2018-03-13 21:30:41 +00:00
var _ restic.Backend = &Backend{}
2018-03-13 21:30:41 +00:00
// Backend uses the REST protocol to access data stored on a server.
type Backend struct {
2021-08-07 20:20:49 +00:00
url *url.URL
connections uint
client http.Client
layout.Layout
}
2018-03-13 21:22:35 +00:00
// the REST API protocol version is decided by HTTP request headers, these are the constants.
2018-01-23 22:12:52 +00:00
const (
2018-03-13 21:22:35 +00:00
ContentTypeV1 = "application/vnd.x.restic.rest.v1"
ContentTypeV2 = "application/vnd.x.restic.rest.v2"
2018-01-23 22:12:52 +00:00
)
// Open opens the REST backend with the given config.
2018-03-13 21:30:41 +00:00
func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
2017-04-11 19:47:57 +00:00
// use url without trailing slash for layout
url := cfg.URL.String()
if url[len(url)-1] == '/' {
url = url[:len(url)-1]
}
2018-03-13 21:30:41 +00:00
be := &Backend{
2021-08-07 20:20:49 +00:00
url: cfg.URL,
client: http.Client{Transport: rt},
Layout: &layout.RESTLayout{URL: url, Join: path.Join},
2021-08-07 20:20:49 +00:00
connections: cfg.Connections,
2017-04-11 19:47:57 +00:00
}
return be, nil
}
// Create creates a new REST on server configured in config.
func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
be, err := Open(cfg, rt)
if err != nil {
return nil, err
}
_, err = be.Stat(ctx, restic.Handle{Type: restic.ConfigFile})
if err == nil {
return nil, errors.Fatal("config file already exists")
}
url := *cfg.URL
values := url.Query()
values.Set("create", "true")
url.RawQuery = values.Encode()
resp, err := be.client.Post(url.String(), "binary/octet-stream", strings.NewReader(""))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Fatalf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
}
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return be, nil
}
2021-08-07 20:20:49 +00:00
func (b *Backend) Connections() uint {
return b.connections
}
// Location returns this backend's location (the server's URL).
2018-03-13 21:30:41 +00:00
func (b *Backend) Location() string {
return b.url.String()
}
// Hasher may return a hash function for calculating a content hash for the backend
func (b *Backend) Hasher() hash.Hash {
return nil
}
// HasAtomicReplace returns whether Save() can atomically replace files
func (b *Backend) HasAtomicReplace() bool {
// rest-server prevents overwriting
return false
}
// Save stores data in the backend at the handle.
2018-03-13 21:30:41 +00:00
func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
2017-06-03 15:39:57 +00:00
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// make sure that client.Post() cannot close the reader by wrapping it
req, err := http.NewRequestWithContext(ctx,
http.MethodPost, b.Filename(h), io.NopCloser(rd))
2018-01-23 22:12:52 +00:00
if err != nil {
return errors.WithStack(err)
2018-01-23 22:12:52 +00:00
}
req.Header.Set("Content-Type", "application/octet-stream")
2018-03-13 21:22:35 +00:00
req.Header.Set("Accept", ContentTypeV2)
2018-01-23 22:12:52 +00:00
// explicitly set the content length, this prevents chunked encoding and
// let's the server know what's coming.
req.ContentLength = rd.Length()
resp, err := b.client.Do(req)
var cerr error
if resp != nil {
_, _ = io.Copy(io.Discard, resp.Body)
cerr = resp.Body.Close()
}
if err != nil {
return errors.WithStack(err)
}
if resp.StatusCode != 200 {
2017-05-28 10:31:19 +00:00
return errors.Errorf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
}
return errors.Wrap(cerr, "Close")
}
// notExistError is returned whenever the requested file does not exist on the
2017-06-15 11:40:27 +00:00
// server.
type notExistError struct {
2017-06-15 11:40:27 +00:00
restic.Handle
}
func (e *notExistError) Error() string {
2017-06-15 11:40:27 +00:00
return fmt.Sprintf("%v does not exist", e.Handle)
}
// IsNotExist returns true if the error was caused by a non-existing file.
2018-03-13 21:30:41 +00:00
func (b *Backend) IsNotExist(err error) bool {
var e *notExistError
return errors.As(err, &e)
2017-06-15 11:40:27 +00:00
}
// Load runs fn with a reader that yields the contents of the file at h at the
// given offset.
2018-03-13 21:30:41 +00:00
func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
r, err := b.openReader(ctx, h, length, offset)
if err != nil {
return err
}
err = fn(r)
if err != nil {
_ = r.Close() // ignore error here
return err
}
// Note: readerat.ReadAt() (the fn) uses io.ReadFull() that doesn't
// wait for EOF after reading body. Due to HTTP/2 stream multiplexing
// and goroutine timings the EOF frame arrives from server (eg. rclone)
// with a delay after reading body. Immediate close might trigger
// HTTP/2 stream reset resulting in the *stream closed* error on server,
// so we wait for EOF before closing body.
var buf [1]byte
_, err = r.Read(buf[:])
if err == io.EOF {
err = nil
}
if e := r.Close(); err == nil {
err = e
}
return err
}
2018-03-13 21:30:41 +00:00
func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, "GET", b.Filename(h), nil)
2017-01-22 21:01:12 +00:00
if err != nil {
return nil, errors.WithStack(err)
2017-01-22 21:01:12 +00:00
}
byteRange := fmt.Sprintf("bytes=%d-", offset)
if length > 0 {
byteRange = fmt.Sprintf("bytes=%d-%d", offset, offset+int64(length)-1)
}
2018-01-23 22:12:52 +00:00
req.Header.Set("Range", byteRange)
2018-03-13 21:22:35 +00:00
req.Header.Set("Accept", ContentTypeV2)
2017-01-22 21:01:12 +00:00
resp, err := b.client.Do(req)
2017-01-22 21:01:12 +00:00
if err != nil {
if resp != nil {
_, _ = io.Copy(io.Discard, resp.Body)
2017-06-03 15:39:57 +00:00
_ = resp.Body.Close()
2017-01-22 21:01:12 +00:00
}
return nil, errors.Wrap(err, "client.Do")
}
2017-06-15 11:40:27 +00:00
if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close()
return nil, &notExistError{h}
2017-06-15 11:40:27 +00:00
}
2017-01-22 21:01:12 +00:00
if resp.StatusCode != 200 && resp.StatusCode != 206 {
2017-06-03 15:39:57 +00:00
_ = resp.Body.Close()
2017-05-28 10:31:19 +00:00
return nil, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
2017-01-22 21:01:12 +00:00
}
return resp.Body, nil
}
// Stat returns information about a blob.
2018-03-13 21:30:41 +00:00
func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodHead, b.Filename(h), nil)
2018-01-23 22:12:52 +00:00
if err != nil {
return restic.FileInfo{}, errors.WithStack(err)
2018-01-23 22:12:52 +00:00
}
2018-03-13 21:22:35 +00:00
req.Header.Set("Accept", ContentTypeV2)
2018-01-23 22:12:52 +00:00
resp, err := b.client.Do(req)
if err != nil {
return restic.FileInfo{}, errors.WithStack(err)
}
_, _ = io.Copy(io.Discard, resp.Body)
if err = resp.Body.Close(); err != nil {
2016-08-31 20:51:35 +00:00
return restic.FileInfo{}, errors.Wrap(err, "Close")
}
2017-06-15 11:40:27 +00:00
if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close()
return restic.FileInfo{}, &notExistError{h}
2017-06-15 11:40:27 +00:00
}
if resp.StatusCode != 200 {
2017-05-28 10:31:19 +00:00
return restic.FileInfo{}, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
}
if resp.ContentLength < 0 {
2016-08-31 20:51:35 +00:00
return restic.FileInfo{}, errors.New("negative content length")
}
2016-08-31 20:51:35 +00:00
bi := restic.FileInfo{
Size: resp.ContentLength,
Name: h.Name,
}
return bi, nil
}
// Remove removes the blob with the given name and type.
2018-03-13 21:30:41 +00:00
func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", b.Filename(h), nil)
if err != nil {
return errors.WithStack(err)
}
2018-03-13 21:22:35 +00:00
req.Header.Set("Accept", ContentTypeV2)
2018-01-23 22:12:52 +00:00
resp, err := b.client.Do(req)
if err != nil {
2016-08-29 19:54:50 +00:00
return errors.Wrap(err, "client.Do")
}
2017-06-15 11:40:27 +00:00
if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close()
return &notExistError{h}
2017-06-15 11:40:27 +00:00
}
if resp.StatusCode != 200 {
return errors.Errorf("blob not removed, server response: %v (%v)", resp.Status, resp.StatusCode)
}
_, err = io.Copy(io.Discard, resp.Body)
2017-06-03 15:39:57 +00:00
if err != nil {
return errors.Wrap(err, "Copy")
}
return errors.Wrap(resp.Body.Close(), "Close")
}
// List runs fn for each file in the backend which has the type t. When an
// error occurs (or fn returns an error), List stops and returns it.
2018-03-13 21:30:41 +00:00
func (b *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
2017-04-11 19:47:57 +00:00
url := b.Dirname(restic.Handle{Type: t})
2016-02-21 15:35:25 +00:00
if !strings.HasSuffix(url, "/") {
url += "/"
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
2018-01-23 22:12:52 +00:00
if err != nil {
return errors.WithStack(err)
2018-01-23 22:12:52 +00:00
}
2018-03-13 21:22:35 +00:00
req.Header.Set("Accept", ContentTypeV2)
2018-01-23 22:12:52 +00:00
resp, err := b.client.Do(req)
if err != nil {
return errors.Wrap(err, "List")
}
if resp.StatusCode != 200 {
return errors.Errorf("List failed, server response: %v (%v)", resp.Status, resp.StatusCode)
}
2018-03-13 21:22:35 +00:00
if resp.Header.Get("Content-Type") == ContentTypeV2 {
2018-01-23 22:12:52 +00:00
return b.listv2(ctx, t, resp, fn)
}
return b.listv1(ctx, t, resp, fn)
}
// listv1 uses the REST protocol v1, where a list HTTP request (e.g. `GET
// /data/`) only returns the names of the files, so we need to issue an HTTP
// HEAD request for each file.
2018-03-13 21:30:41 +00:00
func (b *Backend) listv1(ctx context.Context, t restic.FileType, resp *http.Response, fn func(restic.FileInfo) error) error {
2018-01-23 22:12:52 +00:00
debug.Log("parsing API v1 response")
dec := json.NewDecoder(resp.Body)
var list []string
2018-01-23 22:12:52 +00:00
if err := dec.Decode(&list); err != nil {
return errors.Wrap(err, "Decode")
}
for _, m := range list {
fi, err := b.Stat(ctx, restic.Handle{Name: m, Type: t})
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
fi.Name = m
err = fn(fi)
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
}
return ctx.Err()
}
2018-01-23 22:12:52 +00:00
// listv2 uses the REST protocol v2, where a list HTTP request (e.g. `GET
// /data/`) returns the names and sizes of all files.
2018-03-13 21:30:41 +00:00
func (b *Backend) listv2(ctx context.Context, t restic.FileType, resp *http.Response, fn func(restic.FileInfo) error) error {
2018-01-23 22:12:52 +00:00
debug.Log("parsing API v2 response")
dec := json.NewDecoder(resp.Body)
var list []struct {
Name string `json:"name"`
Size int64 `json:"size"`
}
if err := dec.Decode(&list); err != nil {
return errors.Wrap(err, "Decode")
}
for _, item := range list {
if ctx.Err() != nil {
return ctx.Err()
}
fi := restic.FileInfo{
Name: item.Name,
Size: item.Size,
}
err := fn(fi)
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
}
return ctx.Err()
}
// Close closes all open files.
2018-03-13 21:30:41 +00:00
func (b *Backend) Close() error {
// this does not need to do anything, all open files are closed within the
// same function.
return nil
}
// Delete removes all data in the backend.
2018-03-13 21:30:41 +00:00
func (b *Backend) Delete(ctx context.Context) error {
return backend.DefaultDelete(ctx, b)
}