Skip to content

Commit 99407c9

Browse files
committed
Implemented chunked setcookie.
1 parent b641e4b commit 99407c9

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

lib/resty/session.lua

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local concat = table.concat
55
local hmac = ngx.hmac_sha1
66
local time = ngx.time
77
local http_time = ngx.http_time
8+
local ceil = math.ceil
89
local find = string.find
910
local sub = string.sub
1011
local type = type
@@ -38,8 +39,7 @@ local function setcookie(session, value, expires)
3839
if ngx.headers_sent then return nil, "Attempt to set session cookie after sending out response headers." end
3940
local c = session.cookie
4041
local i = 3
41-
local n = session.name .. "="
42-
local k = { n, value or "" }
42+
local k = {}
4343
local d = c.domain
4444
local x = c.samesite
4545
if expires then
@@ -72,26 +72,44 @@ local function setcookie(session, value, expires)
7272
if c.httponly then
7373
k[i] = "; HttpOnly"
7474
end
75-
k = concat(k)
75+
local v = value or ""
76+
local l = ceil(#v / 4000)
7677
local s = header["Set-Cookie"]
77-
local t = type(s)
78-
if t == "table" then
79-
local f = false
80-
local z = #s
81-
for i=1, z do
82-
if find(s[i], n, 1, true) == 1 then
83-
s[i] = k
84-
f = true
85-
break
86-
end
78+
for j=1, l do
79+
local n = { session.name }
80+
if j > 1 then
81+
n[2] = "."
82+
n[3] = j
83+
n[4] = "="
84+
else
85+
n[2] = "="
8786
end
88-
if not f then
89-
s[z+1] = k
87+
k[1] = concat(n)
88+
if j < l then
89+
k[2] = sub(v, j * 4000 - 3999, 4000) .. "+"
90+
else
91+
k[2] = sub(v, j * 4000 - 3999)
92+
end
93+
k = concat(k)
94+
local t = type(s)
95+
if t == "table" then
96+
local f = false
97+
local z = #s
98+
for i=1, z do
99+
if find(s[i], n, 1, true) == 1 then
100+
s[i] = k
101+
f = true
102+
break
103+
end
104+
end
105+
if not f then
106+
s[z+1] = k
107+
end
108+
elseif t == "string" and find(s, n, 1, true) ~= 1 then
109+
s = { s, k }
110+
else
111+
s = k
90112
end
91-
elseif t == "string" and find(s, n, 1, true) ~= 1 then
92-
s = { s, k }
93-
else
94-
s = k
95113
end
96114
header["Set-Cookie"] = s
97115
return true

0 commit comments

Comments
 (0)