瀏覽代碼

Renaming Context.Req to Context.Request

Manu Mtz-Almeida 11 年之前
父節點
當前提交
2078ecd8e1
共有 4 個文件被更改,包括 15 次插入15 次删除
  1. 1 1
      auth.go
  2. 1 1
      deprecated.go
  3. 9 9
      gin.go
  4. 4 4
      logger.go

+ 1 - 1
auth.go

@@ -74,7 +74,7 @@ func BasicAuth(accounts Accounts) HandlerFunc {
 	}
 	return func(c *Context) {
 		// 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 {
 			// Credentials doesn't match, we return 401 Unauthorized and abort request.
 			c.Writer.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")

+ 1 - 1
deprecated.go

@@ -14,7 +14,7 @@ func (c *Context) EnsureBody(item interface{}) bool {
 // DEPRECATED use bindings directly
 // 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 {
-	return binding.JSON.Bind(c.Req, item)
+	return binding.JSON.Bind(c.Request, item)
 }
 
 // DEPRECATED use gin.Static() instead

+ 9 - 9
gin.go

@@ -49,7 +49,7 @@ type (
 	// 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.
 	Context struct {
-		Req      *http.Request
+		Request  *http.Request
 		Writer   ResponseWriter
 		Keys     map[string]interface{}
 		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 {
 	c := engine.cache.Get().(*Context)
 	c.Writer.reset(w)
-	c.Req = req
+	c.Request = req
 	c.Params = params
 	c.handlers = handlers
 	c.Keys = nil
@@ -276,10 +276,10 @@ func (group *RouterGroup) Static(p, root string) {
 	fileServer := http.FileServer(http.Dir(root))
 
 	group.GET(p, func(c *Context) {
-		original := c.Req.URL.Path
-		c.Req.URL.Path = c.Params.ByName("filepath")
-		fileServer.ServeHTTP(c.Writer, c.Req)
-		c.Req.URL.Path = original
+		original := c.Request.URL.Path
+		c.Request.URL.Path = c.Params.ByName("filepath")
+		fileServer.ServeHTTP(c.Writer, c.Request)
+		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.
 func (c *Context) Bind(obj interface{}) bool {
 	var b binding.Binding
-	ctype := filterFlags(c.Req.Header.Get("Content-Type"))
+	ctype := filterFlags(c.Request.Header.Get("Content-Type"))
 	switch {
-	case c.Req.Method == "GET" || ctype == MIMEPOSTForm:
+	case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
 		b = binding.Form
 	case ctype == MIMEJSON:
 		b = binding.JSON
@@ -428,7 +428,7 @@ func (c *Context) Bind(obj interface{}) 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)
 		return false
 	}

+ 4 - 4
logger.go

@@ -42,15 +42,15 @@ func Logger() HandlerFunc {
 		c.Next()
 
 		// 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 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 requester == "" {
-			requester = c.Req.RemoteAddr
+			requester = c.Request.RemoteAddr
 		}
 
 		var color string
@@ -72,7 +72,7 @@ func Logger() HandlerFunc {
 			color, code, reset,
 			latency,
 			requester,
-			c.Req.Method, c.Req.URL.Path,
+			c.Request.Method, c.Request.URL.Path,
 		)
 
 		// Calculate resolution time