summaryrefslogtreecommitdiff
path: root/lib/httplib.lua
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2010-11-29 21:50:12 +0100
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2010-11-29 21:50:12 +0100
commit0fd6cb126b6238d2cbc01bed12ff043f45039c76 (patch)
tree207d3e0805608fb5cb41a2bd17effa1b83be1a06 /lib/httplib.lua
parente060d02904b24c548a13deab4e51afe0ad823207 (diff)
Having a better gestion of cookies.
Diffstat (limited to 'lib/httplib.lua')
-rw-r--r--lib/httplib.lua27
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