Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/origin/middleware.go
2500 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package origin
6
7
import (
8
"context"
9
10
"github.com/bufbuild/connect-go"
11
)
12
13
func NewInterceptor() *Interceptor {
14
return &Interceptor{}
15
}
16
17
type Interceptor struct{}
18
19
func (i *Interceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
20
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
21
if req.Spec().IsClient {
22
req.Header().Add("Origin", FromContext(ctx))
23
} else {
24
origin := req.Header().Get("Origin")
25
ctx = ToContext(ctx, origin)
26
}
27
28
return next(ctx, req)
29
})
30
}
31
32
func (a *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
33
return func(ctx context.Context, s connect.Spec) connect.StreamingClientConn {
34
conn := next(ctx, s)
35
conn.RequestHeader().Add("Origin", FromContext(ctx))
36
37
return conn
38
}
39
}
40
41
func (a *Interceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
42
return func(ctx context.Context, conn connect.StreamingHandlerConn) error {
43
origin := conn.RequestHeader().Get("Origin")
44
ctx = ToContext(ctx, origin)
45
46
return next(ctx, conn)
47
}
48
}
49
50