Skip to content

Commit 20fe347

Browse files
authored
curl: Fix response status parsing (#633)
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]>
1 parent 3707cdb commit 20fe347

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lua/plenary/curl.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,25 @@ end
251251
------------------------------------------------------------
252252
parse.response = function(lines, dump_path, code)
253253
local headers = P.readlines(dump_path)
254-
local status = tonumber(string.match(headers[1], "([%w+]%d+)"))
255-
local body = F.join(lines, "\n")
254+
local status = nil
255+
local processed_headers = {}
256+
257+
-- Process headers in a single pass
258+
for _, line in ipairs(headers) do
259+
local status_match = line:match "^HTTP/%S*%s+(%d+)"
260+
if status_match then
261+
status = tonumber(status_match)
262+
elseif line ~= "" then
263+
table.insert(processed_headers, line)
264+
end
265+
end
256266

267+
local body = F.join(lines, "\n")
257268
vim.loop.fs_unlink(dump_path)
258-
table.remove(headers, 1)
259269

260270
return {
261-
status = status,
262-
headers = headers,
271+
status = status or 0,
272+
headers = processed_headers,
263273
body = body,
264274
exit = code,
265275
}

0 commit comments

Comments
 (0)