[#111] Fix expiration epoch headers

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-11-30 12:25:50 +03:00 committed by Alex Vanin
parent dbbc9e05cf
commit d6dd244756
2 changed files with 46 additions and 0 deletions

View file

@ -153,6 +153,9 @@ func (r request) receiveFile(clnt pool.Object, objectAddress *object.Address) {
if !isValidToken(key) || !isValidValue(val) { if !isValidToken(key) || !isValidValue(val) {
continue continue
} }
if strings.HasPrefix(key, utils.SystemAttributePrefix) {
key = systemBackwardTranslator(key)
}
r.Response.Header.Set(utils.UserAttributeHeaderPrefix+key, val) r.Response.Header.Set(utils.UserAttributeHeaderPrefix+key, val)
switch key { switch key {
case object.AttributeFileName: case object.AttributeFileName:
@ -190,6 +193,26 @@ func (r request) receiveFile(clnt pool.Object, objectAddress *object.Address) {
r.Response.Header.Set("Content-Disposition", dis+"; filename="+path.Base(filename)) r.Response.Header.Set("Content-Disposition", dis+"; filename="+path.Base(filename))
} }
// systemBackwardTranslator is used to convert headers looking like '__NEOFS__ATTR_NAME' to 'Neofs-Attr-Name'.
func systemBackwardTranslator(key string) string {
// trim specified prefix '__NEOFS__'
key = strings.TrimPrefix(key, utils.SystemAttributePrefix)
var res strings.Builder
res.WriteString("Neofs-")
strs := strings.Split(key, "_")
for i, s := range strs {
s = strings.Title(strings.ToLower(s))
res.WriteString(s)
if i != len(strs)-1 {
res.WriteString("-")
}
}
return res.String()
}
func bearerOpts(ctx context.Context) pool.CallOption { func bearerOpts(ctx context.Context) pool.CallOption {
if tkn, err := tokens.LoadBearerToken(ctx); err == nil { if tkn, err := tokens.LoadBearerToken(ctx); err == nil {
return pool.WithBearer(tkn) return pool.WithBearer(tkn)

View file

@ -0,0 +1,23 @@
package downloader
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSystemBackwardTranslator(t *testing.T) {
input := []string{
"__NEOFS__EXPIRATION_EPOCH",
"__NEOFS__RANDOM_ATTR",
}
expected := []string{
"Neofs-Expiration-Epoch",
"Neofs-Random-Attr",
}
for i, str := range input {
res := systemBackwardTranslator(str)
require.Equal(t, expected[i], res)
}
}