Port over parsing improvements from the LSP

We need to terminate if we ever read 0 bytes which indicates closed
stream.
pull/574/head
Blaž Hrastnik 3 years ago
parent 0a6b60085a
commit ea59f77a6b

@ -82,26 +82,29 @@ impl Transport {
let mut content_length = None; let mut content_length = None;
loop { loop {
buffer.truncate(0); buffer.truncate(0);
reader.read_line(buffer).await?; if reader.read_line(buffer).await? == 0 {
let header = buffer.trim(); return Err(Error::StreamClosed);
};
if header.is_empty() { if buffer == "\r\n" {
// look for an empty CRLF line
break; break;
} }
let mut parts = header.split(": "); let header = buffer.trim();
let parts = header.split_once(": ");
match (parts.next(), parts.next(), parts.next()) { match parts {
(Some("Content-Length"), Some(value), None) => { Some(("Content-Length", value)) => {
content_length = Some(value.parse().context("invalid content length")?); content_length = Some(value.parse().context("invalid content length")?);
} }
(Some(_), Some(_), None) => {} Some((_, _)) => {}
_ => { None => {
return Err(std::io::Error::new( // Workaround: Some non-conformant language servers will output logging and other garbage
std::io::ErrorKind::Other, // into the same stream as JSON-RPC messages. This can also happen from shell scripts that spawn
"Failed to parse header", // the server. Skip such lines and log a warning.
)
.into()); // warn!("Failed to parse header: {:?}", header);
} }
} }
} }
@ -126,7 +129,9 @@ impl Transport {
buffer: &mut String, buffer: &mut String,
) -> Result<()> { ) -> Result<()> {
buffer.truncate(0); buffer.truncate(0);
err.read_line(buffer).await?; if err.read_line(buffer).await? == 0 {
return Err(Error::StreamClosed);
};
error!("err <- {}", buffer); error!("err <- {}", buffer);
Ok(()) Ok(())

Loading…
Cancel
Save