Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix header value #379

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions server/egin/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (c *Container) defaultServerInterceptor() gin.HandlerFunc {
var event = "normal"

// 必须在defer外层,因为要赋值,替换ctx
// 只有在环境变量里的自定义header,才会写入到context value里
for _, key := range loggerKeys {
// 赋值context
getHeaderValue(ctx, key, c.config.EnableTrustedCustomHeader)
Expand Down Expand Up @@ -416,13 +417,7 @@ func getHeaderValue(c *gin.Context, key string, enableTrustedCustomHeader bool)
value := c.GetHeader(key)
if value != "" {
// 如果信任该Header,将header数据赋值到context上
for _, customContextKey := range transport.CustomContextKeys() {
// 如果header key和自定义key是一样的,写入到contet value中
if customContextKey == key {
c.Request = c.Request.WithContext(transport.WithValue(c.Request.Context(), key, value))
}
}

c.Request = c.Request.WithContext(transport.WithValue(c.Request.Context(), key, value))
}
return value
}
Expand Down
10 changes: 2 additions & 8 deletions server/egrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,10 @@ func (c *Container) defaultUnaryServerInterceptor() grpc.UnaryServerInterceptor
var event = "normal"

// 必须在defer外层,因为要赋值,替换ctx
// 只有在环境变量里的自定义header,才会写入到context value里
for _, key := range loggerKeys {
if value := tools.GrpcHeaderValue(ctx, key); value != "" {
// 如果信任该Header,将header数据赋值到context上
for _, customContextKey := range transport.CustomContextKeys() {
// 如果header key和自定义key是一样的,写入到contet value中
if customContextKey == key {
ctx = transport.WithValue(ctx, key, value)
}
}

ctx = transport.WithValue(ctx, key, value)
}
}

Expand Down
Loading