From f33a44bb1ffdfaa96acdca4e13ff159f75fa043c Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sat, 7 Mar 2015 22:02:15 +0700 Subject: [PATCH] Don't parse cookies twice --- server/request.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/request.go b/server/request.go index a986c4e..5c71be9 100644 --- a/server/request.go +++ b/server/request.go @@ -28,13 +28,12 @@ type ( ) func parseRequest(w http.ResponseWriter, r *http.Request) (*request, *statRequest) { - setCookie(w, r) - cook, _ := r.Cookie(cookieName) - login, _ := redis.String(redisPool.Get().Do("HGET", "sessions", cook.Value)) + sid := sessionID(w, r) + login, _ := redis.String(redisPool.Get().Do("HGET", "sessions", sid)) req := &request{ r: r, w: w, - sessionID: cook.Value, + sessionID: sid, login: login, } return req, parseStatRequest(r) @@ -77,8 +76,10 @@ func parseStatRequest(r *http.Request) *statRequest { } } -func setCookie(w http.ResponseWriter, r *http.Request) { - if cook, err := r.Cookie(cookieName); err != nil { +func sessionID(w http.ResponseWriter, r *http.Request) string { + var cook *http.Cookie + var err error + if cook, err = r.Cookie(cookieName); err != nil { cook = &http.Cookie{ Name: cookieName, Value: uuid.New(), @@ -89,4 +90,5 @@ func setCookie(w http.ResponseWriter, r *http.Request) { http.SetCookie(w, cook) r.AddCookie(cook) } + return cook.Value }