Fixes after review

After discussion, we decided to simplify attribute translation for now. Full translation requires support for UTF-8 values encoded according to RFC 8187/7230, this can be clarified and implemented in further PRs.

1. Remove translation tables
2. Use simple translation rules only, for now ignoring UTF-8 keys and values

- `X-Attribute-NEOFS-` prefixed headers considered well-known system attributes
- System Attribute key is uppercased
- System Attribute key's `-` translates to `_`
- `X-Attribute-` prefixed headers considered regular object attributes
- Normal attribute key is a result of http header prefix trimming
- Value string should be left as is, for now

```
HTTP:
X-Attribute-NEOFS-Expiration-Epoch: 123
X-Attribute-FileName: cat.jpg

NeoFS:
__NEOFS__EXPIRATION_EPOCH: "123"
FileName: "cat.jpg"
```

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2021-02-03 16:01:30 +03:00
parent 71999a796d
commit cbaf9e6142
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
5 changed files with 30 additions and 83 deletions

View file

@ -2,63 +2,33 @@ 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
}
const (
userAttributeHeaderPrefix = "X-Attribute-"
neofsAttributeHeaderPrefix = "NEOFS-"
headerFilter struct {
logger *zap.Logger
mapping map[string]string
}
systemAttributePrefix = "__NEOFS__"
)
const userAttributeHeaderPrefix = "X-Attribute-"
func systemTranslator(key []byte) []byte {
// replace `NEOFS-` with `__NEOFS__`
key = bytes.Replace(key, []byte(neofsAttributeHeaderPrefix), []byte(systemAttributePrefix), 1)
func newHeaderFilter(l *zap.Logger, v *viper.Viper) HeaderFilter {
filter := &headerFilter{
logger: l,
mapping: make(map[string]string),
}
// replace `-` with `_`
key = bytes.ReplaceAll(key, []byte("-"), []byte("_"))
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
// replace with uppercase
return bytes.ToUpper(key)
}
func (h *headerFilter) Filter(header *fasthttp.RequestHeader) map[string]string {
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
@ -71,27 +41,27 @@ func (h *headerFilter) Filter(header *fasthttp.RequestHeader) map[string]string
return
}
// checks that after removing attribute prefix we had not empty key
if key = bytes.TrimPrefix(key, prefix); len(key) == 0 {
// removing attribute prefix
key = bytes.TrimPrefix(key, prefix)
// checks that it's a system NeoFS header
if bytes.HasPrefix(key, system) {
key = systemTranslator(key)
}
// checks that attribute key not empty
if len(key) == 0 {
return
}
// checks mapping table and if we found record store it
// at resulting hashmap
if name, ok := h.mapping[string(key)]; ok {
result[name] = string(val)
// make string representation of key / val
k, v := string(key), string(val)
h.logger.Debug("add attribute to result object",
zap.String("key", name),
zap.String("val", string(val)))
result[k] = v
return
}
// otherwise inform that attribute will be ignored
h.logger.Debug("ignore attribute",
zap.String("key", string(key)),
zap.String("val", string(val)))
l.Debug("add attribute to result object",
zap.String("key", k),
zap.String("val", v))
})
return result