mirror of https://github.com/gin-gonic/gin.git
Renaming Context.Req to Context.Request
This commit is contained in:
parent
6b6ec5be77
commit
2078ecd8e1
2
auth.go
2
auth.go
|
@ -74,7 +74,7 @@ func BasicAuth(accounts Accounts) HandlerFunc {
|
||||||
}
|
}
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
// Search user in the slice of allowed credentials
|
// Search user in the slice of allowed credentials
|
||||||
user := searchCredential(pairs, c.Req.Header.Get("Authorization"))
|
user := searchCredential(pairs, c.Request.Header.Get("Authorization"))
|
||||||
if len(user) == 0 {
|
if len(user) == 0 {
|
||||||
// Credentials doesn't match, we return 401 Unauthorized and abort request.
|
// Credentials doesn't match, we return 401 Unauthorized and abort request.
|
||||||
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
||||||
|
|
|
@ -14,7 +14,7 @@ func (c *Context) EnsureBody(item interface{}) bool {
|
||||||
// DEPRECATED use bindings directly
|
// DEPRECATED use bindings directly
|
||||||
// Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.
|
// Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.
|
||||||
func (c *Context) ParseBody(item interface{}) error {
|
func (c *Context) ParseBody(item interface{}) error {
|
||||||
return binding.JSON.Bind(c.Req, item)
|
return binding.JSON.Bind(c.Request, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED use gin.Static() instead
|
// DEPRECATED use gin.Static() instead
|
||||||
|
|
18
gin.go
18
gin.go
|
@ -49,7 +49,7 @@ type (
|
||||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
Context struct {
|
Context struct {
|
||||||
Req *http.Request
|
Request *http.Request
|
||||||
Writer ResponseWriter
|
Writer ResponseWriter
|
||||||
Keys map[string]interface{}
|
Keys map[string]interface{}
|
||||||
Errors errorMsgs
|
Errors errorMsgs
|
||||||
|
@ -185,7 +185,7 @@ func (engine *Engine) RunTLS(addr string, cert string, key string) {
|
||||||
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
||||||
c := engine.cache.Get().(*Context)
|
c := engine.cache.Get().(*Context)
|
||||||
c.Writer.reset(w)
|
c.Writer.reset(w)
|
||||||
c.Req = req
|
c.Request = req
|
||||||
c.Params = params
|
c.Params = params
|
||||||
c.handlers = handlers
|
c.handlers = handlers
|
||||||
c.Keys = nil
|
c.Keys = nil
|
||||||
|
@ -276,10 +276,10 @@ func (group *RouterGroup) Static(p, root string) {
|
||||||
fileServer := http.FileServer(http.Dir(root))
|
fileServer := http.FileServer(http.Dir(root))
|
||||||
|
|
||||||
group.GET(p, func(c *Context) {
|
group.GET(p, func(c *Context) {
|
||||||
original := c.Req.URL.Path
|
original := c.Request.URL.Path
|
||||||
c.Req.URL.Path = c.Params.ByName("filepath")
|
c.Request.URL.Path = c.Params.ByName("filepath")
|
||||||
fileServer.ServeHTTP(c.Writer, c.Req)
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||||
c.Req.URL.Path = original
|
c.Request.URL.Path = original
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -412,9 +412,9 @@ func filterFlags(content string) string {
|
||||||
// if Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer.Like ParseBody() but this method also writes a 400 error if the json is not valid.
|
// if Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer.Like ParseBody() but this method also writes a 400 error if the json is not valid.
|
||||||
func (c *Context) Bind(obj interface{}) bool {
|
func (c *Context) Bind(obj interface{}) bool {
|
||||||
var b binding.Binding
|
var b binding.Binding
|
||||||
ctype := filterFlags(c.Req.Header.Get("Content-Type"))
|
ctype := filterFlags(c.Request.Header.Get("Content-Type"))
|
||||||
switch {
|
switch {
|
||||||
case c.Req.Method == "GET" || ctype == MIMEPOSTForm:
|
case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
|
||||||
b = binding.Form
|
b = binding.Form
|
||||||
case ctype == MIMEJSON:
|
case ctype == MIMEJSON:
|
||||||
b = binding.JSON
|
b = binding.JSON
|
||||||
|
@ -428,7 +428,7 @@ func (c *Context) Bind(obj interface{}) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) BindWith(obj interface{}, b binding.Binding) bool {
|
func (c *Context) BindWith(obj interface{}, b binding.Binding) bool {
|
||||||
if err := b.Bind(c.Req, obj); err != nil {
|
if err := b.Bind(c.Request, obj); err != nil {
|
||||||
c.Fail(400, err)
|
c.Fail(400, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,15 +42,15 @@ func Logger() HandlerFunc {
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// save the IP of the requester
|
// save the IP of the requester
|
||||||
requester := c.Req.Header.Get("X-Real-IP")
|
requester := c.Request.Header.Get("X-Real-IP")
|
||||||
// if the requester-header is empty, check the forwarded-header
|
// if the requester-header is empty, check the forwarded-header
|
||||||
if requester == "" {
|
if requester == "" {
|
||||||
requester = c.Req.Header.Get("X-Forwarded-For")
|
requester = c.Request.Header.Get("X-Forwarded-For")
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the requester is still empty, use the hard-coded address from the socket
|
// if the requester is still empty, use the hard-coded address from the socket
|
||||||
if requester == "" {
|
if requester == "" {
|
||||||
requester = c.Req.RemoteAddr
|
requester = c.Request.RemoteAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
var color string
|
var color string
|
||||||
|
@ -72,7 +72,7 @@ func Logger() HandlerFunc {
|
||||||
color, code, reset,
|
color, code, reset,
|
||||||
latency,
|
latency,
|
||||||
requester,
|
requester,
|
||||||
c.Req.Method, c.Req.URL.Path,
|
c.Request.Method, c.Request.URL.Path,
|
||||||
)
|
)
|
||||||
|
|
||||||
// Calculate resolution time
|
// Calculate resolution time
|
||||||
|
|
Loading…
Reference in New Issue