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"
|
2022-01-21 09:52:16 +00:00
|
|
|
"net/url"
|
2020-08-19 23:36:46 +00:00
|
|
|
"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-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
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-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
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:52:16 +00:00
|
|
|
func overrideResponseHeaders(h http.Header, query url.Values) {
|
|
|
|
for key, value := range query {
|
|
|
|
if hdr, ok := api.ResponseModifiers[strings.ToLower(key)]; ok {
|
|
|
|
h[hdr] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:35:26 +00:00
|
|
|
func writeHeaders(h http.Header, extendedInfo *data.ExtendedObjectInfo, tagSetLength int) {
|
|
|
|
info := extendedInfo.ObjectInfo
|
2022-01-21 09:52:16 +00:00
|
|
|
if len(info.ContentType) > 0 && h.Get(api.ContentType) == "" {
|
2021-06-30 12:29:01 +00:00
|
|
|
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)
|
2022-08-08 22:35:26 +00:00
|
|
|
h.Set(api.AmzVersionID, extendedInfo.Version())
|
2021-08-17 11:23:01 +00:00
|
|
|
h.Set(api.AmzTaggingCount, strconv.Itoa(tagSetLength))
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2022-01-27 13:37:43 +00:00
|
|
|
if cacheControl := info.Headers[api.CacheControl]; cacheControl != "" {
|
|
|
|
h.Set(api.CacheControl, cacheControl)
|
|
|
|
}
|
|
|
|
if expires := info.Headers[api.Expires]; expires != "" {
|
|
|
|
h.Set(api.Expires, expires)
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
for key, val := range info.Headers {
|
2022-01-20 14:36:21 +00:00
|
|
|
if layer.IsSystemHeader(key) {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
conditional, err := parseConditionalHeaders(r.Header)
|
2021-06-30 15:10:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-03-09 13:15:17 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-08-23 08:19:41 +00:00
|
|
|
|
2021-08-10 10:03:09 +00:00
|
|
|
p := &layer.HeadObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
2021-08-10 10:03:09 +00:00
|
|
|
Object: reqInfo.ObjectName,
|
2021-08-19 06:55:22 +00:00
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:35:05 +00:00
|
|
|
extendedInfo, err := h.obj.GetObjectInfo(r.Context(), p)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
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
|
|
|
}
|
2022-06-28 13:35:05 +00:00
|
|
|
info := extendedInfo.ObjectInfo
|
2021-06-30 15:10:11 +00:00
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
if err = checkPreconditions(info, 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-08-19 06:55:22 +00:00
|
|
|
if params, err = fetchRangeHeader(r.Header, uint64(info.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-08-17 11:23:01 +00:00
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
t := &layer.ObjectVersion{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: info.Name,
|
2022-08-04 17:31:33 +00:00
|
|
|
VersionID: info.VersionID(),
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:35:05 +00:00
|
|
|
tagSet, lockInfo, err := h.obj.GetObjectTaggingAndLock(r.Context(), t, extendedInfo.NodeVersion)
|
2021-08-17 11:23:01 +00:00
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
2022-05-31 14:28:06 +00:00
|
|
|
h.logAndSendError(w, "could not get object meta data", reqInfo, err)
|
2021-08-17 11:23:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:52:16 +00:00
|
|
|
if layer.IsAuthenticatedRequest(r.Context()) {
|
|
|
|
overrideResponseHeaders(w.Header(), reqInfo.URL.Query())
|
|
|
|
}
|
2022-03-09 13:15:17 +00:00
|
|
|
|
2022-05-31 14:28:06 +00:00
|
|
|
if err = h.setLockingHeaders(bktInfo, lockInfo, w.Header()); err != nil {
|
2022-03-09 13:15:17 +00:00
|
|
|
h.logAndSendError(w, "could not get locking info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:35:26 +00:00
|
|
|
writeHeaders(w.Header(), extendedInfo, len(tagSet))
|
2021-06-24 11:10:00 +00:00
|
|
|
if params != nil {
|
2021-08-19 06:55:22 +00:00
|
|
|
writeRangeHeaders(w, params, info.Size)
|
2021-09-21 09:31:23 +00:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
2021-06-30 12:29:01 +00:00
|
|
|
|
|
|
|
getParams := &layer.GetObjectParams{
|
2021-08-19 06:55:22 +00:00
|
|
|
ObjectInfo: info,
|
2021-08-10 10:03:09 +00:00
|
|
|
Writer: w,
|
|
|
|
Range: params,
|
2022-06-02 16:56:04 +00:00
|
|
|
BucketInfo: bktInfo,
|
2021-06-30 12:29:01 +00:00
|
|
|
}
|
|
|
|
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-09-10 06:56:56 +00:00
|
|
|
func checkPreconditions(info *data.ObjectInfo, args *conditionalArgs) error {
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(args.IfMatch) > 0 && args.IfMatch != info.HashSum {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrPreconditionFailed)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(args.IfNoneMatch) > 0 && args.IfNoneMatch == info.HashSum {
|
2021-08-09 08:53:58 +00:00
|
|
|
return errors.GetAPIError(errors.ErrNotModified)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
if args.IfModifiedSince != nil && info.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
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
if args.IfUnmodifiedSince != nil && info.Created.After(*args.IfUnmodifiedSince) {
|
2021-07-02 16:05:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
func parseConditionalHeaders(headers http.Header) (*conditionalArgs, error) {
|
2021-06-30 15:10:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
return 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)
|
|
|
|
}
|