2020-09-29 15:05:22 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-01-12 14:55:02 +00:00
|
|
|
"strconv"
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2021-11-10 07:08:33 +00:00
|
|
|
sessionsdk "github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
2020-09-29 15:05:22 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 11:58:15 +00:00
|
|
|
// maxLocalTTL is maximum TTL for an operation to be considered local.
|
|
|
|
const maxLocalTTL = 1
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
type CommonPrm struct {
|
|
|
|
local bool
|
2020-09-29 15:08:16 +00:00
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
netmapEpoch, netmapLookupDepth uint64
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
token *sessionsdk.Token
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
bearer *token.BearerToken
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
ttl uint32
|
2020-09-29 15:05:22 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
xhdrs []*sessionsdk.XHeader
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
// TTL returns TTL for new requests.
|
|
|
|
func (p *CommonPrm) TTL() uint32 {
|
2020-09-29 15:05:22 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.ttl
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 18:01:29 +00:00
|
|
|
return 1
|
2020-09-29 15:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 18:01:29 +00:00
|
|
|
// XHeaders returns X-Headers for new requests.
|
2021-11-10 07:08:33 +00:00
|
|
|
func (p *CommonPrm) XHeaders() []*sessionsdk.XHeader {
|
2020-10-20 13:44:45 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.xhdrs
|
2020-10-20 13:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return nil
|
2020-10-20 13:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
p.local = v
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
func (p *CommonPrm) LocalOnly() bool {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.local
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return false
|
2021-03-13 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (p *CommonPrm) SessionToken() *sessionsdk.Token {
|
2020-09-29 15:08:16 +00:00
|
|
|
if p != nil {
|
|
|
|
return p.token
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
func (p *CommonPrm) BearerToken() *token.BearerToken {
|
|
|
|
if p != nil {
|
|
|
|
return p.bearer
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
func (p *CommonPrm) NetmapEpoch() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) NetmapLookupDepth() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapLookupDepth
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) SetNetmapLookupDepth(v uint64) {
|
|
|
|
if p != nil {
|
|
|
|
p.netmapLookupDepth = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
func CommonPrmFromV2(req interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
2021-01-12 14:55:02 +00:00
|
|
|
}) (*CommonPrm, error) {
|
2020-09-29 15:05:22 +00:00
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
xHdrs := meta.GetXHeaders()
|
2021-11-01 08:35:33 +00:00
|
|
|
ttl := meta.GetTTL()
|
2020-12-28 15:55:16 +00:00
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
prm := &CommonPrm{
|
2022-01-19 11:58:15 +00:00
|
|
|
local: ttl <= maxLocalTTL,
|
2021-11-10 07:08:33 +00:00
|
|
|
xhdrs: make([]*sessionsdk.XHeader, 0, len(xHdrs)),
|
2021-11-01 08:35:33 +00:00
|
|
|
ttl: ttl - 1, // decrease TTL for new requests
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tok := meta.GetSessionToken(); tok != nil {
|
2021-05-31 11:03:17 +00:00
|
|
|
prm.token = sessionsdk.NewTokenFromV2(tok)
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tok := meta.GetBearerToken(); tok != nil {
|
|
|
|
prm.bearer = token.NewBearerTokenFromV2(tok)
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
for i := range xHdrs {
|
2021-01-12 14:55:02 +00:00
|
|
|
switch xHdrs[i].GetKey() {
|
|
|
|
case session.XHeaderNetmapEpoch:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapEpoch, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case session.XHeaderNetmapLookupDepth:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapLookupDepth, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2022-03-15 11:16:46 +00:00
|
|
|
xhdr := sessionsdk.NewXHeaderFromV2(&xHdrs[i])
|
2021-11-01 08:35:33 +00:00
|
|
|
|
|
|
|
prm.xhdrs = append(prm.xhdrs, xhdr)
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
2020-12-28 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
return prm, nil
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|