From e5787f0f39f5482ab59c3df948035e64d2848496 Mon Sep 17 00:00:00 2001 From: Henry Rapley Date: Tue, 7 Feb 2012 07:38:44 +0000 Subject: [PATCH 1/3] attempted bufgix for setting unique headers --- web.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/web.go b/web.go index f911dc0a..b0491298 100644 --- a/web.go +++ b/web.go @@ -90,7 +90,7 @@ func (ctx *Context) ContentType(ext string) { } //Sets a cookie -- duration is the amount of time in seconds. 0 = forever -func (ctx *Context) SetCookie(name string, value string, age int64) { +func (ctx *Context) SetCookie(name string, value string, age int64, settings ...string) { var utctime time.Time var tdelta time.Duration if age == 0 { @@ -101,6 +101,9 @@ func (ctx *Context) SetCookie(name string, value string, age int64) { } utctime = time.Now().Add(tdelta).UTC() cookie := fmt.Sprintf("%s=%s; expires=%s", name, value, webTime(utctime)) + for _, setting := range settings { + cookie = fmt.Sprintf("%s; %s", cookie, setting) + } ctx.SetHeader("Set-Cookie", cookie, false) } @@ -217,7 +220,11 @@ func (c *httpConn) StartResponse(status int) { c.conn.WriteHeader(status) } func (c *httpConn) SetHeader(hdr string, val string, unique bool) { //right now unique can't be implemented through the http package. //see issue 488 - c.conn.Header().Set(hdr, val) + if unique { + c.conn.Header().Set(hdr, val) + return + } + c.conn.Header().Add(hdr, val) } func (c *httpConn) WriteString(content string) { From ef0d07d37c848c6aa1d40e16ae8a067ab71e585e Mon Sep 17 00:00:00 2001 From: Henry Rapley Date: Tue, 7 Feb 2012 07:47:00 +0000 Subject: [PATCH 2/3] debug message --- web.go | 1 + 1 file changed, 1 insertion(+) diff --git a/web.go b/web.go index b0491298..61e54f1a 100644 --- a/web.go +++ b/web.go @@ -224,6 +224,7 @@ func (c *httpConn) SetHeader(hdr string, val string, unique bool) { c.conn.Header().Set(hdr, val) return } + println(hdr) c.conn.Header().Add(hdr, val) } From 40623e4a1906f5ca41e71f01e53dbc40f3f5b8c7 Mon Sep 17 00:00:00 2001 From: Henry Rapley Date: Tue, 7 Feb 2012 17:45:53 +0000 Subject: [PATCH 3/3] fixed unique headers and changed ctx.SetCookie slightly --- web.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/web.go b/web.go index 61e54f1a..d10860ae 100644 --- a/web.go +++ b/web.go @@ -90,6 +90,8 @@ func (ctx *Context) ContentType(ext string) { } //Sets a cookie -- duration is the amount of time in seconds. 0 = forever +// Use `settings` to set other parts of the cookie eg: +// ctx.SetCookie("name", "value", 3600, "path=/", "HttpOnly") func (ctx *Context) SetCookie(name string, value string, age int64, settings ...string) { var utctime time.Time var tdelta time.Duration @@ -116,7 +118,7 @@ func getCookieSig(key string, val []byte, timestamp string) string { return hex } -func (ctx *Context) SetSecureCookie(name string, val string, age int64) { +func (ctx *Context) SetSecureCookie(name string, val string, age int64, settings ...string) { //base64 encode the val if len(ctx.Server.Config.CookieSecret) == 0 { ctx.Logger.Println("Secret Key for secure cookies has not been set. Please call web.SetCookieSecret") @@ -131,7 +133,7 @@ func (ctx *Context) SetSecureCookie(name string, val string, age int64) { timestamp := strconv.FormatInt(time.Now().Unix(), 10) sig := getCookieSig(ctx.Server.Config.CookieSecret, vb, timestamp) cookie := strings.Join([]string{vs, timestamp, sig}, "|") - ctx.SetCookie(name, cookie, age) + ctx.SetCookie(name, cookie, age, settings...) } func (ctx *Context) GetSecureCookie(name string) (string, bool) { @@ -218,13 +220,12 @@ type httpConn struct { func (c *httpConn) StartResponse(status int) { c.conn.WriteHeader(status) } func (c *httpConn) SetHeader(hdr string, val string, unique bool) { - //right now unique can't be implemented through the http package. - //see issue 488 + // Really really naive way to try and implement `unique`. + // Seems to work well enough for my uses. if unique { c.conn.Header().Set(hdr, val) return } - println(hdr) c.conn.Header().Add(hdr, val) }