Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique Headers #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ 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) {
// 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
if age == 0 {
Expand All @@ -101,6 +103,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)
}

Expand All @@ -113,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")
Expand All @@ -128,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) {
Expand Down Expand Up @@ -215,9 +220,13 @@ 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
c.conn.Header().Set(hdr, val)
// 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
}
c.conn.Header().Add(hdr, val)
}

func (c *httpConn) WriteString(content string) {
Expand Down