2020-08-19 23:36:46 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-24 11:10:00 +00:00
|
|
|
"fmt"
|
2020-08-19 23:36:46 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-06-24 11:10:00 +00:00
|
|
|
"strings"
|
2021-06-30 15:10:11 +00:00
|
|
|
"time"
|
2020-08-19 23:36:46 +00:00
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:36:46 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 16:21:53 +00:00
|
|
|
type conditionalArgs struct {
|
2021-07-01 11:24:00 +00:00
|
|
|
IfModifiedSince *time.Time
|
|
|
|
IfUnmodifiedSince *time.Time
|
2021-07-02 16:05:43 +00:00
|
|
|
IfMatch string
|
|
|
|
IfNoneMatch string
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 16:21:53 +00:00
|
|
|
type getObjectArgs struct {
|
|
|
|
Conditional *conditionalArgs
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
|
|
|
|
const prefix = "bytes="
|
|
|
|
rangeHeader := headers.Get("Range")
|
|
|
|
if len(rangeHeader) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-08-06 09:59:28 +00:00
|
|
|
if fullSize == 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
2021-08-06 09:59:28 +00:00
|
|
|
}
|
2021-06-24 11:10:00 +00:00
|
|
|
if !strings.HasPrefix(rangeHeader, prefix) {
|
|
|
|
return nil, fmt.Errorf("unknown unit in range header")
|
|
|
|
}
|
|
|
|
arr := strings.Split(strings.TrimPrefix(rangeHeader, prefix), "-")
|
|
|
|
if len(arr) != 2 || (len(arr[0]) == 0 && len(arr[1]) == 0) {
|
|
|
|
return nil, fmt.Errorf("unknown byte-range-set")
|
|
|
|
}
|
|
|
|
|
|
|
|
var end, start uint64
|
|
|
|
var err0, err1 error
|
|
|
|
base, bitSize := 10, 64
|
|
|
|
|
|
|
|
if len(arr[0]) == 0 {
|
|
|
|
end, err1 = strconv.ParseUint(arr[1], base, bitSize)
|
|
|
|
start = fullSize - end
|
|
|
|
end = fullSize - 1
|
|
|
|
} else if len(arr[1]) == 0 {
|
|
|
|
start, err0 = strconv.ParseUint(arr[0], base, bitSize)
|
|
|
|
end = fullSize - 1
|
|
|
|
} else {
|
|
|
|
start, err0 = strconv.ParseUint(arr[0], base, bitSize)
|
|
|
|
end, err1 = strconv.ParseUint(arr[1], base, bitSize)
|
2021-08-06 09:59:28 +00:00
|
|
|
if end > fullSize-1 {
|
|
|
|
end = fullSize - 1
|
|
|
|
}
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 09:59:28 +00:00
|
|
|
if err0 != nil || err1 != nil || start > end || start > fullSize {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
return &layer.RangeParams{Start: start, End: end}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
func writeHeaders(h http.Header, info *layer.ObjectInfo) {
|
2021-06-30 12:29:01 +00:00
|
|
|
if len(info.ContentType) > 0 {
|
|
|
|
h.Set(api.ContentType, info.ContentType)
|
|
|
|
}
|
2021-07-06 11:07:09 +00:00
|
|
|
h.Set(api.LastModified, info.Created.UTC().Format(http.TimeFormat))
|
2021-06-29 13:40:26 +00:00
|
|
|
h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10))
|
|
|
|
h.Set(api.ETag, info.HashSum)
|
2021-01-14 17:39:48 +00:00
|
|
|
|
|
|
|
for key, val := range info.Headers {
|
2021-08-06 11:46:19 +00:00
|
|
|
h[api.MetadataPrefix+key] = []string{val}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:46 +00:00
|
|
|
func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-06-24 11:10:00 +00:00
|
|
|
err error
|
|
|
|
inf *layer.ObjectInfo
|
|
|
|
params *layer.RangeParams
|
2020-08-19 23:36:46 +00:00
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
2020-08-19 23:36:46 +00:00
|
|
|
)
|
|
|
|
|
2021-06-30 15:10:11 +00:00
|
|
|
args, err := parseGetObjectArgs(r.Header)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse request params", reqInfo, err)
|
2021-06-30 15:10:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
if err = h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), reqInfo.BucketName, reqInfo.ObjectName); err != nil {
|
|
|
|
h.logAndSendError(w, "could not find object", reqInfo, err)
|
2020-08-19 23:36:46 +00:00
|
|
|
return
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
2021-06-30 15:10:11 +00:00
|
|
|
|
2021-07-14 12:51:45 +00:00
|
|
|
if err = checkPreconditions(inf, args.Conditional); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, err)
|
2021-06-30 15:10:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
if params, err = fetchRangeHeader(r.Header, uint64(inf.Size)); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse range header", reqInfo, err)
|
2021-06-24 11:10:00 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
writeHeaders(w.Header(), inf)
|
2021-06-24 11:10:00 +00:00
|
|
|
if params != nil {
|
|
|
|
writeRangeHeaders(w, params, inf.Size)
|
|
|
|
}
|
2021-06-30 12:29:01 +00:00
|
|
|
|
|
|
|
getParams := &layer.GetObjectParams{
|
|
|
|
Bucket: inf.Bucket,
|
|
|
|
Object: inf.Name,
|
|
|
|
Writer: w,
|
|
|
|
Range: params,
|
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not get object", reqInfo, err)
|
2021-06-30 09:29:43 +00:00
|
|
|
}
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 12:51:45 +00:00
|
|
|
func checkPreconditions(inf *layer.ObjectInfo, args *conditionalArgs) error {
|
2021-07-02 16:05:43 +00:00
|
|
|
if len(args.IfMatch) > 0 && args.IfMatch != inf.HashSum {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrPreconditionFailed)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
|
|
|
if len(args.IfNoneMatch) > 0 && args.IfNoneMatch == inf.HashSum {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrNotModified)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
|
|
|
if args.IfModifiedSince != nil && inf.Created.Before(*args.IfModifiedSince) {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrNotModified)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
|
|
|
if args.IfUnmodifiedSince != nil && inf.Created.After(*args.IfUnmodifiedSince) {
|
|
|
|
if len(args.IfMatch) == 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrPreconditionFailed)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 12:51:45 +00:00
|
|
|
return nil
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:10:11 +00:00
|
|
|
func parseGetObjectArgs(headers http.Header) (*getObjectArgs, error) {
|
|
|
|
var err error
|
2021-07-02 16:21:53 +00:00
|
|
|
args := &conditionalArgs{
|
2021-07-02 16:05:43 +00:00
|
|
|
IfMatch: headers.Get(api.IfMatch),
|
|
|
|
IfNoneMatch: headers.Get(api.IfNoneMatch),
|
|
|
|
}
|
2021-06-30 15:10:11 +00:00
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil {
|
|
|
|
return nil, err
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil {
|
|
|
|
return nil, err
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 16:21:53 +00:00
|
|
|
return &getObjectArgs{Conditional: args}, nil
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:24:00 +00:00
|
|
|
func parseHTTPTime(data string) (*time.Time, error) {
|
2021-07-01 07:45:55 +00:00
|
|
|
if len(data) == 0 {
|
2021-07-01 11:24:00 +00:00
|
|
|
return nil, nil
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:24:00 +00:00
|
|
|
result, err := time.Parse(http.TimeFormat, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse http time %s: %w", data, err)
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|
2021-07-01 11:24:00 +00:00
|
|
|
return &result, nil
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
func writeRangeHeaders(w http.ResponseWriter, params *layer.RangeParams, size int64) {
|
2021-08-06 09:59:28 +00:00
|
|
|
w.Header().Set(api.AcceptRanges, "bytes")
|
|
|
|
w.Header().Set(api.ContentRange, fmt.Sprintf("bytes %d-%d/%d", params.Start, params.End, size))
|
|
|
|
w.Header().Set(api.ContentLength, strconv.FormatUint(params.End-params.Start+1, 10))
|
2021-06-24 11:10:00 +00:00
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
|
|
}
|