2021-03-31 16:58:42 +00:00
|
|
|
package uploader
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
const (
|
2021-09-08 06:15:42 +00:00
|
|
|
userAttributeHeaderPrefix = "X-Attribute-"
|
|
|
|
systemAttributePrefix = "__NEOFS__"
|
2021-01-25 19:36:46 +00:00
|
|
|
)
|
|
|
|
|
2021-09-08 06:15:42 +00:00
|
|
|
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)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// replace `-` with `_`
|
|
|
|
key = bytes.ReplaceAll(key, []byte("-"), []byte("_"))
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// replace with uppercase
|
|
|
|
return bytes.ToUpper(key)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]string {
|
2021-01-25 19:36:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// removing attribute prefix
|
|
|
|
key = bytes.TrimPrefix(key, prefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// checks that it's a system NeoFS header
|
2021-09-08 06:15:42 +00:00
|
|
|
for _, system := range neofsAttributeHeaderPrefixes {
|
|
|
|
if bytes.HasPrefix(key, system) {
|
|
|
|
key = systemTranslator(key, system)
|
|
|
|
break
|
|
|
|
}
|
2021-02-03 13:01:30 +00:00
|
|
|
}
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// checks that attribute key not empty
|
|
|
|
if len(key) == 0 {
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// make string representation of key / val
|
|
|
|
k, v := string(key), string(val)
|
|
|
|
|
|
|
|
result[k] = v
|
|
|
|
|
|
|
|
l.Debug("add attribute to result object",
|
|
|
|
zap.String("key", k),
|
|
|
|
zap.String("val", v))
|
2021-01-25 19:36:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|