@@ -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 ?
6160--- @return string domain
6261--- @return string path
6362local 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
112118end
113119
149155function 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 ()
155161end
0 commit comments