2020-08-19 23:36:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-30 12:29:01 +00:00
|
|
|
"bytes"
|
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) {
|
2022-06-28 13:35:05 +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
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
conditional, err := parseConditionalHeaders(r.Header)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse request params", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
h.logAndSendError(w, "could not find object", reqInfo, err)
|
2020-08-19 23:36:34 +00:00
|
|
|
return
|
2021-06-30 12:29:01 +00:00
|
|
|
}
|
2022-06-28 13:35:05 +00:00
|
|
|
info := extendedInfo.ObjectInfo
|
2022-06-01 14:09:28 +00:00
|
|
|
|
|
|
|
if err = checkPreconditions(info, conditional); err != nil {
|
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(info.ContentType) == 0 {
|
2022-06-29 10:51:05 +00:00
|
|
|
if info.ContentType = layer.MimeByFileName(info.Name); len(info.ContentType) == 0 {
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
|
|
|
getParams := &layer.GetObjectParams{
|
|
|
|
ObjectInfo: info,
|
|
|
|
Writer: buffer,
|
|
|
|
Range: getRangeToDetectContentType(info.Size),
|
|
|
|
BucketInfo: bktInfo,
|
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", info.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
info.ContentType = http.DetectContentType(buffer.Bytes())
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-05-31 14:28:06 +00:00
|
|
|
func (h *handler) setLockingHeaders(bktInfo *data.BucketInfo, lockInfo *data.LockInfo, header http.Header) error {
|
2022-03-09 13:15:17 +00:00
|
|
|
if !bktInfo.ObjectLockEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
legalHold := &data.LegalHold{Status: legalHoldOff}
|
2022-05-26 13:11:14 +00:00
|
|
|
retention := &data.Retention{Mode: governanceMode}
|
2022-03-09 13:15:17 +00:00
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsLegalHoldSet() {
|
2022-05-31 14:28:06 +00:00
|
|
|
legalHold.Status = legalHoldOn
|
2022-03-09 13:15:17 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsRetentionSet() {
|
|
|
|
retention.RetainUntilDate = lockInfo.UntilDate()
|
|
|
|
if lockInfo.IsCompliance() {
|
2022-05-31 14:28:06 +00:00
|
|
|
retention.Mode = complianceMode
|
2022-03-09 13:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|