Skip to content

Commit

Permalink
fix(cookie): clean() should keep cookies (fix #434)
Browse files Browse the repository at this point in the history
  • Loading branch information
boltlessengineer committed Sep 2, 2024
1 parent 412a103 commit e2f13a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lua/rest-nvim/cookie_jar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ local config = require("rest-nvim.config")
---@field domain string
---@field path string
---@field expires integer
---@field max_date integer?
---@field secure boolean?
---@field httponly boolean?
---@field samesite string?
Expand Down Expand Up @@ -61,9 +60,9 @@ end
---@return string domain
---@return string path
local function parse_url(url)
local domain, path = url:match("^https?://([^/]+)(/[^?#]*)$")
local domain, path = url:match("^%a+://([^/]+)(/[^?#]*)$")
if not path then
domain = url:match("^https?://([^/]+)")
domain = url:match("^%a+://([^/]+)")
path = "/"
end
return domain, path
Expand All @@ -80,10 +79,12 @@ function M.parse_set_cookie(req_url, header)
logger.error("Invalid Set-Cookie header: " .. header)
return
end
logger.debug("parsing set-cookie:", name, value)
local cookie = {
name = name,
value = value or "",
}
local max_age
for attr, val in header:gmatch(";%s*([^=]+)=?([^;]*)") do
attr = attr:lower()
if attr == "domain" then
Expand All @@ -93,7 +94,7 @@ function M.parse_set_cookie(req_url, header)
elseif attr == "expires" then
cookie.expires = utils.parse_http_time(val)
elseif attr == "max-age" then
cookie.max_age = tonumber(val)
max_age = tonumber(val)
elseif attr == "secure" then
cookie.secure = true
elseif attr == "httponly" then
Expand All @@ -104,10 +105,15 @@ function M.parse_set_cookie(req_url, header)
cookie.priority = val
end
end
cookie.domain = cookie.domain or req_url:match("^https?://([^/]+)")
cookie.domain = "." .. cookie.domain
cookie.domain = cookie.domain or ("." .. req_url:match("^%a+://([^/]+)"))
cookie.path = cookie.path or "/"
if max_age == -1 then
cookie.expires = -1
elseif max_age then
cookie.expires = os.time() + max_age
end
cookie.expires = cookie.expires or -1
logger.debug("cookie parsed from Set-Cookie Header:", cookie)
return cookie
end

Expand Down Expand Up @@ -149,7 +155,7 @@ end
function M.clean()
M.jar = vim.iter(M.jar)
:filter(function(cookie)
return cookie.max_age == 0 or cookie.expires < os.time()
return cookie.expires == -1 or cookie.expires > os.time()
end)
:totable()
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cookie_jar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("Cookies unit tests", function()
domain = ".example.com",
path = "/",
expires = -1,
}, jar.parse_set_cookie(url, "cookie3=value3;domain=example.com"))
}, jar.parse_set_cookie(url, "cookie3=value3;domain=.example.com"))
assert.is_same({
name = "cookie4",
value = "value4",
Expand Down

0 comments on commit e2f13a2

Please sign in to comment.