-
Notifications
You must be signed in to change notification settings - Fork 1
/
mw-headers.go
58 lines (49 loc) · 1.35 KB
/
mw-headers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gateway
import (
"context"
"encoding/json"
"net/http"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/sirjager/trueauth/config"
)
func customHeaderMatcher(h []string) func(s string) (string, bool) {
return func(key string) (string, bool) {
header := strings.ToLower(key)
for _, v := range h {
myheader := strings.ToLower(v[1:])
if myheader == header {
if strings.HasPrefix(v, "+") {
return myheader, true
}
if strings.HasPrefix(v, "-") {
return "", false
}
}
}
return runtime.DefaultHeaderMatcher(key)
}
}
func addCustomHeaders(
config config.Config,
) func(context.Context, http.ResponseWriter, protoreflect.ProtoMessage) error {
return func(ctx context.Context, w http.ResponseWriter, m protoreflect.ProtoMessage) error {
if md, ok := runtime.ServerMetadataFromContext(ctx); ok {
for k, v := range md.HeaderMD {
if strings.HasPrefix(k, "set-cookie:") {
cookie := http.Cookie{}
if err := json.Unmarshal([]byte(v[0]), &cookie); err != nil {
return err
}
http.SetCookie(w, &cookie)
} else {
w.Header().Set(k, strings.Join(v, ","))
}
}
}
// adding server name in header to determine which server sent it
w.Header().Set("x-server-name", config.Server.ServerName)
return nil
}
}