Skip to content

Commit

Permalink
more fixes for release.2011-03-07.1 : get Cookies from http.Request.H…
Browse files Browse the repository at this point in the history
…eader
  • Loading branch information
[email protected] committed Mar 11, 2011
1 parent 66e3813 commit dc7f9ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 50 deletions.
26 changes: 2 additions & 24 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Request struct {
Params map[string]string
ParamData []byte
Cookies map[string]string
Cookie []*http.Cookie
Files map[string]filedata
RemoteAddr string
RemotePort int
Expand Down Expand Up @@ -79,6 +80,7 @@ func newRequest(hr *http.Request, hc http.ResponseWriter) *Request {
Referer: hr.Referer,
UserAgent: hr.UserAgent,
FullParams: hr.Form,
Cookie: hr.Cookie,
RemoteAddr: remoteAddr.IP.String(),
RemotePort: remoteAddr.Port,
}
Expand Down Expand Up @@ -256,30 +258,6 @@ func (r *Request) parseParams() (err os.Error) {
return nil
}

func (r *Request) parseCookies() (err os.Error) {
if r.Cookies != nil {
return
}

r.Cookies = make(map[string]string)

if va, ok := r.Headers["Cookie"]; ok {
for _, v := range va {
cookies := strings.Split(v, ";", -1)
for _, cookie := range cookies {
cookie = strings.TrimSpace(cookie)
parts := strings.Split(cookie, "=", 2)
if len(parts) != 2 {
continue
}
r.Cookies[parts[0]] = parts[1]
}
}
}

return nil
}

func (r *Request) HasFile(name string) bool {
if r.Files == nil || len(r.Files) == 0 {
return false
Expand Down
48 changes: 22 additions & 26 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,34 @@ func (ctx *Context) SetSecureCookie(name string, val string, age int64) {
}

func (ctx *Context) GetSecureCookie(name string) (string, bool) {
cookie, ok := ctx.Request.Cookies[name]
if !ok {
return "", false
}
for _, cookie := range ctx.Request.Cookie {
if cookie.Name != name {
continue
}

parts := strings.Split(cookie, "|", 3)
parts := strings.Split(cookie.Value, "|", 3)

val := parts[0]
timestamp := parts[1]
sig := parts[2]
val := parts[0]
timestamp := parts[1]
sig := parts[2]

if getCookieSig(ctx.Server.Config.CookieSecret, []byte(val), timestamp) != sig {
return "", false
}
if getCookieSig(ctx.Server.Config.CookieSecret, []byte(val), timestamp) != sig {
return "", false
}

ts, _ := strconv.Atoi64(timestamp)
ts, _ := strconv.Atoi64(timestamp)

if time.Seconds()-31*86400 > ts {
return "", false
}
if time.Seconds()-31*86400 > ts {
return "", false
}

buf := bytes.NewBufferString(val)
encoder := base64.NewDecoder(base64.StdEncoding, buf)
buf := bytes.NewBufferString(val)
encoder := base64.NewDecoder(base64.StdEncoding, buf)

res, _ := ioutil.ReadAll(encoder)
return string(res), true
res, _ := ioutil.ReadAll(encoder)
return string(res), true
}
return "", false
}

// small optimization: cache the context type instead of repeteadly calling reflect.Typeof
Expand Down Expand Up @@ -226,7 +228,7 @@ func (c *httpConn) Write(content []byte) (n int, err os.Error) {
}

func (c *httpConn) Close() {
rwc, buf, _ := c.conn.(http.Hijacker).Hijack()
rwc, buf, _ := c.conn.(http.Hijacker).Hijack()
if buf != nil {
buf.Flush()
}
Expand Down Expand Up @@ -316,12 +318,6 @@ func (s *Server) routeHandler(req *Request, c conn) {
s.Logger.Printf("Failed to parse form data %q\n", perr.String())
}

//parse the cookies
perr = req.parseCookies()
if perr != nil {
s.Logger.Printf("Failed to parse cookies %q", perr.String())
}

ctx := Context{req, s, c, false}

//set some default headers
Expand Down

0 comments on commit dc7f9ee

Please sign in to comment.