2020-10-02 12:23:52 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2022-05-17 13:32:03 +00:00
|
|
|
"errors"
|
2020-10-02 12:23:52 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-12-14 12:07:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/acl"
|
2020-10-02 12:23:52 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-05-12 16:37:46 +00:00
|
|
|
refsV2 "github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-10-02 12:23:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
objectSDKAddress "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
objectSDKID "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-05-17 13:32:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-10-02 12:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
storage ObjectStorage
|
|
|
|
|
|
|
|
msg xHeaderSource
|
2020-12-15 09:06:04 +00:00
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
addr *objectSDKAddress.Address
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectStorage interface {
|
2022-01-26 12:11:13 +00:00
|
|
|
Head(*objectSDKAddress.Address) (*object.Object, error)
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response interface {
|
|
|
|
GetMetaHeader() *session.ResponseMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerSource struct {
|
2022-05-17 12:46:25 +00:00
|
|
|
requestHeaders []eaclSDK.Header
|
|
|
|
objectHeaders []eaclSDK.Header
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
|
|
|
storage: new(localStorage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) {
|
2020-10-02 12:23:52 +00:00
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
if cfg.msg == nil {
|
|
|
|
return nil, errors.New("message is not provided")
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
objHdrs, err := cfg.objectHeaders()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2022-05-17 12:46:25 +00:00
|
|
|
|
|
|
|
return headerSource{
|
|
|
|
objectHeaders: objHdrs,
|
|
|
|
requestHeaders: requestHeaders(cfg.msg),
|
|
|
|
}, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func (h headerSource) HeadersOfType(typ eaclSDK.FilterHeaderType) ([]eaclSDK.Header, bool) {
|
2020-10-02 12:23:52 +00:00
|
|
|
switch typ {
|
|
|
|
default:
|
|
|
|
return nil, true
|
|
|
|
case eaclSDK.HeaderFromRequest:
|
2022-05-17 12:46:25 +00:00
|
|
|
return h.requestHeaders, true
|
2020-10-02 12:23:52 +00:00
|
|
|
case eaclSDK.HeaderFromObject:
|
2022-05-17 12:46:25 +00:00
|
|
|
return h.objectHeaders, true
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 09:45:58 +00:00
|
|
|
func requestHeaders(msg xHeaderSource) []eaclSDK.Header {
|
2022-05-17 13:40:25 +00:00
|
|
|
return msg.GetXHeaders()
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func (h *cfg) objectHeaders() ([]eaclSDK.Header, error) {
|
2020-10-02 12:23:52 +00:00
|
|
|
switch m := h.msg.(type) {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected message type %T", h.msg))
|
2022-05-17 13:32:03 +00:00
|
|
|
case requestXHeaderSource:
|
2020-10-02 12:23:52 +00:00
|
|
|
switch req := m.req.(type) {
|
|
|
|
case *objectV2.GetRequest:
|
2020-12-15 09:06:04 +00:00
|
|
|
return h.localObjectHeaders(h.addr)
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.HeadRequest:
|
2020-12-15 09:06:04 +00:00
|
|
|
return h.localObjectHeaders(h.addr)
|
2020-12-18 12:51:29 +00:00
|
|
|
case
|
|
|
|
*objectV2.GetRangeRequest,
|
|
|
|
*objectV2.GetRangeHashRequest,
|
|
|
|
*objectV2.DeleteRequest:
|
2022-05-17 12:46:25 +00:00
|
|
|
return addressHeaders(h.addr), nil
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.PutRequest:
|
|
|
|
if v, ok := req.GetBody().GetObjectPart().(*objectV2.PutObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
if h.addr == nil {
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 := v.GetObjectID()
|
|
|
|
var id objectSDKID.ID
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
if idV2 != nil {
|
|
|
|
if err := id.ReadFromV2(*idV2); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse object ID: %w", err)
|
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnrV2 := v.GetHeader().GetContainerID()
|
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
if cnrV2 != nil {
|
2022-05-17 12:46:25 +00:00
|
|
|
if err := cnr.ReadFromV2(*cnrV2); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse container ID: %w", err)
|
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.addr = new(objectSDKAddress.Address)
|
|
|
|
h.addr.SetContainerID(cnr)
|
|
|
|
h.addr.SetObjectID(id)
|
2020-12-14 13:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
hs := headersFromObject(object.NewFromV2(oV2), h.addr)
|
2020-12-15 09:06:04 +00:00
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
return hs, nil
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
case *objectV2.SearchRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
cnrV2 := req.GetBody().GetContainerID()
|
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
if cnrV2 != nil {
|
2022-05-17 12:46:25 +00:00
|
|
|
if err := cnr.ReadFromV2(*cnrV2); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse container ID: %w", err)
|
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
return []eaclSDK.Header{cidHeader(cnr)}, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2022-05-17 13:32:03 +00:00
|
|
|
case responseXHeaderSource:
|
2020-10-02 12:23:52 +00:00
|
|
|
switch resp := m.resp.(type) {
|
2020-12-14 12:07:20 +00:00
|
|
|
default:
|
2020-12-15 09:06:04 +00:00
|
|
|
hs, _ := h.localObjectHeaders(h.addr)
|
2022-05-17 12:46:25 +00:00
|
|
|
return hs, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
case *objectV2.GetResponse:
|
|
|
|
if v, ok := resp.GetBody().GetObjectPart().(*objectV2.GetObjectPartInit); ok {
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
oV2.SetObjectID(v.GetObjectID())
|
|
|
|
oV2.SetHeader(v.GetHeader())
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), h.addr), nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
case *objectV2.HeadResponse:
|
|
|
|
oV2 := new(objectV2.Object)
|
|
|
|
|
|
|
|
var hdr *objectV2.Header
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
2020-12-07 17:49:47 +00:00
|
|
|
case *objectV2.ShortHeader:
|
2020-10-02 12:23:52 +00:00
|
|
|
hdr = new(objectV2.Header)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := h.addr.ContainerID()
|
|
|
|
|
|
|
|
var idV2 refsV2.ContainerID
|
|
|
|
id.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
hdr.SetContainerID(&idV2)
|
2020-12-07 17:49:47 +00:00
|
|
|
hdr.SetVersion(v.GetVersion())
|
|
|
|
hdr.SetCreationEpoch(v.GetCreationEpoch())
|
|
|
|
hdr.SetOwnerID(v.GetOwnerID())
|
|
|
|
hdr.SetObjectType(v.GetObjectType())
|
|
|
|
hdr.SetPayloadLength(v.GetPayloadLength())
|
|
|
|
case *objectV2.HeaderWithSignature:
|
|
|
|
hdr = v.GetHeader()
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oV2.SetHeader(hdr)
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
return headersFromObject(object.NewFromV2(oV2), h.addr), nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
return nil, nil
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 12:46:25 +00:00
|
|
|
func (h *cfg) localObjectHeaders(addr *objectSDKAddress.Address) ([]eaclSDK.Header, error) {
|
2020-12-14 12:07:20 +00:00
|
|
|
obj, err := h.storage.Head(addr)
|
2022-05-17 12:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
// Still parse addressHeaders, because the errors is ignored in some places.
|
|
|
|
return addressHeaders(addr), err
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
2022-05-17 12:46:25 +00:00
|
|
|
return headersFromObject(obj, addr), nil
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
func cidHeader(idCnr cid.ID) sysObjHdr {
|
|
|
|
return sysObjHdr{
|
2020-12-14 12:07:20 +00:00
|
|
|
k: acl.FilterObjectContainerID,
|
2022-05-17 13:32:03 +00:00
|
|
|
v: idCnr.String(),
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:32:03 +00:00
|
|
|
func oidHeader(oid objectSDKID.ID) sysObjHdr {
|
|
|
|
return sysObjHdr{
|
2020-12-14 12:07:20 +00:00
|
|
|
k: acl.FilterObjectID,
|
2022-05-17 13:32:03 +00:00
|
|
|
v: oid.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ownerIDHeader(ownerID *owner.ID) sysObjHdr {
|
|
|
|
return sysObjHdr{
|
|
|
|
k: acl.FilterObjectOwnerID,
|
|
|
|
v: ownerID.String(),
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func addressHeaders(addr *objectSDKAddress.Address) []eaclSDK.Header {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := addr.ContainerID()
|
|
|
|
|
2022-01-20 09:45:58 +00:00
|
|
|
res := make([]eaclSDK.Header, 1, 2)
|
2022-05-17 13:32:03 +00:00
|
|
|
res[0] = cidHeader(cnr)
|
2020-10-02 12:23:52 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if oid, ok := addr.ObjectID(); ok {
|
2022-05-17 13:32:03 +00:00
|
|
|
res = append(res, oidHeader(oid))
|
2020-12-14 12:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2020-10-02 12:23:52 +00:00
|
|
|
}
|