diff --git a/context.go b/context.go index 569e18a..c1422a8 100644 --- a/context.go +++ b/context.go @@ -159,6 +159,7 @@ func GetClientIP(req *http.Request) string { ip := h.Get(HeaderXForwardedFor) if ip != "" { arr := sort.StringSlice(strings.Split(ip, ",")) + // 从后往前找第一个非内网IP的则为客户IP sort.Sort(sort.Reverse(arr)) for _, value := range arr { v := strings.TrimSpace(value) @@ -215,6 +216,7 @@ func (c *Context) QueryParam(name string) string { // Query get the query map. // It will return map[string]string, not the same as url.Values +// If want to get url.Values, use c.Request.URL.Query() func (c *Context) Query() map[string]string { query := c.getCacheQuery() m := make(map[string]string) @@ -414,9 +416,8 @@ func (c *Context) Cookie(name string) (*http.Cookie, error) { } // AddCookie add the cookie to the response -func (c *Context) AddCookie(cookie *http.Cookie) error { - c.AddHeader(HeaderSetCookie, cookie.String()) - return nil +func (c *Context) AddCookie(cookie *http.Cookie) { + http.SetCookie(c, cookie) } func (c *Context) getKeys() []string { @@ -480,7 +481,7 @@ func cloneCookie(cookie *http.Cookie) *http.Cookie { } } -func (c *Context) addSigCookie(cookie *http.Cookie) (err error) { +func (c *Context) addSigCookie(cookie *http.Cookie) { sc := cloneCookie(cookie) sc.Name = sc.Name + SignedCookieSuffix keys := c.getKeys() @@ -489,18 +490,13 @@ func (c *Context) addSigCookie(cookie *http.Cookie) (err error) { } kg := keygrip.New(keys) sc.Value = string(kg.Sign([]byte(sc.Value))) - err = c.AddCookie(sc) - return + c.AddCookie(sc) } // AddSignedCookie add the signed cookie to the response -func (c *Context) AddSignedCookie(cookie *http.Cookie) (err error) { - err = c.AddCookie(cookie) - if err != nil { - return - } - err = c.addSigCookie(cookie) - return +func (c *Context) AddSignedCookie(cookie *http.Cookie) { + c.AddCookie(cookie) + c.addSigCookie(cookie) } // NoContent no content for response diff --git a/context_test.go b/context_test.go index d5460e6..94daa0d 100644 --- a/context_test.go +++ b/context_test.go @@ -347,8 +347,7 @@ func TestCookie(t *testing.T) { Path: "/", HttpOnly: true, } - err := c.AddCookie(cookie) - assert.Nil(err) + c.AddCookie(cookie) assert.Equal("a=b; Path=/; Max-Age=300; HttpOnly; Secure", c.GetHeader(HeaderSetCookie)) }) @@ -375,8 +374,7 @@ func TestSignedCookie(t *testing.T) { Path: "/", HttpOnly: true, } - err := c.AddSignedCookie(cookie) - assert.Nil(err) + c.AddSignedCookie(cookie) assert.Equal("a=b; Path=/; Max-Age=300; HttpOnly; Secure,a.sig=9yv2rWFijew8K8a5Uw9jxRJE53s; Path=/; Max-Age=300; HttpOnly; Secure", strings.Join(c.Header()[HeaderSetCookie], ",")) })