diff options
author | Blaž Hrastnik | 2021-10-17 04:54:47 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-10-17 04:54:47 +0000 |
commit | ea59f77a6baa0658b50fed50739e1f8956819395 (patch) | |
tree | 8046b1b1528765fd8e5a82277352e15150af577c | |
parent | 0a6b60085a56876068bf652c9491c435e9cca229 (diff) |
Port over parsing improvements from the LSP
We need to terminate if we ever read 0 bytes which indicates closed
stream.
-rw-r--r-- | helix-dap/src/transport.rs | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs index a20dfbe1..3f4eec43 100644 --- a/helix-dap/src/transport.rs +++ b/helix-dap/src/transport.rs @@ -82,26 +82,29 @@ impl Transport { let mut content_length = None; loop { buffer.truncate(0); - reader.read_line(buffer).await?; - let header = buffer.trim(); + if reader.read_line(buffer).await? == 0 { + return Err(Error::StreamClosed); + }; - if header.is_empty() { + if buffer == "\r\n" { + // look for an empty CRLF line break; } - let mut parts = header.split(": "); + let header = buffer.trim(); + let parts = header.split_once(": "); - match (parts.next(), parts.next(), parts.next()) { - (Some("Content-Length"), Some(value), None) => { + match parts { + Some(("Content-Length", value)) => { content_length = Some(value.parse().context("invalid content length")?); } - (Some(_), Some(_), None) => {} - _ => { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Failed to parse header", - ) - .into()); + Some((_, _)) => {} + None => { + // Workaround: Some non-conformant language servers will output logging and other garbage + // into the same stream as JSON-RPC messages. This can also happen from shell scripts that spawn + // the server. Skip such lines and log a warning. + + // warn!("Failed to parse header: {:?}", header); } } } @@ -126,7 +129,9 @@ impl Transport { buffer: &mut String, ) -> Result<()> { buffer.truncate(0); - err.read_line(buffer).await?; + if err.read_line(buffer).await? == 0 { + return Err(Error::StreamClosed); + }; error!("err <- {}", buffer); Ok(()) |