Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to pass certs #519

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lua/rest-nvim/client/curl/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,29 @@ local builder = {}
---@return string[] args
function builder.extras(req)
local args = {}

if config.clients.curl.opts.set_compressed then
if
vim.iter(req.headers):any(function(key, values)
return key == "accept-encoding"
and vim.iter(values):any(function(value)
return value:find("gzip")
end)
if vim.iter(req.headers):any(function(key, values)
return key == "accept-encoding" and vim.iter(values):any(function(value)
return value:find("gzip")
end)
then
end) then
vim.list_extend(args, { "--compressed" })
end
end
--
for domain, _ in pairs(config.clients.curl.opts.certificates) do
local target = req.url

local s, _ = string.find(target, domain, 1, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have domain matching functionality in cookie_jar.lua so we can use same logic for both, but for now, I think this is fine. I might add a TODO comment here though.

local function match_cookie(url, cookie)
local req_domain, req_path = utils.parse_url(url)
if not req_domain then
return false
end
local domain_matches = ("." .. req_domain):match(vim.pesc(cookie.domain) .. "$")
local path_matches = req_path:sub(1, #cookie.path) == cookie.path
if domain_matches and path_matches then
logger.debug(
("cookie %s with domain %s and path %s matched to url: %s"):format(
cookie.name,
cookie.domain,
cookie.path,
url
)
)
else
logger.debug(
("cookie %s with domain %s and path %s NOT matched to url: %s"):format(
cookie.name,
cookie.domain,
cookie.path,
url
)
)
end
return domain_matches and path_matches
end


if s ~= nil then
vim.list_extend(args, { "--cert", config.clients.curl.opts.certificates[domain].set_certificate_crt })
vim.list_extend(args, { "--key", config.clients.curl.opts.certificates[domain].set_certificate_key })
break
end
end

return args
end

Expand Down Expand Up @@ -273,7 +284,10 @@ function builder.statistics()
return ("? %s:%%{%s}\n"):format(style.id, style.id)
end)
:join("")
return { "-w", "%{stderr}" .. format }

local output_str = "'%{stderr}" .. format .. "'"

return { "-w", output_str }
end

---@package
Expand Down Expand Up @@ -320,6 +334,7 @@ function builder.build(req, ignore_stats)
if not ignore_stats then
insert(args, builder.STAT_ARGS)
end

return vim.iter(args):flatten(math.huge):totable()
end

Expand All @@ -345,6 +360,7 @@ function curl.request(request)
})
local future = nio.control.future()
local args = builder.build(request)

curl.cli(args, function(sc)
if sc.code ~= 0 then
local message = "Something went wrong when making the request with cURL:\n"
Expand Down
5 changes: 5 additions & 0 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ local default_config = {
---@type boolean Add `--compressed` argument when `Accept-Encoding` header includes
---`gzip`
set_compressed = false,
---@class rest.Config.Clients.Curl.Certificates
---@field set_certificate_crt string
---@field set_certificate_key string
---@type table<string, rest.Config.Clients.Curl.Certificates>
certificates = nil,
},
},
},
Expand Down