@@ -5,6 +5,7 @@ local concat = table.concat
5
5
local hmac = ngx .hmac_sha1
6
6
local time = ngx .time
7
7
local http_time = ngx .http_time
8
+ local ceil = math.ceil
8
9
local find = string.find
9
10
local sub = string.sub
10
11
local type = type
@@ -38,8 +39,7 @@ local function setcookie(session, value, expires)
38
39
if ngx .headers_sent then return nil , " Attempt to set session cookie after sending out response headers." end
39
40
local c = session .cookie
40
41
local i = 3
41
- local n = session .name .. " ="
42
- local k = { n , value or " " }
42
+ local k = {}
43
43
local d = c .domain
44
44
local x = c .samesite
45
45
if expires then
@@ -72,26 +72,44 @@ local function setcookie(session, value, expires)
72
72
if c .httponly then
73
73
k [i ] = " ; HttpOnly"
74
74
end
75
- k = concat (k )
75
+ local v = value or " "
76
+ local l = ceil (# v / 4000 )
76
77
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 ] = " ="
87
86
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
90
112
end
91
- elseif t == " string" and find (s , n , 1 , true ) ~= 1 then
92
- s = { s , k }
93
- else
94
- s = k
95
113
end
96
114
header [" Set-Cookie" ] = s
97
115
return true
0 commit comments