Merge branch 'develop' into performance

Conflicts:
	context.go
	context_test.go
	gin_test.go
	recovery_test.go
	utils.go
This commit is contained in:
Manu Mtz-Almeida
2015-04-08 13:37:25 +02:00
18 changed files with 971 additions and 102 deletions
+23 -22
View File
@@ -6,7 +6,7 @@ package gin
import (
"errors"
"log"
"fmt"
"math"
"net/http"
"strings"
@@ -32,7 +32,7 @@ type Context struct {
Engine *Engine
Keys map[string]interface{}
Errors errorMsgs
accepted []string
Accepted []string
}
/************************************/
@@ -46,7 +46,7 @@ func (c *Context) reset() {
c.index = -1
c.Keys = nil
c.Errors = c.Errors[0:0]
c.accepted = nil
c.Accepted = nil
}
func (c *Context) Copy() *Context {
@@ -83,6 +83,10 @@ func (c *Context) AbortWithStatus(code int) {
c.Abort()
}
func (c *Context) IsAborted() bool {
return c.index == AbortIndex
}
/************************************/
/********* ERROR MANAGEMENT *********/
/************************************/
@@ -98,7 +102,7 @@ func (c *Context) Fail(code int, err error) {
c.AbortWithStatus(code)
}
func (c *Context) ErrorTyped(err error, typ uint32, meta interface{}) {
func (c *Context) ErrorTyped(err error, typ int, meta interface{}) {
c.Errors = append(c.Errors, errorMsg{
Err: err.Error(),
Type: typ,
@@ -147,9 +151,8 @@ func (c *Context) MustGet(key string) interface{} {
if value, exists := c.Get(key); exists {
return value
} else {
log.Panicf("Key %s does not exist", key)
panic("Key " + key + " does not exist")
}
return nil
}
/************************************/
@@ -164,7 +167,7 @@ func (c *Context) ClientIP() string {
clientIP = c.Request.Header.Get("X-Forwarded-For")
clientIP = strings.Split(clientIP, ",")[0]
if len(clientIP) > 0 {
return clientIP
return strings.TrimSpace(clientIP)
}
return c.Request.RemoteAddr
}
@@ -237,7 +240,7 @@ func (c *Context) Redirect(code int, location string) {
if code >= 300 && code <= 308 {
c.Render(code, render.Redirect, c.Request, location)
} else {
log.Panicf("Cannot redirect with status code %d", code)
panic(fmt.Sprintf("Cannot redirect with status code %d", code))
}
}
@@ -276,7 +279,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
case binding.MIMEHTML:
if len(config.HTMLPath) == 0 {
log.Panic("negotiate config is wrong. html path is needed")
panic("negotiate config is wrong. html path is needed")
}
data := chooseData(config.HTMLData, config.Data)
c.HTML(code, config.HTMLPath, data)
@@ -292,26 +295,24 @@ func (c *Context) Negotiate(code int, config Negotiate) {
func (c *Context) NegotiateFormat(offered ...string) string {
if len(offered) == 0 {
log.Panic("you must provide at least one offer")
panic("you must provide at least one offer")
}
if c.accepted == nil {
c.accepted = parseAccept(c.Request.Header.Get("Accept"))
if c.Accepted == nil {
c.Accepted = parseAccept(c.Request.Header.Get("Accept"))
}
if len(c.accepted) == 0 {
if len(c.Accepted) == 0 {
return offered[0]
} else {
for _, accepted := range c.accepted {
for _, offert := range offered {
if accepted == offert {
return offert
}
}
for _, accepted := range c.Accepted {
for _, offert := range offered {
if accepted == offert {
return offert
}
}
return ""
}
return ""
}
func (c *Context) SetAccepted(formats ...string) {
c.accepted = formats
c.Accepted = formats
}