Skip to content

Commit

Permalink
curl: Fix response status parsing (#633)
Browse files Browse the repository at this point in the history
The response headers can have multiple steps in them, example:

"HTTP/1.1 200 Connection established",
"",
"HTTP/1.1 400 Bad Request",
"Content-Security-Policy: default-src 'none'; sandbox",
"Content-Type: text/plain; charset=utf-8",
"Strict-Transport-Security: max-age=31536000",
"X-Content-Type-Options: nosniff",

before this would be reported as 200 incorrectly instead of 400 bad request.
this fixes that issue

Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam authored Feb 11, 2025
1 parent 3707cdb commit 20fe347
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lua/plenary/curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,25 @@ end
------------------------------------------------------------
parse.response = function(lines, dump_path, code)
local headers = P.readlines(dump_path)
local status = tonumber(string.match(headers[1], "([%w+]%d+)"))
local body = F.join(lines, "\n")
local status = nil
local processed_headers = {}

-- Process headers in a single pass
for _, line in ipairs(headers) do
local status_match = line:match "^HTTP/%S*%s+(%d+)"
if status_match then
status = tonumber(status_match)
elseif line ~= "" then
table.insert(processed_headers, line)
end
end

local body = F.join(lines, "\n")
vim.loop.fs_unlink(dump_path)
table.remove(headers, 1)

return {
status = status,
headers = headers,
status = status or 0,
headers = processed_headers,
body = body,
exit = code,
}
Expand Down

0 comments on commit 20fe347

Please sign in to comment.