[#87] Allow canonical X-Attribute-Neofs-* headers

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-09-08 09:15:42 +03:00 committed by Alex Vanin
parent d2b0532929
commit 0597c0c143
3 changed files with 46 additions and 11 deletions

View file

@ -8,15 +8,15 @@ import (
)
const (
userAttributeHeaderPrefix = "X-Attribute-"
neofsAttributeHeaderPrefix = "NEOFS-"
systemAttributePrefix = "__NEOFS__"
userAttributeHeaderPrefix = "X-Attribute-"
systemAttributePrefix = "__NEOFS__"
)
func systemTranslator(key []byte) []byte {
// replace `NEOFS-` with `__NEOFS__`
key = bytes.Replace(key, []byte(neofsAttributeHeaderPrefix), []byte(systemAttributePrefix), 1)
var neofsAttributeHeaderPrefixes = [...][]byte{[]byte("Neofs-"), []byte("NEOFS-"), []byte("neofs-")}
func systemTranslator(key, prefix []byte) []byte {
// replace specified prefix with `__NEOFS__`
key = bytes.Replace(key, prefix, []byte(systemAttributePrefix), 1)
// replace `-` with `_`
key = bytes.ReplaceAll(key, []byte("-"), []byte("_"))
@ -28,7 +28,6 @@ func systemTranslator(key []byte) []byte {
func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]string {
result := make(map[string]string)
prefix := []byte(userAttributeHeaderPrefix)
system := []byte(neofsAttributeHeaderPrefix)
header.VisitAll(func(key, val []byte) {
// checks that key and val not empty
@ -45,8 +44,11 @@ func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]str
key = bytes.TrimPrefix(key, prefix)
// checks that it's a system NeoFS header
if bytes.HasPrefix(key, system) {
key = systemTranslator(key)
for _, system := range neofsAttributeHeaderPrefixes {
if bytes.HasPrefix(key, system) {
key = systemTranslator(key, system)
break
}
}
// checks that attribute key not empty