mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
Compare commits
4 Commits
41b201fcf8
...
8fc2951efc
Author | SHA1 | Date | |
---|---|---|---|
|
8fc2951efc | ||
|
de17f182bb | ||
|
29ee7e600e | ||
|
b781e570b3 |
@ -16,34 +16,21 @@ local function format_message_with_content_length(message)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class (private) vim.lsp.rpc.Headers: {string: any}
|
--- Extract content-length from the msg header
|
||||||
---@field content_length integer
|
|
||||||
|
|
||||||
--- Parses an LSP Message's header
|
|
||||||
---
|
---
|
||||||
---@param header string The header to parse.
|
---@param header string The header to parse
|
||||||
---@return vim.lsp.rpc.Headers#parsed headers
|
---@return integer?
|
||||||
local function parse_headers(header)
|
local function get_content_length(header)
|
||||||
assert(type(header) == 'string', 'header must be a string')
|
for line in header:gmatch('(.-)\r\n') do
|
||||||
--- @type vim.lsp.rpc.Headers
|
|
||||||
local headers = {}
|
|
||||||
for line in vim.gsplit(header, '\r\n', { plain = true }) do
|
|
||||||
if line == '' then
|
if line == '' then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
--- @type string?, string?
|
local key, value = line:match('^%s*(%S+)%s*:%s*(%d+)%s*$')
|
||||||
local key, value = line:match('^%s*(%S+)%s*:%s*(.+)%s*$')
|
if key and key:lower() == 'content-length' then
|
||||||
if key then
|
return tonumber(value)
|
||||||
key = key:lower():gsub('%-', '_') --- @type string
|
|
||||||
headers[key] = value
|
|
||||||
else
|
|
||||||
log.error('invalid header line %q', line)
|
|
||||||
error(string.format('invalid header line %q', line))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
headers.content_length = tonumber(headers.content_length)
|
error('Content-Length not found in header: ' .. header)
|
||||||
or error(string.format('Content-Length not found in headers. %q', header))
|
|
||||||
return headers
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This is the start of any possible header patterns. The gsub converts it to a
|
-- This is the start of any possible header patterns. The gsub converts it to a
|
||||||
@ -52,15 +39,45 @@ local header_start_pattern = ('content'):gsub('%w', function(c)
|
|||||||
return '[' .. c .. c:upper() .. ']'
|
return '[' .. c .. c:upper() .. ']'
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
local has_strbuffer, strbuffer = pcall(require, "string.buffer")
|
||||||
|
|
||||||
--- The actual workhorse.
|
--- The actual workhorse.
|
||||||
local function request_parser_loop()
|
---@type function
|
||||||
|
local request_parser_loop
|
||||||
|
|
||||||
|
if has_strbuffer then
|
||||||
|
request_parser_loop = function()
|
||||||
|
local buf = strbuffer.new()
|
||||||
|
while true do
|
||||||
|
local msg = buf:tostring()
|
||||||
|
local header_end = msg:find('\r\n\r\n', 1, true)
|
||||||
|
if header_end then
|
||||||
|
local header = buf:get(header_end + 1)
|
||||||
|
buf:skip(2) -- skip past header boundary
|
||||||
|
local content_length = get_content_length(header)
|
||||||
|
while #buf < content_length do
|
||||||
|
local chunk = coroutine.yield()
|
||||||
|
buf:put(chunk)
|
||||||
|
end
|
||||||
|
local body = buf:get(content_length)
|
||||||
|
local chunk = coroutine.yield(body)
|
||||||
|
buf:put(chunk)
|
||||||
|
else
|
||||||
|
local chunk = coroutine.yield()
|
||||||
|
buf:put(chunk)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
request_parser_loop = function()
|
||||||
local buffer = '' -- only for header part
|
local buffer = '' -- only for header part
|
||||||
while true do
|
while true do
|
||||||
-- A message can only be complete if it has a double CRLF and also the full
|
-- A message can only be complete if it has a double CRLF and also the full
|
||||||
-- payload, so first let's check for the CRLFs
|
-- payload, so first let's check for the CRLFs
|
||||||
local start, finish = buffer:find('\r\n\r\n', 1, true)
|
local header_end, body_start = buffer:find('\r\n\r\n', 1, true)
|
||||||
-- Start parsing the headers
|
-- Start parsing the headers
|
||||||
if start then
|
if header_end then
|
||||||
-- This is a workaround for servers sending initial garbage before
|
-- This is a workaround for servers sending initial garbage before
|
||||||
-- sending headers, such as if a bash script sends stdout. It assumes
|
-- sending headers, such as if a bash script sends stdout. It assumes
|
||||||
-- that we know all of the headers ahead of time. At this moment, the
|
-- that we know all of the headers ahead of time. At this moment, the
|
||||||
@ -76,13 +93,13 @@ local function request_parser_loop()
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
local headers = parse_headers(buffer:sub(buffer_start, start - 1))
|
local header = buffer:sub(buffer_start, header_end + 1)
|
||||||
local content_length = headers.content_length
|
local content_length = get_content_length(header)
|
||||||
-- Use table instead of just string to buffer the message. It prevents
|
-- Use table instead of just string to buffer the message. It prevents
|
||||||
-- a ton of strings allocating.
|
-- a ton of strings allocating.
|
||||||
-- ref. http://www.lua.org/pil/11.6.html
|
-- ref. http://www.lua.org/pil/11.6.html
|
||||||
---@type string[]
|
---@type string[]
|
||||||
local body_chunks = { buffer:sub(finish + 1) }
|
local body_chunks = { buffer:sub(body_start + 1) }
|
||||||
local body_length = #body_chunks[1]
|
local body_length = #body_chunks[1]
|
||||||
-- Keep waiting for data until we have enough.
|
-- Keep waiting for data until we have enough.
|
||||||
while body_length < content_length do
|
while body_length < content_length do
|
||||||
@ -103,7 +120,7 @@ local function request_parser_loop()
|
|||||||
-- Yield our data.
|
-- Yield our data.
|
||||||
|
|
||||||
--- @type string
|
--- @type string
|
||||||
local data = coroutine.yield(headers, body)
|
local data = coroutine.yield(body)
|
||||||
or error('Expected more data for the body. The server may have died.')
|
or error('Expected more data for the body. The server may have died.')
|
||||||
buffer = rest .. data
|
buffer = rest .. data
|
||||||
else
|
else
|
||||||
@ -115,6 +132,8 @@ local function request_parser_loop()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -237,7 +256,7 @@ local default_dispatchers = {
|
|||||||
--- @param on_exit? fun()
|
--- @param on_exit? fun()
|
||||||
--- @param on_error fun(err: any)
|
--- @param on_error fun(err: any)
|
||||||
function M.create_read_loop(handle_body, on_exit, on_error)
|
function M.create_read_loop(handle_body, on_exit, on_error)
|
||||||
local parse_chunk = coroutine.wrap(request_parser_loop) --[[@as fun(chunk: string?): vim.lsp.rpc.Headers?, string?]]
|
local parse_chunk = coroutine.wrap(request_parser_loop) --[[@as fun(chunk: string?): string]]
|
||||||
parse_chunk()
|
parse_chunk()
|
||||||
return function(err, chunk)
|
return function(err, chunk)
|
||||||
if err then
|
if err then
|
||||||
@ -253,9 +272,9 @@ function M.create_read_loop(handle_body, on_exit, on_error)
|
|||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local headers, body = parse_chunk(chunk)
|
local body = parse_chunk(chunk)
|
||||||
if headers then
|
if body then
|
||||||
handle_body(assert(body))
|
handle_body(body)
|
||||||
chunk = ''
|
chunk = ''
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user