2020-08-19 23:36:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-30 12:29:01 +00:00
|
|
|
"bytes"
|
2022-03-09 13:15:17 +00:00
|
|
|
"context"
|
2020-08-19 23:36:34 +00:00
|
|
|
"net/http"
|
|
|
|
|
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-17 11:23:01 +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:34 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
const sizeToDetectType = 512
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
func getRangeToDetectContentType(maxSize int64) *layer.RangeParams {
|
|
|
|
end := uint64(maxSize)
|
|
|
|
if sizeToDetectType < end {
|
|
|
|
end = sizeToDetectType
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layer.RangeParams{
|
|
|
|
Start: 0,
|
|
|
|
End: end - 1,
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:34 +00:00
|
|
|
func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-08-19 06:55:22 +00:00
|
|
|
err error
|
2021-09-10 06:56:56 +00:00
|
|
|
info *data.ObjectInfo
|
2020-08-19 23:36:34 +00:00
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
2020-08-19 23:36:34 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if info, err = h.obj.GetObjectInfo(r.Context(), p); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not fetch object info", reqInfo, err)
|
2020-08-19 23:36:34 +00:00
|
|
|
return
|
2021-06-30 12:29:01 +00:00
|
|
|
}
|
2021-08-17 11:23:49 +00:00
|
|
|
tagSet, err := h.obj.GetObjectTagging(r.Context(), info)
|
2021-08-17 11:23:01 +00:00
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
h.logAndSendError(w, "could not get object tag set", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(info.ContentType) == 0 {
|
2021-08-18 13:48:58 +00:00
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
|
|
|
getParams := &layer.GetObjectParams{
|
2021-08-19 06:55:22 +00:00
|
|
|
ObjectInfo: info,
|
2021-08-18 13:48:58 +00:00
|
|
|
Writer: buffer,
|
2021-08-19 06:55:22 +00:00
|
|
|
Range: getRangeToDetectContentType(info.Size),
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
2021-08-27 21:33:50 +00:00
|
|
|
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", info.ID))
|
2021-08-18 13:48:58 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
info.ContentType = http.DetectContentType(buffer.Bytes())
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2022-03-09 13:15:17 +00:00
|
|
|
if err = h.setLockingHeaders(r.Context(), bktInfo, info, w.Header()); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get locking info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:23:01 +00:00
|
|
|
writeHeaders(w.Header(), info, len(tagSet))
|
2020-10-24 13:09:22 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-08-19 23:36:34 +00:00
|
|
|
}
|
2020-08-22 02:45:48 +00:00
|
|
|
|
|
|
|
func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-22 02:45:48 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-08-23 08:37:08 +00:00
|
|
|
if err != nil {
|
2021-08-23 08:19:41 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-22 02:45:48 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
w.Header().Set(api.ContainerID, bktInfo.CID.EncodeToString())
|
2020-08-22 02:45:48 +00:00
|
|
|
api.WriteResponse(w, http.StatusOK, nil, api.MimeNone)
|
|
|
|
}
|
2022-03-09 13:15:17 +00:00
|
|
|
|
|
|
|
func (h *handler) setLockingHeaders(ctx context.Context, bktInfo *data.BucketInfo, objInfo *data.ObjectInfo, header http.Header) error {
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
legalHold := &data.LegalHold{Status: legalHoldOff}
|
|
|
|
legalHoldInfo, err := h.obj.HeadSystemObject(ctx, bktInfo, objInfo.LegalHoldObject())
|
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if legalHoldInfo != nil {
|
|
|
|
legalHold.Status = legalHoldOn
|
|
|
|
}
|
|
|
|
|
|
|
|
retention := &data.Retention{Mode: governanceMode}
|
|
|
|
retentionInfo, err := h.obj.HeadSystemObject(ctx, bktInfo, objInfo.RetentionObject())
|
|
|
|
if err != nil && !errors.IsS3Error(err, errors.ErrNoSuchKey) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if retentionInfo != nil {
|
|
|
|
retention.RetainUntilDate = retentionInfo.Headers[layer.AttributeRetainUntil]
|
|
|
|
if retentionInfo.Headers[layer.AttributeComplianceMode] != "" {
|
|
|
|
retention.Mode = complianceMode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeLockHeaders(header, legalHold, retention)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLockHeaders(h http.Header, legalHold *data.LegalHold, retention *data.Retention) {
|
|
|
|
h.Set(api.AmzObjectLockLegalHold, legalHold.Status)
|
|
|
|
|
|
|
|
if retention.RetainUntilDate != "" {
|
|
|
|
h.Set(api.AmzObjectLockRetainUntilDate, retention.RetainUntilDate)
|
|
|
|
h.Set(api.AmzObjectLockMode, retention.Mode)
|
|
|
|
}
|
|
|
|
}
|