forked from TrueCloudLab/frostfs-s3-gw
Merge pull request #126 from KirillovDenis/feature/94-time_based_conditional
[#94] CopyObject/GetObject support conditional headers
This commit is contained in:
commit
daed0978a6
4 changed files with 117 additions and 42 deletions
|
@ -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 != 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{
|
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 := ©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
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/api"
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
||||||
|
@ -12,6 +13,11 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type getObjectArgs struct {
|
||||||
|
IfModifiedSince *time.Time
|
||||||
|
IfUnmodifiedSince *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
|
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
|
||||||
const prefix = "bytes="
|
const prefix = "bytes="
|
||||||
rangeHeader := headers.Get("Range")
|
rangeHeader := headers.Get("Range")
|
||||||
|
@ -52,7 +58,7 @@ func writeHeaders(h http.Header, info *layer.ObjectInfo) {
|
||||||
if len(info.ContentType) > 0 {
|
if len(info.ContentType) > 0 {
|
||||||
h.Set(api.ContentType, info.ContentType)
|
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.ContentLength, strconv.FormatInt(info.Size, 10))
|
||||||
h.Set(api.ETag, info.HashSum)
|
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())
|
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 {
|
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)
|
writeError(w, r, h.log, "could not find object", rid, bkt, obj, err)
|
||||||
return
|
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 {
|
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)
|
writeError(w, r, h.log, "could not parse range header", rid, bkt, obj, err)
|
||||||
return
|
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) {
|
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))
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
// Standard S3 HTTP response constants.
|
// Standard S3 HTTP request/response constants.
|
||||||
const (
|
const (
|
||||||
LastModified = "Last-Modified"
|
LastModified = "Last-Modified"
|
||||||
Date = "Date"
|
Date = "Date"
|
||||||
|
@ -22,4 +22,9 @@ const (
|
||||||
ContentDisposition = "Content-Disposition"
|
ContentDisposition = "Content-Disposition"
|
||||||
Authorization = "Authorization"
|
Authorization = "Authorization"
|
||||||
Action = "Action"
|
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"
|
||||||
)
|
)
|
||||||
|
|
|
@ -69,6 +69,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.
|
||||||
|
@ -398,11 +399,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() {
|
||||||
|
@ -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{
|
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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue