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
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2022-08-01 16:52:09 +00:00
|
|
|
"go.uber.org/zap"
|
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="
|
2024-09-10 14:10:59 +00:00
|
|
|
rangeHeader := headers.Get(api.Range)
|
2021-06-24 11:10:00 +00:00
|
|
|
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-01 16:52:09 +00:00
|
|
|
func addSSECHeaders(responseHeader http.Header, requestHeader http.Header) {
|
|
|
|
responseHeader.Set(api.AmzServerSideEncryptionCustomerAlgorithm, requestHeader.Get(api.AmzServerSideEncryptionCustomerAlgorithm))
|
|
|
|
responseHeader.Set(api.AmzServerSideEncryptionCustomerKeyMD5, requestHeader.Get(api.AmzServerSideEncryptionCustomerKeyMD5))
|
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
func writeHeaders(h http.Header, requestHeader http.Header, extendedInfo *data.ExtendedObjectInfo, tagSetLength int,
|
|
|
|
isBucketUnversioned, md5Enabled bool) {
|
2022-08-08 22:35:26 +00:00
|
|
|
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))
|
2022-08-01 16:52:09 +00:00
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
if len(info.Headers[layer.AttributeEncryptionAlgorithm]) > 0 {
|
2022-08-01 16:52:09 +00:00
|
|
|
h.Set(api.ContentLength, info.Headers[layer.AttributeDecryptedSize])
|
|
|
|
addSSECHeaders(h, requestHeader)
|
2023-06-27 12:49:20 +00:00
|
|
|
} else if len(info.Headers[layer.MultipartObjectSize]) > 0 {
|
|
|
|
h.Set(api.ContentLength, info.Headers[layer.MultipartObjectSize])
|
2022-08-01 16:52:09 +00:00
|
|
|
} else {
|
2023-06-01 13:45:28 +00:00
|
|
|
h.Set(api.ContentLength, strconv.FormatUint(info.Size, 10))
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
h.Set(api.ETag, data.Quote(info.ETag(md5Enabled)))
|
2023-10-02 08:52:07 +00:00
|
|
|
|
2021-08-17 11:23:01 +00:00
|
|
|
h.Set(api.AmzTaggingCount, strconv.Itoa(tagSetLength))
|
2023-10-25 11:49:39 +00:00
|
|
|
h.Set(api.AmzStorageClass, api.DefaultStorageClass)
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2022-08-05 01:15:24 +00:00
|
|
|
if !isBucketUnversioned {
|
|
|
|
h.Set(api.AmzVersionID, extendedInfo.Version())
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-07-10 09:17:44 +00:00
|
|
|
if encodings := info.Headers[api.ContentEncoding]; encodings != "" {
|
|
|
|
h.Set(api.ContentEncoding, encodings)
|
|
|
|
}
|
2023-10-16 15:32:08 +00:00
|
|
|
if contentLanguage := info.Headers[api.ContentLanguage]; contentLanguage != "" {
|
|
|
|
h.Set(api.ContentLanguage, contentLanguage)
|
|
|
|
}
|
2022-01-27 13:37:43 +00:00
|
|
|
|
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
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo = middleware.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-08-05 00:54:21 +00:00
|
|
|
extendedInfo, err := h.obj.GetExtendedObjectInfo(r.Context(), p)
|
2022-06-28 13:35:05 +00:00
|
|
|
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
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
if err = checkPreconditions(info, conditional, h.cfg.MD5Enabled()); 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
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:07:18 +00:00
|
|
|
encryptionParams, err := formEncryptionParams(r)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid sse headers", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
if err = encryptionParams.MatchObjectEncryption(layer.FormEncryptionInfo(info.Headers)); err != nil {
|
2022-08-01 16:52:09 +00:00
|
|
|
h.logAndSendError(w, "encryption doesn't match object", reqInfo, errors.GetAPIError(errors.ErrBadRequest), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:54:12 +00:00
|
|
|
fullSize, err := layer.GetObjectSize(info)
|
2023-06-27 12:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid size header", reqInfo, errors.GetAPIError(errors.ErrBadRequest))
|
|
|
|
return
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:45:28 +00:00
|
|
|
if params, err = fetchRangeHeader(r.Header, fullSize); 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
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
t := &data.ObjectVersion{
|
2022-05-26 13:11:14 +00:00
|
|
|
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-05 01:15:24 +00:00
|
|
|
bktSettings, err := h.obj.GetBucketSettings(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket settings", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-06 13:37:53 +00:00
|
|
|
getPayloadParams := &layer.GetObjectParams{
|
|
|
|
ObjectInfo: info,
|
|
|
|
Versioned: p.Versioned(),
|
|
|
|
Range: params,
|
|
|
|
BucketInfo: bktInfo,
|
|
|
|
Encryption: encryptionParams,
|
|
|
|
}
|
|
|
|
|
|
|
|
objPayload, err := h.obj.GetObject(r.Context(), getPayloadParams)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object payload", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
writeHeaders(w.Header(), r.Header, extendedInfo, len(tagSet), bktSettings.Unversioned(), h.cfg.MD5Enabled())
|
2021-06-24 11:10:00 +00:00
|
|
|
if params != nil {
|
2023-06-27 12:49:20 +00:00
|
|
|
writeRangeHeaders(w, params, fullSize)
|
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
|
|
|
|
2023-07-06 13:37:53 +00:00
|
|
|
if err = objPayload.StreamTo(w); err != nil {
|
|
|
|
h.logAndSendError(w, "could not stream object payload", reqInfo, err)
|
|
|
|
return
|
2021-06-30 09:29:43 +00:00
|
|
|
}
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
func checkPreconditions(info *data.ObjectInfo, args *conditionalArgs, md5Enabled bool) error {
|
|
|
|
etag := info.ETag(md5Enabled)
|
|
|
|
if len(args.IfMatch) > 0 && args.IfMatch != etag {
|
|
|
|
return fmt.Errorf("%w: etag mismatched: '%s', '%s'", errors.GetAPIError(errors.ErrPreconditionFailed), args.IfMatch, etag)
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
2023-10-27 15:15:33 +00:00
|
|
|
if len(args.IfNoneMatch) > 0 && args.IfNoneMatch == etag {
|
|
|
|
return fmt.Errorf("%w: etag matched: '%s', '%s'", errors.GetAPIError(errors.ErrNotModified), args.IfNoneMatch, etag)
|
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) {
|
2023-07-10 06:59:12 +00:00
|
|
|
return fmt.Errorf("%w: not modified since '%s', last modified '%s'", errors.GetAPIError(errors.ErrNotModified),
|
|
|
|
args.IfModifiedSince.Format(time.RFC3339), info.Created.Format(time.RFC3339))
|
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) {
|
2023-07-10 06:59:12 +00:00
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html#API_GetObject_RequestSyntax
|
|
|
|
// If both of the If-Match and If-Unmodified-Since headers are present in the request as follows:
|
|
|
|
// If-Match condition evaluates to true, and;
|
|
|
|
// If-Unmodified-Since condition evaluates to false;
|
|
|
|
// then, S3 returns 200 OK and the data requested.
|
2021-07-02 16:05:43 +00:00
|
|
|
if len(args.IfMatch) == 0 {
|
2023-07-10 06:59:12 +00:00
|
|
|
return fmt.Errorf("%w: modified since '%s', last modified '%s'", errors.GetAPIError(errors.ErrPreconditionFailed),
|
|
|
|
args.IfUnmodifiedSince.Format(time.RFC3339), info.Created.Format(time.RFC3339))
|
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{
|
2023-10-27 15:15:33 +00:00
|
|
|
IfMatch: data.UnQuote(headers.Get(api.IfMatch)),
|
|
|
|
IfNoneMatch: data.UnQuote(headers.Get(api.IfNoneMatch)),
|
2021-07-02 16:05:43 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-06-01 13:45:28 +00:00
|
|
|
func writeRangeHeaders(w http.ResponseWriter, params *layer.RangeParams, size uint64) {
|
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)
|
|
|
|
}
|