Skip to content

Commit a52b83b

Browse files
fix: handle localhost domains in cookies
1 parent 8b48954 commit a52b83b

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

lua/rest-nvim/cookie_jar.lua

+2-16
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,6 @@ function M.load_jar()
5454
end
5555
end
5656

57-
---parse url to domain and path
58-
---path will be fallback to "/" if not found
59-
---@param url string
60-
---@return string domain
61-
---@return string path
62-
local function parse_url(url)
63-
local domain, path = url:match("^%a+://([^/]+)(/[^?#]*)$")
64-
if not path then
65-
domain = url:match("^%a+://([^/]+)")
66-
path = "/"
67-
end
68-
return domain, path
69-
end
70-
7157
---@private
7258
---parse Set-Cookie header to cookie
7359
---@param req_url string request URL to be used as fallback domain & path of cookie
@@ -105,7 +91,7 @@ function M.parse_set_cookie(req_url, header)
10591
cookie.priority = val
10692
end
10793
end
108-
cookie.domain = cookie.domain or ("." .. req_url:match("^%a+://([^/]+)"))
94+
cookie.domain = cookie.domain or ("." .. utils.parse_url(req_url))
10995
cookie.path = cookie.path or "/"
11096
if max_age == -1 then
11197
cookie.expires = -1
@@ -184,7 +170,7 @@ function M.save_jar()
184170
end
185171

186172
local function match_cookie(url, cookie)
187-
local req_domain, req_path = parse_url(url)
173+
local req_domain, req_path = utils.parse_url(url)
188174
if not req_domain then
189175
return false
190176
end

lua/rest-nvim/utils.lua

+11
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ function utils.parse_http_time(time_str)
105105
return os.time(time_table) + gmt_offset
106106
end
107107

108+
---parse url to domain and path
109+
---path will be fallback to "/" if not found
110+
---@param url string
111+
---@return string domain
112+
---@return string path
113+
function utils.parse_url(url)
114+
local domain = url:match("^%a+://([^/]+)") or url:match("^([^/]+)")
115+
local path = url:match("[^:/]+(/[^?#]*)") or "/"
116+
return domain, path
117+
end
118+
108119
--- Default transformers for statistics
109120
local transform = {
110121
---Transform `time` into a readable typed time (e.g. 200ms)

spec/utils_spec.lua

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ local function open(path)
77
return 0
88
end
99

10+
describe("basic utils", function()
11+
it("parse_url", function()
12+
local function parse_url(url)
13+
local domain, path = utils.parse_url(url)
14+
return { domain = domain, path = path }
15+
end
16+
assert.same({
17+
domain = "domain.com",
18+
path = "/some/path",
19+
}, parse_url("http://domain.com/some/path?query=value"))
20+
assert.same({
21+
domain = "domain.com",
22+
path = "/some/path",
23+
}, parse_url("domain.com/some/path?query=value"))
24+
assert.same({
25+
domain = "localhost:8000",
26+
path = "/some/path",
27+
}, parse_url("localhost:8000/some/path?query=value"))
28+
end)
29+
end)
30+
1031
describe("tree-sitter utils", function()
1132
local source = open("spec/examples/script/post_request_script.http")
1233
it("ts_parse_source", function()

0 commit comments

Comments
 (0)