fix: gin Context nil engine check

This commit is contained in:
weedge 2022-08-18 00:03:18 +08:00
parent 1c48977cca
commit b1d0b15eb8

View File

@ -524,6 +524,10 @@ func (c *Context) PostFormArray(key string) (values []string) {
} }
func (c *Context) initFormCache() { func (c *Context) initFormCache() {
if c.engine == nil {
return
}
if c.formCache == nil { if c.formCache == nil {
c.formCache = make(url.Values) c.formCache = make(url.Values)
req := c.Request req := c.Request
@ -574,6 +578,9 @@ func (c *Context) get(m map[string][]string, key string) (map[string]string, boo
// FormFile returns the first file for the provided form key. // FormFile returns the first file for the provided form key.
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) { func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
if c.engine == nil {
return nil, nil
}
if c.Request.MultipartForm == nil { if c.Request.MultipartForm == nil {
if err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { if err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
return nil, err return nil, err
@ -589,6 +596,9 @@ func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
// MultipartForm is the parsed multipart form, including file uploads. // MultipartForm is the parsed multipart form, including file uploads.
func (c *Context) MultipartForm() (*multipart.Form, error) { func (c *Context) MultipartForm() (*multipart.Form, error) {
if c.engine == nil {
return nil, nil
}
err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory) err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory)
return c.Request.MultipartForm, err return c.Request.MultipartForm, err
} }
@ -763,6 +773,9 @@ func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error
// If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, // If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy,
// the remote IP (coming from Request.RemoteAddr) is returned. // the remote IP (coming from Request.RemoteAddr) is returned.
func (c *Context) ClientIP() string { func (c *Context) ClientIP() string {
if c.engine == nil {
return ""
}
// Check if we're running on a trusted platform, continue running backwards if error // Check if we're running on a trusted platform, continue running backwards if error
if c.engine.TrustedPlatform != "" { if c.engine.TrustedPlatform != "" {
// Developers can define their own header of Trusted Platform or use predefined constants // Developers can define their own header of Trusted Platform or use predefined constants
@ -926,6 +939,9 @@ func (c *Context) Render(code int, r render.Render) {
// It also updates the HTTP code and sets the Content-Type as "text/html". // It also updates the HTTP code and sets the Content-Type as "text/html".
// See http://golang.org/doc/articles/wiki/ // See http://golang.org/doc/articles/wiki/
func (c *Context) HTML(code int, name string, obj any) { func (c *Context) HTML(code int, name string, obj any) {
if c.engine == nil {
return
}
instance := c.engine.HTMLRender.Instance(name, obj) instance := c.engine.HTMLRender.Instance(name, obj)
c.Render(code, instance) c.Render(code, instance)
} }
@ -942,6 +958,9 @@ func (c *Context) IndentedJSON(code int, obj any) {
// Default prepends "while(1)," to response body if the given struct is array values. // Default prepends "while(1)," to response body if the given struct is array values.
// It also sets the Content-Type as "application/json". // It also sets the Content-Type as "application/json".
func (c *Context) SecureJSON(code int, obj any) { func (c *Context) SecureJSON(code int, obj any) {
if c.engine == nil {
return
}
c.Render(code, render.SecureJSON{Prefix: c.engine.secureJSONPrefix, Data: obj}) c.Render(code, render.SecureJSON{Prefix: c.engine.secureJSONPrefix, Data: obj})
} }
@ -1168,7 +1187,7 @@ func (c *Context) SetAccepted(formats ...string) {
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context. // Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
func (c *Context) Deadline() (deadline time.Time, ok bool) { func (c *Context) Deadline() (deadline time.Time, ok bool) {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil { if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
return return
} }
return c.Request.Context().Deadline() return c.Request.Context().Deadline()
@ -1176,7 +1195,7 @@ func (c *Context) Deadline() (deadline time.Time, ok bool) {
// Done returns nil (chan which will wait forever) when c.Request has no Context. // Done returns nil (chan which will wait forever) when c.Request has no Context.
func (c *Context) Done() <-chan struct{} { func (c *Context) Done() <-chan struct{} {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil { if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
return nil return nil
} }
return c.Request.Context().Done() return c.Request.Context().Done()
@ -1184,7 +1203,7 @@ func (c *Context) Done() <-chan struct{} {
// Err returns nil when c.Request has no Context. // Err returns nil when c.Request has no Context.
func (c *Context) Err() error { func (c *Context) Err() error {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil { if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
return nil return nil
} }
return c.Request.Context().Err() return c.Request.Context().Err()
@ -1205,7 +1224,7 @@ func (c *Context) Value(key any) any {
return val return val
} }
} }
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil { if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
return nil return nil
} }
return c.Request.Context().Value(key) return c.Request.Context().Value(key)