1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use tokio::io;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc;

use crate::json_rpc::{ClientMessage, ServerMessage};
use crate::transport::Connection;

fn parse_header_line(header_line: &str) -> (String, String) {
    let mut parts = header_line.splitn(2, ':');

    let name = parts
        .next()
        .expect("Did not find header name")
        .trim()
        .to_ascii_lowercase();

    let value = parts
        .next()
        .expect("Did not find header value")
        .trim()
        .to_owned();

    (name, value)
}

/// Waits for the passed I/O future and `break`s from the current loop if the pipe is broken
///
/// This is useful to propagate closing `stdout`/`stdin` by closing the respective MPSC channel.
macro_rules! break_on_broken_pipe {
    ($io_future:expr, $message:expr) => {
        if let Err(err) = $io_future.await {
            if err.kind() == io::ErrorKind::BrokenPipe {
                break;
            } else {
                panic!("{}: {:?}", $message, err);
            }
        }
    };
}

pub fn create_connection(
    mut reader: impl io::AsyncBufRead + Unpin + Send + 'static,
    mut writer: impl io::AsyncWrite + Unpin + Send + 'static,
) -> Connection {
    // Allow some concurrency with the session but 4 message is a bit excessive
    // This allows for backpressure on `stdin`/`stdout`
    let (send_outgoing, mut recv_outgoing) = mpsc::channel::<ServerMessage>(4);
    let (send_incoming, recv_incoming) = mpsc::channel::<ClientMessage>(4);

    // Write all our responses out sequentially
    tokio::spawn(async move {
        while let Some(response) = recv_outgoing.recv().await {
            let response_bytes =
                serde_json::to_vec(&response).expect("Could not serialise response");

            break_on_broken_pipe!(
                writer.write_all(
                    format!("Content-Length: {}\r\n\r\n", response_bytes.len()).as_bytes()
                ),
                "Could not write response header"
            );

            break_on_broken_pipe!(
                writer.write_all(&response_bytes),
                "Could not write response body"
            );

            break_on_broken_pipe!(writer.flush(), "Could not flush writer");
        }
    });

    tokio::spawn(async move {
        loop {
            let mut content_length: Option<usize> = None;
            let mut line_buffer = String::new();

            // Read the header
            loop {
                break_on_broken_pipe!(
                    reader.read_line(&mut line_buffer),
                    "Could not read header line from stdin"
                );

                if line_buffer == "\r\n" {
                    // Read full header
                    break;
                }

                let (name, value) = parse_header_line(&line_buffer);
                if name == "content-length" {
                    content_length = Some(value.parse().expect("Cannot parse Content-Length"));
                }

                line_buffer.clear();
            }

            let content_length = content_length.expect("Header had no Content-Length");

            // Read the entire content
            let mut read_buffer = Vec::<u8>::new();
            read_buffer.resize(content_length, 0);

            break_on_broken_pipe!(
                reader.read_exact(&mut read_buffer),
                "Could not read body from stdin"
            );

            let client_message: ClientMessage =
                serde_json::from_slice(&read_buffer).expect("Invalid JSON");

            if send_incoming.send(client_message).await.is_err() {
                // Channel closed
                break;
            }
        }
    });

    Connection {
        incoming: recv_incoming,
        outgoing: send_outgoing,
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use crate::json_rpc::Notification;

    #[tokio::test]
    async fn test_happy_recv_notification() {
        let body = br#"{"jsonrpc":"2.0","method":"initialized","params":{}}"#;

        let mut message = format!("Content-Length: {}\r\n", body.len()).into_bytes();
        message.extend_from_slice(b"\r\n");
        message.extend_from_slice(body);

        let Connection { mut incoming, .. } = create_connection(
            io::BufReader::new(std::io::Cursor::new(message)),
            Vec::new(),
        );

        let client_message = incoming.recv().await.unwrap();
        assert_eq!(
            ClientMessage::Notification(Notification::new_lsp::<
                lsp_types::notification::Initialized,
            >(lsp_types::InitializedParams {})),
            client_message,
        );
    }
}