Skip to content

Commit 01d705b

Browse files
committed
Release 2.17.
1 parent fa15fbc commit 01d705b

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

Changes.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
All notable changes to `lua-resty-session` will be documented in this file.
44

5+
## [2.17] - 2017-06-12
6+
### Added
7+
- Added session.hide() function to hide session cookies from upstream
8+
on reverse proxy scenarios.
9+
510
## [2.16] - 2017-05-31
611
### Changed
712
- Delays setting the defaults until needed, allowing users to safely
813
require "resty.session" in different contexts.
914

1015
## [2.15] - 2017-02-13
11-
1216
## Added
1317
- Added a support for chunked cookies.
1418
See also: https://github.com/bungle/lua-resty-session/issues/35

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,16 @@ local session = require "resty.session".start()
656656
session:destroy()
657657
```
658658

659+
#### session:hide()
660+
661+
Sometimes, when you are using `lua-resty-session` in reverse proxy, you may want to hide the session
662+
cookies from the upstream server. To do that you can call `session:hide()`.
663+
664+
```lua
665+
local session = require "resty.session".start()
666+
session:hide()
667+
```
668+
659669
### Fields
660670

661671
#### string session.id

lib/resty/session.lua

+51-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ local concat = table.concat
55
local hmac = ngx.hmac_sha1
66
local time = ngx.time
77
local http_time = ngx.http_time
8+
local set_header = ngx.req.set_header
9+
local clear_header = ngx.req.clear_header
810
local ceil = math.ceil
911
local max = math.max
1012
local find = string.find
13+
local gsub = string.gsub
1114
local sub = string.sub
1215
local type = type
1316
local pcall = pcall
@@ -200,7 +203,7 @@ local function init()
200203
end
201204

202205
local session = {
203-
_VERSION = "2.16"
206+
_VERSION = "2.17"
204207
}
205208

206209
session.__index = session
@@ -369,4 +372,51 @@ function session:destroy()
369372
return setcookie(self, "", true)
370373
end
371374

375+
function session:hide()
376+
local cookies = var.http_cookie
377+
if not cookies then
378+
return
379+
end
380+
local r = {}
381+
local n = self.name
382+
local i = 1
383+
local j = 0
384+
local s = find(cookies, ";", 1, true)
385+
while s do
386+
local c = sub(cookies, i, s - 1)
387+
local b = find(c, "=", 1, true)
388+
if b then
389+
local key = gsub(sub(c, 1, b - 1), "^%s+", "")
390+
if key ~= n and key ~= "" then
391+
local z = #n
392+
if sub(key, z + 1, z + 1) ~= "_" or not tonumber(sub(key, z + 2)) then
393+
j = j + 1
394+
r[j] = c
395+
end
396+
end
397+
end
398+
i = s + 1
399+
s = find(cookies, ";", i, true)
400+
end
401+
local c = sub(cookies, i)
402+
if c and c ~= "" then
403+
local b = find(c, "=", 1, true)
404+
if b then
405+
local key = gsub(sub(c, 1, b - 1), "^%s+", "")
406+
if key ~= n and key ~= "" then
407+
local z = #n
408+
if sub(key, z + 1, z + 1) ~= "_" or not tonumber(sub(key, z + 2)) then
409+
j = j + 1
410+
r[j] = c
411+
end
412+
end
413+
end
414+
end
415+
if j == 0 then
416+
clear_header("Cookie")
417+
else
418+
set_header("Cookie", concat(r, "; ", 1, j))
419+
end
420+
end
421+
372422
return session

0 commit comments

Comments
 (0)