|
| 1 | +---@mod rest-nvim.cookie_jar Cookie handler module |
| 2 | + |
| 3 | +local M = { |
| 4 | + ---@type rest.Cookie[] |
| 5 | + jar = {}, |
| 6 | +} |
| 7 | + |
| 8 | +local utils = require("rest-nvim.utils") |
| 9 | +local logger = require("rest-nvim.logger") |
| 10 | +local config = require("rest-nvim.config") |
| 11 | + |
| 12 | +---@class rest.Cookie |
| 13 | +---@field name string |
| 14 | +---@field value string |
| 15 | +---@field domain string |
| 16 | +---@field path string |
| 17 | +---@field expires integer |
| 18 | +---@field max_date integer? |
| 19 | +---@field secure boolean? |
| 20 | +---@field httponly boolean? |
| 21 | +---@field samesite string? |
| 22 | +---@field priority string? |
| 23 | + |
| 24 | +---Load Cookie jar from rest-nvim.cookies file |
| 25 | +function M.load_jar() |
| 26 | + if not utils.file_exists(config.cookies.path) then |
| 27 | + return |
| 28 | + end |
| 29 | + local file, openerr = io.open(config.cookies.path, "r") |
| 30 | + if not file then |
| 31 | + local err_msg = string.format("Failed to open rest.nvim cookies file: %s", openerr) |
| 32 | + vim.notify(err_msg, vim.log.levels.ERROR) |
| 33 | + logger.error(err_msg) |
| 34 | + return |
| 35 | + end |
| 36 | + for line in file:lines() do |
| 37 | + local seps = vim.split(line, "\t") |
| 38 | + if seps[1] ~= "" and not vim.startswith(seps[1], "#") then |
| 39 | + if #seps ~= 5 then |
| 40 | + local err_msg = "error while parsing cookies file at line:\n" .. line .. "\n" |
| 41 | + vim.notify(err_msg, vim.log.levels.ERROR) |
| 42 | + logger.error(err_msg) |
| 43 | + return |
| 44 | + end |
| 45 | + ---@type rest.Cookie |
| 46 | + local cookie = { |
| 47 | + domain = seps[1], |
| 48 | + path = seps[2], |
| 49 | + name = seps[3], |
| 50 | + value = seps[4], |
| 51 | + expires = assert(tonumber(seps[5])), |
| 52 | + } |
| 53 | + table.insert(M.jar, cookie) |
| 54 | + end |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +---parse url to domain and path |
| 59 | +---path will be fallback to "/" if not found |
| 60 | +---@param url string |
| 61 | +---@return string domain |
| 62 | +---@return string path |
| 63 | +local function parse_url(url) |
| 64 | + local domain, path = url:match("^https?://([^/]+)(/[^?#]*)$") |
| 65 | + if not path then |
| 66 | + domain = url:match("^https?://([^/]+)") |
| 67 | + path = "/" |
| 68 | + end |
| 69 | + return domain, path |
| 70 | +end |
| 71 | + |
| 72 | +---@private |
| 73 | +---parse Set-Cookie header to cookie |
| 74 | +---@param req_url string request URL to be used as fallback domain & path of cookie |
| 75 | +---@param header string |
| 76 | +---@return rest.Cookie? |
| 77 | +function M.parse_set_cookie(req_url, header) |
| 78 | + local name, value = header:match("^%s*([^=]+)=([^;]*)") |
| 79 | + if not name then |
| 80 | + logger.error("Invalid Set-Cookie header: " .. header) |
| 81 | + return |
| 82 | + end |
| 83 | + local cookie = { |
| 84 | + name = name, |
| 85 | + value = value or "", |
| 86 | + } |
| 87 | + for attr, val in header:gmatch(";%s*([^=]+)=?([^;]*)") do |
| 88 | + attr = attr:lower() |
| 89 | + if attr == "domain" then |
| 90 | + cookie.domain = val |
| 91 | + elseif attr == "path" then |
| 92 | + cookie.path = val |
| 93 | + elseif attr == "expires" then |
| 94 | + cookie.expires = utils.parse_http_time(val) |
| 95 | + elseif attr == "max-age" then |
| 96 | + cookie.max_age = tonumber(val) |
| 97 | + elseif attr == "secure" then |
| 98 | + cookie.secure = true |
| 99 | + elseif attr == "httponly" then |
| 100 | + cookie.httponly = true |
| 101 | + elseif attr == "samesite" then |
| 102 | + cookie.samesite = val |
| 103 | + elseif attr == "priority" then |
| 104 | + cookie.priority = val |
| 105 | + end |
| 106 | + end |
| 107 | + cookie.domain = cookie.domain or req_url:match("^https?://([^/]+)") |
| 108 | + cookie.domain = "." .. cookie.domain |
| 109 | + cookie.path = cookie.path or "/" |
| 110 | + cookie.expires = cookie.expires or -1 |
| 111 | + return cookie |
| 112 | +end |
| 113 | + |
| 114 | +---@param jar rest.Cookie[] |
| 115 | +---@param cookie rest.Cookie |
| 116 | +local function jar_insert(jar, cookie) |
| 117 | + for i, c in ipairs(jar) do |
| 118 | + if c.name == cookie.name and c.domain == cookie.domain and c.path == cookie.path then |
| 119 | + jar[i] = cookie |
| 120 | + return |
| 121 | + end |
| 122 | + end |
| 123 | + table.insert(jar, cookie) |
| 124 | +end |
| 125 | + |
| 126 | +---@param fn function |
| 127 | +---@param arg any |
| 128 | +local function curry(fn, arg) |
| 129 | + return function(...) |
| 130 | + return fn(arg, ...) |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +---Save cookies from response |
| 135 | +---Request is provided as a context |
| 136 | +---@param req_url string |
| 137 | +---@param res rest.Response |
| 138 | +function M.update_jar(req_url, res) |
| 139 | + if not res.headers["set-cookie"] then |
| 140 | + return |
| 141 | + end |
| 142 | + vim.iter(res.headers["set-cookie"]):map(curry(M.parse_set_cookie, req_url)):each(curry(jar_insert, M.jar)) |
| 143 | + M.clean() |
| 144 | + M.save_jar() |
| 145 | +end |
| 146 | + |
| 147 | +---@private |
| 148 | +---Cleanup expired cookies |
| 149 | +function M.clean() |
| 150 | + M.jar = vim |
| 151 | + .iter(M.jar) |
| 152 | + :filter(function(cookie) |
| 153 | + return cookie.max_age == 0 or cookie.expires < os.time() |
| 154 | + end) |
| 155 | + :totable() |
| 156 | +end |
| 157 | + |
| 158 | +---Save current cookie jar to cookies file |
| 159 | +function M.save_jar() |
| 160 | + -- TOOD: make this function asynchronous |
| 161 | + local file, openerr = io.open(config.cookies.path, "w") |
| 162 | + if not file then |
| 163 | + local err_msg = string.format("Failed to open rest.nvim cookies file: %s", openerr) |
| 164 | + vim.notify(err_msg, vim.log.levels.ERROR) |
| 165 | + logger.error(err_msg) |
| 166 | + return |
| 167 | + end |
| 168 | + file:write("# domain\tpath\tname\tvalue\texpires\n") |
| 169 | + for _, cookie in ipairs(M.jar) do |
| 170 | + file:write(table.concat({ |
| 171 | + cookie.domain, |
| 172 | + cookie.path, |
| 173 | + cookie.name, |
| 174 | + cookie.value, |
| 175 | + cookie.expires, |
| 176 | + }, "\t") .. "\n") |
| 177 | + end |
| 178 | + file:close() |
| 179 | +end |
| 180 | + |
| 181 | +local function match_cookie(url, cookie) |
| 182 | + local req_domain, req_path = parse_url(url) |
| 183 | + if not req_domain then |
| 184 | + return false |
| 185 | + end |
| 186 | + local domain_matches = ("." .. req_domain):match(vim.pesc(cookie.domain) .. "$") |
| 187 | + local path_matches = req_path:sub(1, #cookie.path) == cookie.path |
| 188 | + if domain_matches and path_matches then |
| 189 | + logger.debug( |
| 190 | + ("cookie %s with domain %s and path %s matched to url: %s"):format(cookie.name, cookie.domain, cookie.path, url) |
| 191 | + ) |
| 192 | + else |
| 193 | + logger.debug( |
| 194 | + ("cookie %s with domain %s and path %s NOT matched to url: %s"):format( |
| 195 | + cookie.name, |
| 196 | + cookie.domain, |
| 197 | + cookie.path, |
| 198 | + url |
| 199 | + ) |
| 200 | + ) |
| 201 | + end |
| 202 | + return domain_matches and path_matches |
| 203 | +end |
| 204 | + |
| 205 | +---Load cookies for request |
| 206 | +---@param req rest.Request |
| 207 | +function M.load_cookies(req) |
| 208 | + logger.debug("loading cookies for request:" .. req.url) |
| 209 | + vim.iter(M.jar):filter(curry(match_cookie, req.url)):each(curry(jar_insert, req.cookies)) |
| 210 | +end |
| 211 | + |
| 212 | +M.load_jar() |
| 213 | + |
| 214 | +return M |
0 commit comments