2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-20 23:22:32 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
2021-10-28 23:37:51 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2020-03-20 23:22:32 +08:00
|
|
|
)
|
|
|
|
|
2021-08-01 09:17:37 +08:00
|
|
|
// RequestFromCtx retrieves and returns the Request object from context.
|
|
|
|
func RequestFromCtx(ctx context.Context) *Request {
|
|
|
|
if v := ctx.Value(ctxKeyForRequest); v != nil {
|
|
|
|
return v.(*Request)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:31:45 +08:00
|
|
|
// Context is alias for function GetCtx.
|
2020-03-21 13:32:43 +08:00
|
|
|
// This function overwrites the http.Request.Context function.
|
2020-04-06 22:31:45 +08:00
|
|
|
// See GetCtx.
|
2020-03-20 23:22:32 +08:00
|
|
|
func (r *Request) Context() context.Context {
|
|
|
|
if r.context == nil {
|
2021-10-28 23:37:51 +08:00
|
|
|
// DO NOT use the http context as it will be canceled after request done,
|
|
|
|
// which makes the asynchronous goroutine encounter "context canceled" error.
|
|
|
|
// r.context = r.Request.Context()
|
|
|
|
r.context = gctx.New()
|
2020-03-20 23:22:32 +08:00
|
|
|
}
|
2021-08-01 09:17:37 +08:00
|
|
|
// Inject Request object into context.
|
|
|
|
if RequestFromCtx(r.context) == nil {
|
|
|
|
r.context = context.WithValue(r.context, ctxKeyForRequest, r)
|
|
|
|
}
|
2020-03-20 23:22:32 +08:00
|
|
|
return r.context
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:31:45 +08:00
|
|
|
// GetCtx retrieves and returns the request's context.
|
2020-03-20 23:22:32 +08:00
|
|
|
func (r *Request) GetCtx() context.Context {
|
|
|
|
return r.Context()
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:57:08 +08:00
|
|
|
// SetCtx custom context for current request.
|
|
|
|
func (r *Request) SetCtx(ctx context.Context) {
|
|
|
|
r.context = ctx
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:22:32 +08:00
|
|
|
// GetCtxVar retrieves and returns a Var with given key name.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The optional parameter `def` specifies the default value of the Var if given `key`
|
2020-04-06 22:31:45 +08:00
|
|
|
// does not exist in the context.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var {
|
2020-03-20 23:22:32 +08:00
|
|
|
value := r.Context().Value(key)
|
|
|
|
if value == nil && len(def) > 0 {
|
|
|
|
value = def[0]
|
|
|
|
}
|
|
|
|
return gvar.New(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCtxVar sets custom parameter to context with key-value pair.
|
|
|
|
func (r *Request) SetCtxVar(key interface{}, value interface{}) {
|
|
|
|
r.context = context.WithValue(r.Context(), key, value)
|
|
|
|
}
|