2021-01-25 19:36:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
HeaderFilter interface {
|
|
|
|
Filter(header *fasthttp.RequestHeader) map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
headerFilter struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
mapping map[string]string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-01-26 09:40:01 +00:00
|
|
|
const userAttributeHeaderPrefix = "X-Attribute-"
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
func newHeaderFilter(l *zap.Logger, v *viper.Viper) HeaderFilter {
|
|
|
|
filter := &headerFilter{
|
|
|
|
logger: l,
|
|
|
|
mapping: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
index := strconv.Itoa(i)
|
|
|
|
key := strings.Join([]string{cfgUploaderHeader, index, cfgUploaderHeaderKey}, ".")
|
|
|
|
rep := strings.Join([]string{cfgUploaderHeader, index, cfgUploaderHeaderVal}, ".")
|
|
|
|
|
|
|
|
keyValue := v.GetString(key)
|
|
|
|
repValue := v.GetString(rep)
|
|
|
|
|
|
|
|
if keyValue == "" || repValue == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
filter.mapping[keyValue] = repValue
|
|
|
|
|
|
|
|
l.Debug("load upload header table value",
|
|
|
|
zap.String("key", keyValue),
|
|
|
|
zap.String("val", repValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default values
|
|
|
|
filter.mapping[object.AttributeFileName] = object.AttributeFileName
|
|
|
|
filter.mapping[object.AttributeTimestamp] = object.AttributeTimestamp
|
|
|
|
|
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *headerFilter) Filter(header *fasthttp.RequestHeader) map[string]string {
|
|
|
|
result := make(map[string]string)
|
2021-01-26 09:40:01 +00:00
|
|
|
prefix := []byte(userAttributeHeaderPrefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
header.VisitAll(func(key, val []byte) {
|
2021-01-26 15:36:53 +00:00
|
|
|
// checks that key and val not empty
|
2021-01-25 19:36:46 +00:00
|
|
|
if len(key) == 0 || len(val) == 0 {
|
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks that key has attribute prefix
|
|
|
|
if !bytes.HasPrefix(key, prefix) {
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks that after removing attribute prefix we had not empty key
|
|
|
|
if key = bytes.TrimPrefix(key, prefix); len(key) == 0 {
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks mapping table and if we found record store it
|
|
|
|
// at resulting hashmap
|
|
|
|
if name, ok := h.mapping[string(key)]; ok {
|
2021-01-25 19:36:46 +00:00
|
|
|
result[name] = string(val)
|
|
|
|
|
|
|
|
h.logger.Debug("add attribute to result object",
|
|
|
|
zap.String("key", name),
|
|
|
|
zap.String("val", string(val)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
// otherwise inform that attribute will be ignored
|
2021-01-25 19:36:46 +00:00
|
|
|
h.logger.Debug("ignore attribute",
|
|
|
|
zap.String("key", string(key)),
|
|
|
|
zap.String("val", string(val)))
|
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|