Skip to content

Commit e2f13a2

Browse files
fix(cookie): clean() should keep cookies (fix #434)
1 parent 412a103 commit e2f13a2

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lua/rest-nvim/cookie_jar.lua

+13-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ local config = require("rest-nvim.config")
1515
---@field domain string
1616
---@field path string
1717
---@field expires integer
18-
---@field max_date integer?
1918
---@field secure boolean?
2019
---@field httponly boolean?
2120
---@field samesite string?
@@ -61,9 +60,9 @@ end
6160
---@return string domain
6261
---@return string path
6362
local function parse_url(url)
64-
local domain, path = url:match("^https?://([^/]+)(/[^?#]*)$")
63+
local domain, path = url:match("^%a+://([^/]+)(/[^?#]*)$")
6564
if not path then
66-
domain = url:match("^https?://([^/]+)")
65+
domain = url:match("^%a+://([^/]+)")
6766
path = "/"
6867
end
6968
return domain, path
@@ -80,10 +79,12 @@ function M.parse_set_cookie(req_url, header)
8079
logger.error("Invalid Set-Cookie header: " .. header)
8180
return
8281
end
82+
logger.debug("parsing set-cookie:", name, value)
8383
local cookie = {
8484
name = name,
8585
value = value or "",
8686
}
87+
local max_age
8788
for attr, val in header:gmatch(";%s*([^=]+)=?([^;]*)") do
8889
attr = attr:lower()
8990
if attr == "domain" then
@@ -93,7 +94,7 @@ function M.parse_set_cookie(req_url, header)
9394
elseif attr == "expires" then
9495
cookie.expires = utils.parse_http_time(val)
9596
elseif attr == "max-age" then
96-
cookie.max_age = tonumber(val)
97+
max_age = tonumber(val)
9798
elseif attr == "secure" then
9899
cookie.secure = true
99100
elseif attr == "httponly" then
@@ -104,10 +105,15 @@ function M.parse_set_cookie(req_url, header)
104105
cookie.priority = val
105106
end
106107
end
107-
cookie.domain = cookie.domain or req_url:match("^https?://([^/]+)")
108-
cookie.domain = "." .. cookie.domain
108+
cookie.domain = cookie.domain or ("." .. req_url:match("^%a+://([^/]+)"))
109109
cookie.path = cookie.path or "/"
110+
if max_age == -1 then
111+
cookie.expires = -1
112+
elseif max_age then
113+
cookie.expires = os.time() + max_age
114+
end
110115
cookie.expires = cookie.expires or -1
116+
logger.debug("cookie parsed from Set-Cookie Header:", cookie)
111117
return cookie
112118
end
113119

@@ -149,7 +155,7 @@ end
149155
function M.clean()
150156
M.jar = vim.iter(M.jar)
151157
:filter(function(cookie)
152-
return cookie.max_age == 0 or cookie.expires < os.time()
158+
return cookie.expires == -1 or cookie.expires > os.time()
153159
end)
154160
:totable()
155161
end

spec/cookie_jar_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("Cookies unit tests", function()
2929
domain = ".example.com",
3030
path = "/",
3131
expires = -1,
32-
}, jar.parse_set_cookie(url, "cookie3=value3;domain=example.com"))
32+
}, jar.parse_set_cookie(url, "cookie3=value3;domain=.example.com"))
3333
assert.is_same({
3434
name = "cookie4",
3535
value = "value4",

0 commit comments

Comments
 (0)