mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
39 lines
812 B
Go
Executable File
39 lines
812 B
Go
Executable File
package protocol
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type key int
|
|
|
|
const (
|
|
userKey key = iota
|
|
requestKey
|
|
)
|
|
|
|
// ContextWithUser returns a context combined with a User.
|
|
func ContextWithUser(ctx context.Context, user *MemoryUser) context.Context {
|
|
return context.WithValue(ctx, userKey, user)
|
|
}
|
|
|
|
// UserFromContext extracts a User from the given context, if any.
|
|
func UserFromContext(ctx context.Context) *MemoryUser {
|
|
v := ctx.Value(userKey)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.(*MemoryUser)
|
|
}
|
|
|
|
func ContextWithRequestHeader(ctx context.Context, request *RequestHeader) context.Context {
|
|
return context.WithValue(ctx, requestKey, request)
|
|
}
|
|
|
|
func RequestHeaderFromContext(ctx context.Context) *RequestHeader {
|
|
request := ctx.Value(requestKey)
|
|
if request == nil {
|
|
return nil
|
|
}
|
|
return request.(*RequestHeader)
|
|
}
|