summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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