Skip to content

Commit b641e4b

Browse files
committed
Added support for chunked cookie reading (getcookie), setcookie is still not WIP.
1 parent 85916c0 commit b641e4b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/resty/session.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local hmac = ngx.hmac_sha1
66
local time = ngx.time
77
local http_time = ngx.http_time
88
local find = string.find
9+
local sub = string.sub
910
local type = type
1011
local pcall = pcall
1112
local tonumber = tonumber
@@ -96,6 +97,21 @@ local function setcookie(session, value, expires)
9697
return true
9798
end
9899

100+
local function getcookie(name, i)
101+
local n = { "cookie_", name }
102+
if i then
103+
n[3] = "."
104+
n[4] = i
105+
else
106+
i = 1
107+
end
108+
local c = var[concat(n)]
109+
if not c then return nil end
110+
local l = #c
111+
if l < 4001 then return c end
112+
return concat{ sub(c, 1, 4000), getcookie(name, i + 1) }
113+
end
114+
99115
local function save(session, close)
100116
session.expires = time() + session.cookie.lifetime
101117
local i, e, s = session.id, session.expires, session.storage
@@ -147,7 +163,7 @@ local defaults = {
147163
defaults.secret = var.session_secret or random(32, true) or random(32)
148164

149165
local session = {
150-
_VERSION = "2.14"
166+
_VERSION = "2.15-dev"
151167
}
152168

153169
session.__index = session
@@ -245,7 +261,7 @@ function session.open(opts)
245261
scheme
246262
}
247263
self.opened = true
248-
local cookie = var["cookie_" .. self.name]
264+
local cookie = getcookie(self.name)
249265
if cookie then
250266
local i, e, d, h = self.storage:open(cookie, self.cookie.lifetime)
251267
if i and e and e > time() and d and h then

0 commit comments

Comments
 (0)