diff --git a/api/handler/copy.go b/api/handler/copy.go index 460c93e7c..aa6009d9d 100644 --- a/api/handler/copy.go +++ b/api/handler/copy.go @@ -12,6 +12,11 @@ import ( "go.uber.org/zap" ) +type copyObjectArgs struct { + IfModifiedSince time.Time + IfUnmodifiedSince time.Time +} + // path2BucketObject returns bucket and object. func path2BucketObject(path string) (bucket, prefix string) { path = strings.TrimPrefix(path, api.SlashSeparator) @@ -58,44 +63,40 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) { 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{ SrcBucket: srcBucket, DstBucket: bkt, SrcObject: srcObject, DstObject: obj, + SrcSize: inf.Size, + Header: inf.Headers, } if inf, err = h.obj.CopyObject(r.Context(), params); err != nil { - h.log.Error("could not copy object", - 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) - + writeErrorCopy(w, r, h.log, "could not copy object", rid, bkt, obj, srcBucket, srcObject, err) return } else if err = api.EncodeToResponse(w, &CopyObjectResponse{LastModified: inf.Created.Format(time.RFC3339), ETag: inf.HashSum}); err != nil { - h.log.Error("something went wrong", - 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) - + writeErrorCopy(w, r, h.log, "something went wrong", rid, bkt, obj, srcBucket, srcObject, err) return } @@ -104,3 +105,33 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) { zap.String("object", inf.Name), 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 := ©ObjectArgs{} + + 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 +} diff --git a/api/handler/get.go b/api/handler/get.go index 2450062c0..c431d09e7 100644 --- a/api/handler/get.go +++ b/api/handler/get.go @@ -81,6 +81,7 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) { args, err := parseGetObjectArgs(r.Header) if err != nil { + writeError(w, r, h.log, "could not parse request params", rid, bkt, obj, err) return } @@ -122,23 +123,30 @@ func parseGetObjectArgs(headers http.Header) (*getObjectArgs, error) { var err error args := &getObjectArgs{} - ifModifiedSince := headers.Get(api.IfModifiedSince) - if len(ifModifiedSince) > 0 { - if args.IfModifiedSince, err = time.Parse(http.TimeFormat, ifModifiedSince); err != nil { - return nil, fmt.Errorf("couldn't parse %s header: %w", api.IfModifiedSince, err) - } + if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil { + return nil, 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 } +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) { w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", params.Start, params.End, size)) diff --git a/api/headers.go b/api/headers.go index 4e8380f4e..735ebb69f 100644 --- a/api/headers.go +++ b/api/headers.go @@ -24,4 +24,7 @@ const ( Action = "Action" IfModifiedSince = "If-Modified-Since" IfUnmodifiedSince = "If-Unmodified-Since" + + AmzCopyIfModifiedSince = "X-Amz-Copy-Source-If-Modified-Since" + AmzCopyIfUnmodifiedSince = "X-Amz-Copy-Source-If-Unmodified-Since" ) diff --git a/api/layer/layer.go b/api/layer/layer.go index 30593e503..563ba8fe5 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -68,6 +68,7 @@ type ( DstBucket string SrcObject string DstObject string + SrcSize int64 Header map[string]string } // 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. 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() 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{ Bucket: p.DstBucket, Object: p.DstObject, - Size: info.Size, + Size: p.SrcSize, Reader: pr, - Header: info.Headers, + Header: p.Header, }) }