Merge pull request #126 from KirillovDenis/feature/94-time_based_conditional

[#94] CopyObject/GetObject support conditional headers
This commit is contained in:
Roman Khimov 2021-07-02 17:19:16 +03:00 committed by GitHub
commit daed0978a6
4 changed files with 117 additions and 42 deletions

View file

@ -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 != nil && inf.Created.Before(*args.IfModifiedSince) {
w.WriteHeader(http.StatusNotModified)
return
}
if args.IfUnmodifiedSince != nil && 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 := &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

@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/nspcc-dev/neofs-s3-gw/api"
@ -12,6 +13,11 @@ import (
"go.uber.org/zap"
)
type getObjectArgs struct {
IfModifiedSince *time.Time
IfUnmodifiedSince *time.Time
}
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
const prefix = "bytes="
rangeHeader := headers.Get("Range")
@ -52,7 +58,7 @@ func writeHeaders(h http.Header, info *layer.ObjectInfo) {
if len(info.ContentType) > 0 {
h.Set(api.ContentType, info.ContentType)
}
h.Set(api.LastModified, info.Created.Format(http.TimeFormat))
h.Set(api.LastModified, info.Created.Format(time.RFC3339))
h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10))
h.Set(api.ETag, info.HashSum)
@ -73,10 +79,26 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
rid = api.GetRequestID(r.Context())
)
args, err := parseGetObjectArgs(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(), bkt, obj); err != nil {
writeError(w, r, h.log, "could not find object", rid, bkt, obj, err)
return
}
if args.IfModifiedSince != nil && inf.Created.Before(*args.IfModifiedSince) {
w.WriteHeader(http.StatusNotModified)
return
}
if args.IfUnmodifiedSince != nil && inf.Created.After(*args.IfUnmodifiedSince) {
w.WriteHeader(http.StatusPreconditionFailed)
return
}
if params, err = fetchRangeHeader(r.Header, uint64(inf.Size)); err != nil {
writeError(w, r, h.log, "could not parse range header", rid, bkt, obj, err)
return
@ -97,6 +119,32 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
}
}
func parseGetObjectArgs(headers http.Header) (*getObjectArgs, error) {
var err error
args := &getObjectArgs{}
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil {
return nil, 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) {
if len(data) == 0 {
return nil, nil
}
result, err := time.Parse(http.TimeFormat, data)
if err != nil {
return nil, 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))

View file

@ -1,6 +1,6 @@
package api
// Standard S3 HTTP response constants.
// Standard S3 HTTP request/response constants.
const (
LastModified = "Last-Modified"
Date = "Date"
@ -22,4 +22,9 @@ const (
ContentDisposition = "Content-Disposition"
Authorization = "Authorization"
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"
)

View file

@ -69,6 +69,7 @@ type (
DstBucket string
SrcObject string
DstObject string
SrcSize int64
Header map[string]string
}
// CreateBucketParams stores bucket create request parameters.
@ -398,11 +399,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() {
@ -417,17 +413,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,
})
}