2021-07-18 13:40:19 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-07-29 16:16:43 +00:00
|
|
|
"net/url"
|
2021-07-18 13:40:19 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-07-29 16:16:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-07-18 13:40:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-07-18 13:40:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListObjectsV1Handler handles objects listing requests for API version 1.
|
|
|
|
func (h *handler) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
params, err := parseListObjectsArgsV1(reqInfo)
|
2021-07-18 13:40:19 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "failed to parse arguments", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
if err = h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
list, err := h.obj.ListObjectsV1(r.Context(), params)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
if err = api.EncodeToResponse(w, encodeV1(params, list)); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeV1(p *layer.ListObjectsParamsV1, list *layer.ListObjectsInfoV1) *ListObjectsV1Response {
|
|
|
|
res := &ListObjectsV1Response{
|
|
|
|
Name: p.Bucket,
|
|
|
|
EncodingType: p.Encode,
|
|
|
|
Marker: p.Marker,
|
|
|
|
Prefix: p.Prefix,
|
|
|
|
MaxKeys: p.MaxKeys,
|
|
|
|
Delimiter: p.Delimiter,
|
2021-07-29 14:23:36 +00:00
|
|
|
IsTruncated: list.IsTruncated,
|
|
|
|
NextMarker: list.NextMarker,
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:10:28 +00:00
|
|
|
res.CommonPrefixes = fillPrefixes(list.Prefixes, p.Encode)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2021-07-20 12:40:38 +00:00
|
|
|
res.Contents = fillContentsWithOwner(list.Objects, p.Encode)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsV2Handler handles objects listing requests for API version 2.
|
|
|
|
func (h *handler) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
params, err := parseListObjectsArgsV2(reqInfo)
|
2021-07-18 13:40:19 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "failed to parse arguments", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
if err = h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
list, err := h.obj.ListObjectsV2(r.Context(), params)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
if err = api.EncodeToResponse(w, encodeV2(params, list)); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeV2(p *layer.ListObjectsParamsV2, list *layer.ListObjectsInfoV2) *ListObjectsV2Response {
|
|
|
|
res := &ListObjectsV2Response{
|
2021-07-29 14:23:36 +00:00
|
|
|
Name: p.Bucket,
|
|
|
|
EncodingType: p.Encode,
|
|
|
|
Prefix: s3PathEncode(p.Prefix, p.Encode),
|
|
|
|
KeyCount: len(list.Objects) + len(list.Prefixes),
|
|
|
|
MaxKeys: p.MaxKeys,
|
|
|
|
Delimiter: s3PathEncode(p.Delimiter, p.Encode),
|
|
|
|
StartAfter: s3PathEncode(p.StartAfter, p.Encode),
|
|
|
|
IsTruncated: list.IsTruncated,
|
2021-07-18 13:40:19 +00:00
|
|
|
ContinuationToken: p.ContinuationToken,
|
|
|
|
NextContinuationToken: list.NextContinuationToken,
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:10:28 +00:00
|
|
|
res.CommonPrefixes = fillPrefixes(list.Prefixes, p.Encode)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2021-07-20 12:40:38 +00:00
|
|
|
res.Contents = fillContents(list.Objects, p.Encode, p.FetchOwner)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
func parseListObjectsArgsV1(reqInfo *api.ReqInfo) (*layer.ListObjectsParamsV1, error) {
|
2021-07-18 13:40:19 +00:00
|
|
|
var (
|
2021-07-29 14:18:51 +00:00
|
|
|
res layer.ListObjectsParamsV1
|
2021-08-05 09:18:52 +00:00
|
|
|
queryValues = reqInfo.URL.Query()
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
common, err := parseListObjectArgs(reqInfo)
|
2021-07-18 13:40:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.ListObjectsParamsCommon = *common
|
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
res.Marker = queryValues.Get("marker")
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
func parseListObjectsArgsV2(reqInfo *api.ReqInfo) (*layer.ListObjectsParamsV2, error) {
|
2021-07-18 13:40:19 +00:00
|
|
|
var (
|
2021-07-29 14:18:51 +00:00
|
|
|
res layer.ListObjectsParamsV2
|
2021-08-05 09:18:52 +00:00
|
|
|
queryValues = reqInfo.URL.Query()
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
common, err := parseListObjectArgs(reqInfo)
|
2021-07-18 13:40:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.ListObjectsParamsCommon = *common
|
|
|
|
|
2021-07-29 16:16:43 +00:00
|
|
|
res.ContinuationToken, err = parseContinuationToken(queryValues)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
res.StartAfter = queryValues.Get("start-after")
|
|
|
|
res.FetchOwner, _ = strconv.ParseBool(queryValues.Get("fetch-owner"))
|
2021-07-18 13:40:19 +00:00
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
func parseListObjectArgs(reqInfo *api.ReqInfo) (*layer.ListObjectsParamsCommon, error) {
|
2021-07-18 13:40:19 +00:00
|
|
|
var (
|
2021-07-29 14:18:51 +00:00
|
|
|
err error
|
|
|
|
res layer.ListObjectsParamsCommon
|
2021-08-05 09:18:52 +00:00
|
|
|
queryValues = reqInfo.URL.Query()
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
res.Bucket = reqInfo.BucketName
|
2021-07-29 14:18:51 +00:00
|
|
|
res.Delimiter = queryValues.Get("delimiter")
|
|
|
|
res.Encode = queryValues.Get("encoding-type")
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
if queryValues.Get("max-keys") == "" {
|
2021-07-18 13:40:19 +00:00
|
|
|
res.MaxKeys = maxObjectList
|
2021-07-29 14:18:51 +00:00
|
|
|
} else if res.MaxKeys, err = strconv.Atoi(queryValues.Get("max-keys")); err != nil || res.MaxKeys < 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidMaxKeys)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
res.Prefix = queryValues.Get("prefix")
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:16:43 +00:00
|
|
|
func parseContinuationToken(queryValues url.Values) (string, error) {
|
|
|
|
if val, ok := queryValues["continuation-token"]; ok {
|
|
|
|
if err := object.NewID().Parse(val[0]); err != nil {
|
2021-08-09 08:53:58 +00:00
|
|
|
return "", errors.GetAPIError(errors.ErrIncorrectContinuationToken)
|
2021-07-29 16:16:43 +00:00
|
|
|
}
|
|
|
|
return val[0], nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:10:28 +00:00
|
|
|
func fillPrefixes(src []string, encode string) []CommonPrefix {
|
|
|
|
var dst []CommonPrefix
|
|
|
|
for _, obj := range src {
|
|
|
|
dst = append(dst, CommonPrefix{
|
|
|
|
Prefix: s3PathEncode(obj, encode),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:40:38 +00:00
|
|
|
func fillContentsWithOwner(src []*layer.ObjectInfo, encode string) []Object {
|
|
|
|
return fillContents(src, encode, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillContents(src []*layer.ObjectInfo, encode string, fetchOwner bool) []Object {
|
2021-07-19 09:10:28 +00:00
|
|
|
var dst []Object
|
|
|
|
for _, obj := range src {
|
2021-07-20 12:40:38 +00:00
|
|
|
res := Object{
|
2021-07-19 09:10:28 +00:00
|
|
|
Key: s3PathEncode(obj.Name, encode),
|
|
|
|
Size: obj.Size,
|
|
|
|
LastModified: obj.Created.Format(time.RFC3339),
|
2021-07-20 12:40:38 +00:00
|
|
|
ETag: obj.HashSum,
|
|
|
|
}
|
2021-07-19 09:10:28 +00:00
|
|
|
|
2021-07-20 12:40:38 +00:00
|
|
|
if fetchOwner {
|
|
|
|
res.Owner = &Owner{
|
2021-07-19 09:10:28 +00:00
|
|
|
ID: obj.Owner.String(),
|
|
|
|
DisplayName: obj.Owner.String(),
|
2021-07-20 12:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 09:10:28 +00:00
|
|
|
|
2021-07-20 12:40:38 +00:00
|
|
|
dst = append(dst, res)
|
2021-07-19 09:10:28 +00:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
func (h *handler) ListBucketObjectVersionsHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
|
|
|
p, err := parseListObjectVersionsRequest(reqInfo)
|
2021-07-18 13:40:19 +00:00
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "failed to parse request", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := h.obj.ListObjectVersions(r.Context(), p)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := encodeListObjectVersionsToResponse(info, p.Bucket)
|
2021-08-05 09:18:52 +00:00
|
|
|
if err = api.EncodeToResponse(w, response); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
func parseListObjectVersionsRequest(reqInfo *api.ReqInfo) (*layer.ListObjectVersionsParams, error) {
|
2021-07-18 13:40:19 +00:00
|
|
|
var (
|
2021-07-29 14:18:51 +00:00
|
|
|
err error
|
|
|
|
res layer.ListObjectVersionsParams
|
2021-08-05 09:18:52 +00:00
|
|
|
queryValues = reqInfo.URL.Query()
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
if queryValues.Get("max-keys") == "" {
|
2021-07-18 13:40:19 +00:00
|
|
|
res.MaxKeys = maxObjectList
|
2021-07-29 14:18:51 +00:00
|
|
|
} else if res.MaxKeys, err = strconv.Atoi(queryValues.Get("max-keys")); err != nil || res.MaxKeys <= 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidMaxKeys)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 14:18:51 +00:00
|
|
|
res.Prefix = queryValues.Get("prefix")
|
|
|
|
res.KeyMarker = queryValues.Get("marker")
|
|
|
|
res.Delimiter = queryValues.Get("delimiter")
|
|
|
|
res.Encode = queryValues.Get("encoding-type")
|
|
|
|
res.VersionIDMarker = queryValues.Get("version-id-marker")
|
2021-08-05 09:18:52 +00:00
|
|
|
res.Bucket = reqInfo.BucketName
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeListObjectVersionsToResponse(info *layer.ListObjectVersionsInfo, bucketName string) *ListObjectsVersionsResponse {
|
|
|
|
res := ListObjectsVersionsResponse{
|
|
|
|
Name: bucketName,
|
|
|
|
IsTruncated: info.IsTruncated,
|
|
|
|
KeyMarker: info.KeyMarker,
|
|
|
|
NextKeyMarker: info.NextKeyMarker,
|
|
|
|
NextVersionIDMarker: info.NextVersionIDMarker,
|
|
|
|
VersionIDMarker: info.VersionIDMarker,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range info.CommonPrefixes {
|
|
|
|
res.CommonPrefixes = append(res.CommonPrefixes, CommonPrefix{Prefix: *prefix})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ver := range info.Version {
|
|
|
|
res.Version = append(res.Version, ObjectVersionResponse{
|
|
|
|
IsLatest: ver.IsLatest,
|
|
|
|
Key: ver.Object.Name,
|
|
|
|
LastModified: ver.Object.Created.Format(time.RFC3339),
|
|
|
|
Owner: Owner{
|
|
|
|
ID: ver.Object.Owner.String(),
|
|
|
|
DisplayName: ver.Object.Owner.String(),
|
|
|
|
},
|
|
|
|
Size: ver.Object.Size,
|
2021-08-13 14:13:14 +00:00
|
|
|
VersionID: ver.Object.Version(),
|
2021-07-18 13:40:19 +00:00
|
|
|
ETag: ver.Object.HashSum,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// this loop is not starting till versioning is not implemented
|
|
|
|
for _, del := range info.DeleteMarker {
|
|
|
|
res.DeleteMarker = append(res.DeleteMarker, DeleteMarkerEntry{
|
|
|
|
IsLatest: del.IsLatest,
|
2021-08-13 14:13:14 +00:00
|
|
|
Key: del.Object.Name,
|
|
|
|
LastModified: del.Object.Created.Format(time.RFC3339),
|
2021-07-18 13:40:19 +00:00
|
|
|
Owner: Owner{
|
2021-08-13 14:13:14 +00:00
|
|
|
ID: del.Object.Owner.String(),
|
|
|
|
DisplayName: del.Object.Owner.String(),
|
2021-07-18 13:40:19 +00:00
|
|
|
},
|
2021-08-13 14:13:14 +00:00
|
|
|
VersionID: del.Object.Version(),
|
2021-07-18 13:40:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res
|
|
|
|
}
|