diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2010-11-29 21:50:12 +0100 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2010-11-29 21:50:12 +0100 |
commit | 0fd6cb126b6238d2cbc01bed12ff043f45039c76 (patch) | |
tree | 207d3e0805608fb5cb41a2bd17effa1b83be1a06 /lib/httplib.lua | |
parent | e060d02904b24c548a13deab4e51afe0ad823207 (diff) |
Having a better gestion of cookies.
Diffstat (limited to 'lib/httplib.lua')
-rw-r--r-- | lib/httplib.lua | 27 |
1 files changed, 17 insertions, 10 deletions
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 |