Path: blob/main/components/public-api-server/pkg/origin/middleware.go
2500 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package origin56import (7"context"89"github.com/bufbuild/connect-go"10)1112func NewInterceptor() *Interceptor {13return &Interceptor{}14}1516type Interceptor struct{}1718func (i *Interceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {19return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {20if req.Spec().IsClient {21req.Header().Add("Origin", FromContext(ctx))22} else {23origin := req.Header().Get("Origin")24ctx = ToContext(ctx, origin)25}2627return next(ctx, req)28})29}3031func (a *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {32return func(ctx context.Context, s connect.Spec) connect.StreamingClientConn {33conn := next(ctx, s)34conn.RequestHeader().Add("Origin", FromContext(ctx))3536return conn37}38}3940func (a *Interceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {41return func(ctx context.Context, conn connect.StreamingHandlerConn) error {42origin := conn.RequestHeader().Get("Origin")43ctx = ToContext(ctx, origin)4445return next(ctx, conn)46}47}484950