From e1d27558b3b1aa95a864036b1960fe2c267fb3ea Mon Sep 17 00:00:00 2001 From: Damon Zhao Date: Tue, 8 Sep 2015 15:37:20 +0800 Subject: [PATCH] add parameters for set cookie --- context.go | 52 +++++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/context.go b/context.go index c4fb96f8..37b11003 100644 --- a/context.go +++ b/context.go @@ -322,51 +322,29 @@ func (c *Context) Header(key, value string) { } } -func (c *Context) SetCookie(name string, value string, options ...interface{}) { +func (c *Context) SetCookie( + name string, + value string, + maxAge int, + path string, + domain string, + secure bool, + httpOnly bool, +) { cookie := http.Cookie{} cookie.Name = name cookie.Value = url.QueryEscape(value) - if len(options) > 0 { - switch v := options[0].(type) { - case int: - cookie.MaxAge = v - case int64: - cookie.MaxAge = int(v) - case int32: - cookie.MaxAge = int(v) - } - } + cookie.MaxAge = maxAge cookie.Path = "/" - if len(options) > 1 { - if v, ok := options[1].(string); ok && len(v) > 0 { - cookie.Path = v - } + if path != "" { + cookie.Path = path } - if len(options) > 2 { - if v, ok := options[2].(string); ok && len(v) > 0 { - cookie.Domain = v - } - } - - if len(options) > 3 { - switch v := options[3].(type) { - case bool: - cookie.Secure = v - default: - if options[3] != nil { - cookie.Secure = true - } - } - } - - if len(options) > 4 { - if v, ok := options[4].(bool); ok && v { - cookie.HttpOnly = true - } - } + cookie.Domain = domain + cookie.Secure = secure + cookie.HttpOnly = httpOnly c.Writer.Header().Add("Set-Cookie", cookie.String()) }