Skip to content

Commit

Permalink
refactor: remove unused error of add cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 12, 2020
1 parent 95b5ae9 commit d4a76be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
22 changes: 9 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand All @@ -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], ","))
})

Expand Down

0 comments on commit d4a76be

Please sign in to comment.