From 83ec39d02efb06d098b6f7becedb1be34a6fd3ce Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 10 Nov 2015 11:47:10 +0800 Subject: [PATCH] refactor max age cookies setting --- context/output.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/context/output.go b/context/output.go index a72c6f74..6ffc5072 100644 --- a/context/output.go +++ b/context/output.go @@ -80,25 +80,21 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface //fix cookie not work in IE if len(others) > 0 { + var maxAge int64 + switch v := others[0].(type) { case int: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) - } else if v <= 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } - case int64: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) - } else if v <= 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } + maxAge = int64(v) case int32: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) - } else if v <= 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } + maxAge = int64(v) + case int64: + maxAge = v + } + + if maxAge > 0 { + fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(maxAge)*time.Second).UTC().Format(time.RFC1123), maxAge) + } else if maxAge <= 0 { + fmt.Fprintf(&b, "; Max-Age=0") } }