2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-26 12:44:07 +00:00
|
|
|
"fmt"
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
2021-01-25 19:36:46 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-11-17 15:01:38 +00:00
|
|
|
func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) (map[string]string, error) {
|
|
|
|
var err error
|
2021-01-25 19:36:46 +00:00
|
|
|
result := make(map[string]string)
|
2021-11-30 07:22:05 +00:00
|
|
|
prefix := []byte(utils.UserAttributeHeaderPrefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
header.VisitAll(func(key, val []byte) {
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the key and the 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
|
|
|
}
|
|
|
|
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the key has attribute prefix
|
2021-01-26 15:36:53 +00:00
|
|
|
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
|
2022-11-17 15:01:38 +00:00
|
|
|
clearKey := bytes.TrimPrefix(key, prefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2023-03-16 08:40:08 +00:00
|
|
|
clearKey = utils.TransformIfSystem(clearKey)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the attribute key is not empty
|
2022-11-17 15:01:38 +00:00
|
|
|
if len(clearKey) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if key gets duplicated
|
|
|
|
// return error containing full key name (with prefix)
|
|
|
|
if _, ok := result[string(clearKey)]; ok {
|
|
|
|
err = fmt.Errorf("key duplication error: %s", string(key))
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// make string representation of key / val
|
2022-11-17 15:01:38 +00:00
|
|
|
k, v := string(clearKey), string(val)
|
2021-02-03 13:01:30 +00:00
|
|
|
|
|
|
|
result[k] = v
|
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
l.Debug(logs.AddAttributeToResultObject,
|
2021-02-03 13:01:30 +00:00
|
|
|
zap.String("key", k),
|
|
|
|
zap.String("val", v))
|
2021-01-25 19:36:46 +00:00
|
|
|
})
|
|
|
|
|
2022-11-17 15:01:38 +00:00
|
|
|
return result, err
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|