From 0fd6cb126b6238d2cbc01bed12ff043f45039c76 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Mon, 29 Nov 2010 21:50:12 +0100 Subject: Having a better gestion of cookies. --- lib/httplib.lua | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'lib/httplib.lua') diff --git a/lib/httplib.lua b/lib/httplib.lua index f0e3453..42c42a6 100644 --- a/lib/httplib.lua +++ b/lib/httplib.lua @@ -52,30 +52,37 @@ local function clean_sessions() end end -function get_session(req, res) - local sid - - clean_sessions() - +function get_cookies(req) + local cookies = {} if req.headers.Cookie then local cookie_strings = split(req.headers.Cookie, ";") - local cookies = {} local k, v for k, v in pairs(cookie_strings) do local cname, cvalue = string.match(v, "(%w+)=(.+)") cookies[cname] = cvalue end - sid = cookies.sid end + return cookies +end + +function set_cookie(res, key, val) + res.cookies_path = "/" + res.cookies = key .. "=" .. val +end + +function get_session(req, res) + local sid + + clean_sessions() + + sid = get_cookies(req)["sid"] if not sid or not sessions[sid] then sid = generate_new_session() end - res.cookies_path = "/" - res.cookies = "sid=" .. sid - + set_cookie(res, "sid", sid) sessions[sid].access = time() return sessions[sid].data, sid -- cgit v1.2.3