2022-05-12 21:15:08 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-06-22 19:40:52 +00:00
|
|
|
"fmt"
|
2022-05-12 21:15:08 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
GetObjectAttributesResponse struct {
|
|
|
|
ETag string `xml:"ETag,omitempty"`
|
|
|
|
ObjectSize int64 `xml:"ObjectSize,omitempty"`
|
|
|
|
StorageClass string `xml:"StorageClass,omitempty"`
|
|
|
|
ObjectParts *ObjectParts `xml:"ObjectParts,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectParts struct {
|
|
|
|
IsTruncated bool `xml:"IsTruncated,omitempty"`
|
|
|
|
MaxParts int `xml:"MaxParts,omitempty"`
|
|
|
|
NextPartNumberMarker int `xml:"NextPartNumberMarker,omitempty"`
|
|
|
|
PartNumberMarker int `xml:"PartNumberMarker,omitempty"`
|
|
|
|
Parts []Part `xml:"Part,omitempty"`
|
2022-06-01 09:54:27 +00:00
|
|
|
|
|
|
|
// Only this field is used.
|
|
|
|
PartsCount int `xml:"PartsCount,omitempty"`
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Part struct {
|
|
|
|
PartNumber int `xml:"PartNumber,omitempty"`
|
|
|
|
Size int `xml:"Size,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
GetObjectAttributesArgs struct {
|
2022-06-01 09:54:27 +00:00
|
|
|
Attributes []string
|
|
|
|
VersionID string
|
|
|
|
Conditional *conditionalArgs
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eTag = "ETag"
|
|
|
|
checksum = "Checksum"
|
|
|
|
objectParts = "ObjectParts"
|
|
|
|
storageClass = "StorageClass"
|
|
|
|
objectSize = "ObjectSize"
|
|
|
|
)
|
|
|
|
|
|
|
|
var validAttributes = map[string]struct{}{
|
|
|
|
eTag: {},
|
|
|
|
checksum: {},
|
|
|
|
objectParts: {},
|
|
|
|
storageClass: {},
|
|
|
|
objectSize: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetObjectAttributesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
info *data.ObjectInfo
|
|
|
|
|
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
|
|
|
)
|
|
|
|
|
|
|
|
params, err := parseGetObjectAttributeArgs(r)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid request", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: params.VersionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err = h.obj.GetObjectInfo(r.Context(), p); err != nil {
|
|
|
|
h.logAndSendError(w, "could not fetch object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:09:28 +00:00
|
|
|
if err = checkPreconditions(info, params.Conditional); err != nil {
|
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:15:08 +00:00
|
|
|
response, err := encodeToObjectAttributesResponse(info, params)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't encode object info to response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeAttributesHeaders(w.Header(), info, params)
|
|
|
|
if err = api.EncodeToResponse(w, response); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeAttributesHeaders(h http.Header, info *data.ObjectInfo, params *GetObjectAttributesArgs) {
|
|
|
|
h.Set(api.LastModified, info.Created.UTC().Format(http.TimeFormat))
|
|
|
|
if len(params.VersionID) != 0 {
|
|
|
|
h.Set(api.AmzVersionID, info.Version())
|
|
|
|
}
|
|
|
|
|
2022-05-31 09:10:59 +00:00
|
|
|
if info.IsDeleteMarker {
|
2022-05-12 21:15:08 +00:00
|
|
|
h.Set(api.AmzDeleteMarker, strconv.FormatBool(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// x-amz-request-charged
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, error) {
|
2022-06-01 09:54:27 +00:00
|
|
|
res := &GetObjectAttributesArgs{
|
|
|
|
VersionID: r.URL.Query().Get(api.QueryVersionID),
|
|
|
|
}
|
2022-05-12 21:15:08 +00:00
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
attributesVal := r.Header.Get(api.AmzObjectAttributes)
|
2022-05-12 21:15:08 +00:00
|
|
|
if attributesVal == "" {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := strings.Split(attributesVal, ",")
|
|
|
|
for _, a := range attributes {
|
|
|
|
if _, ok := validAttributes[a]; !ok {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
|
|
|
|
}
|
|
|
|
res.Attributes = append(res.Attributes, a)
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
return res, nil
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encodeToObjectAttributesResponse(info *data.ObjectInfo, p *GetObjectAttributesArgs) (*GetObjectAttributesResponse, error) {
|
|
|
|
resp := &GetObjectAttributesResponse{}
|
|
|
|
|
|
|
|
for _, attr := range p.Attributes {
|
|
|
|
switch attr {
|
|
|
|
case eTag:
|
|
|
|
resp.ETag = info.HashSum
|
|
|
|
case storageClass:
|
|
|
|
resp.StorageClass = "STANDARD"
|
|
|
|
case objectSize:
|
|
|
|
resp.ObjectSize = info.Size
|
|
|
|
case objectParts:
|
2022-06-01 09:54:27 +00:00
|
|
|
parts, err := formUploadAttributes(info)
|
2022-05-12 21:15:08 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("form upload attributes: %w", err)
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
if parts != nil {
|
|
|
|
resp.ObjectParts = parts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
func formUploadAttributes(info *data.ObjectInfo) (*ObjectParts, error) {
|
|
|
|
var err error
|
2022-05-12 21:15:08 +00:00
|
|
|
res := ObjectParts{}
|
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
partsCountStr, ok := info.Headers[layer.UploadCompletedPartsCount]
|
|
|
|
if !ok {
|
2022-05-12 21:15:08 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
res.PartsCount, err = strconv.Atoi(partsCountStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid parts count header '%s': %w", partsCountStr, err)
|
2022-05-12 21:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|