[#94] CopyObject support conditional headers

Supported X-Amz-Copy-Source-If-Modified-Since and
X-Amz-Copy-Source-If-Unmodified-Since.

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-01 10:45:55 +03:00
parent da4eca5da5
commit ab8dd4201c
4 changed files with 84 additions and 51 deletions

View file

@ -12,6 +12,11 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
type copyObjectArgs struct {
IfModifiedSince time.Time
IfUnmodifiedSince time.Time
}
// path2BucketObject returns bucket and object. // path2BucketObject returns bucket and object.
func path2BucketObject(path string) (bucket, prefix string) { func path2BucketObject(path string) (bucket, prefix string) {
path = strings.TrimPrefix(path, api.SlashSeparator) path = strings.TrimPrefix(path, api.SlashSeparator)
@ -58,44 +63,40 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
srcBucket, srcObject := path2BucketObject(src) srcBucket, srcObject := path2BucketObject(src)
args, err := parseCopyObjectArgs(r.Header)
if err != nil {
writeError(w, r, h.log, "could not parse request params ", rid, bkt, obj, err)
return
}
if inf, err = h.obj.GetObjectInfo(r.Context(), srcBucket, srcObject); err != nil {
writeError(w, r, h.log, "could not find object", rid, bkt, obj, err)
return
}
if !args.IfModifiedSince.IsZero() && inf.Created.Before(args.IfModifiedSince) {
w.WriteHeader(http.StatusNotModified)
return
}
if !args.IfUnmodifiedSince.IsZero() && inf.Created.After(args.IfUnmodifiedSince) {
w.WriteHeader(http.StatusPreconditionFailed)
return
}
params := &layer.CopyObjectParams{ params := &layer.CopyObjectParams{
SrcBucket: srcBucket, SrcBucket: srcBucket,
DstBucket: bkt, DstBucket: bkt,
SrcObject: srcObject, SrcObject: srcObject,
DstObject: obj, DstObject: obj,
SrcSize: inf.Size,
Header: inf.Headers,
} }
if inf, err = h.obj.CopyObject(r.Context(), params); err != nil { if inf, err = h.obj.CopyObject(r.Context(), params); err != nil {
h.log.Error("could not copy object", writeErrorCopy(w, r, h.log, "could not copy object", rid, bkt, obj, srcBucket, srcObject, err)
zap.String("request_id", rid),
zap.String("dst_bucket_name", bkt),
zap.String("dst_object_name", obj),
zap.String("src_bucket_name", srcBucket),
zap.String("src_object_name", srcObject),
zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{
Code: api.GetAPIError(api.ErrInternalError).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusInternalServerError,
}, r.URL)
return return
} else if err = api.EncodeToResponse(w, &CopyObjectResponse{LastModified: inf.Created.Format(time.RFC3339), ETag: inf.HashSum}); err != nil { } else if err = api.EncodeToResponse(w, &CopyObjectResponse{LastModified: inf.Created.Format(time.RFC3339), ETag: inf.HashSum}); err != nil {
h.log.Error("something went wrong", writeErrorCopy(w, r, h.log, "something went wrong", rid, bkt, obj, srcBucket, srcObject, err)
zap.String("request_id", rid),
zap.String("dst_bucket_name", bkt),
zap.String("dst_object_name", obj),
zap.String("src_bucket_name", srcBucket),
zap.String("src_object_name", srcObject),
zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{
Code: api.GetAPIError(api.ErrInternalError).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusInternalServerError,
}, r.URL)
return return
} }
@ -104,3 +105,33 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
zap.String("object", inf.Name), zap.String("object", inf.Name),
zap.Stringer("object_id", inf.ID())) zap.Stringer("object_id", inf.ID()))
} }
func writeErrorCopy(w http.ResponseWriter, r *http.Request, log *zap.Logger, msg, rid, bkt, obj, srcBucket, srcObject string, err error) {
log.Error(msg,
zap.String("request_id", rid),
zap.String("dst_bucket_name", bkt),
zap.String("dst_object_name", obj),
zap.String("src_bucket_name", srcBucket),
zap.String("src_object_name", srcObject),
zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{
Code: api.GetAPIError(api.ErrInternalError).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusInternalServerError,
}, r.URL)
}
func parseCopyObjectArgs(headers http.Header) (*copyObjectArgs, error) {
var err error
args := &copyObjectArgs{}
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfModifiedSince)); err != nil {
return nil, err
}
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfUnmodifiedSince)); err != nil {
return nil, err
}
return args, nil
}

View file

@ -81,6 +81,7 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
args, err := parseGetObjectArgs(r.Header) args, err := parseGetObjectArgs(r.Header)
if err != nil { if err != nil {
writeError(w, r, h.log, "could not parse request params", rid, bkt, obj, err)
return return
} }
@ -122,23 +123,30 @@ func parseGetObjectArgs(headers http.Header) (*getObjectArgs, error) {
var err error var err error
args := &getObjectArgs{} args := &getObjectArgs{}
ifModifiedSince := headers.Get(api.IfModifiedSince) if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil {
if len(ifModifiedSince) > 0 { return nil, err
if args.IfModifiedSince, err = time.Parse(http.TimeFormat, ifModifiedSince); err != nil {
return nil, fmt.Errorf("couldn't parse %s header: %w", api.IfModifiedSince, err)
}
}
ifUnmodifiedSince := headers.Get(api.IfUnmodifiedSince)
if len(ifUnmodifiedSince) > 0 {
if args.IfUnmodifiedSince, err = time.Parse(http.TimeFormat, ifUnmodifiedSince); err != nil {
return nil, fmt.Errorf("couldn't parse %s header: %w", api.IfUnmodifiedSince, err)
} }
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil {
return nil, err
} }
return args, nil return args, nil
} }
func parseHTTPTime(data string) (time.Time, error) {
var result time.Time
var err error
if len(data) == 0 {
return result, nil
}
if result, err = time.Parse(http.TimeFormat, data); err != nil {
return result, fmt.Errorf("couldn't parse http time %s: %w", data, err)
}
return result, nil
}
func writeRangeHeaders(w http.ResponseWriter, params *layer.RangeParams, size int64) { func writeRangeHeaders(w http.ResponseWriter, params *layer.RangeParams, size int64) {
w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", params.Start, params.End, size)) w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", params.Start, params.End, size))

View file

@ -24,4 +24,7 @@ const (
Action = "Action" Action = "Action"
IfModifiedSince = "If-Modified-Since" IfModifiedSince = "If-Modified-Since"
IfUnmodifiedSince = "If-Unmodified-Since" IfUnmodifiedSince = "If-Unmodified-Since"
AmzCopyIfModifiedSince = "X-Amz-Copy-Source-If-Modified-Since"
AmzCopyIfUnmodifiedSince = "X-Amz-Copy-Source-If-Unmodified-Since"
) )

View file

@ -68,6 +68,7 @@ type (
DstBucket string DstBucket string
SrcObject string SrcObject string
DstObject string DstObject string
SrcSize int64
Header map[string]string Header map[string]string
} }
// CreateBucketParams stores bucket create request parameters. // CreateBucketParams stores bucket create request parameters.
@ -378,11 +379,6 @@ func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
// CopyObject from one bucket into another bucket. // CopyObject from one bucket into another bucket.
func (n *layer) CopyObject(ctx context.Context, p *CopyObjectParams) (*ObjectInfo, error) { func (n *layer) CopyObject(ctx context.Context, p *CopyObjectParams) (*ObjectInfo, error) {
info, err := n.GetObjectInfo(ctx, p.SrcBucket, p.SrcObject)
if err != nil {
return nil, fmt.Errorf("couldn't get object info: %w", err)
}
pr, pw := io.Pipe() pr, pw := io.Pipe()
go func() { go func() {
@ -397,17 +393,12 @@ func (n *layer) CopyObject(ctx context.Context, p *CopyObjectParams) (*ObjectInf
} }
}() }()
// set custom headers
for k, v := range p.Header {
info.Headers[k] = v
}
return n.PutObject(ctx, &PutObjectParams{ return n.PutObject(ctx, &PutObjectParams{
Bucket: p.DstBucket, Bucket: p.DstBucket,
Object: p.DstObject, Object: p.DstObject,
Size: info.Size, Size: p.SrcSize,
Reader: pr, Reader: pr,
Header: info.Headers, Header: p.Header,
}) })
} }