|
| 1 | +//! The endpoint a `tcpAnnounced` adapter prints on its own output. |
| 2 | +//! |
| 3 | +//! The host reads the adapter's output line by line and asks Core about each |
| 4 | +//! one until a line announces the endpoint, then connects to it. Core decides |
| 5 | +//! which line is the announcement and which endpoints are acceptable, so every |
| 6 | +//! platform host reads the same announcements the same way. Core never reads |
| 7 | +//! the process output, opens a socket, resolves a name or keeps a clock: the |
| 8 | +//! host bounds the wait with the transport's discovery deadline. |
| 9 | +
|
| 10 | +use super::*; |
| 11 | +use lithe_protocol::{CoreError, ErrorCode}; |
| 12 | + |
| 13 | +/// Longest endpoint text after the prefix, in bytes: room for a bracketed IPv6 |
| 14 | +/// address and a port, with margin. |
| 15 | +const MAX_ANNOUNCED_ENDPOINT: usize = 128; |
| 16 | + |
| 17 | +/// Where a `port` announcement listens: adapters that print only a port listen |
| 18 | +/// on the IPv4 loopback address. |
| 19 | +const ANNOUNCED_PORT_HOST: &str = "127.0.0.1"; |
| 20 | + |
| 21 | +/// `debug.adapters.readAnnouncement`: whether one output line of a |
| 22 | +/// `tcpAnnounced` adapter announces its endpoint. |
| 23 | +/// |
| 24 | +/// The payload is `{ endpoint, stream, line }`: the `endpoint` of the |
| 25 | +/// adapter's launch plan, the stream the line came from, and the line without |
| 26 | +/// its line break. A line on another stream, or one without the prefix, is |
| 27 | +/// `{ status: "none" }`. A line with the prefix is the announcement: the rest |
| 28 | +/// of the line after the prefix's first occurrence, without surrounding |
| 29 | +/// whitespace, is `{ status: "announced", host, port }` when it is a loopback |
| 30 | +/// endpoint, and `{ status: "invalid", reason }` otherwise. A malformed |
| 31 | +/// payload fails with `invalid_request`. |
| 32 | +pub fn read_announcement(payload: Value) -> Result<Value, CoreError> { |
| 33 | + #[derive(Deserialize)] |
| 34 | + #[serde(deny_unknown_fields)] |
| 35 | + struct Request { |
| 36 | + endpoint: DebugEndpoint, |
| 37 | + stream: String, |
| 38 | + line: String, |
| 39 | + } |
| 40 | + let request: Request = serde_json::from_value(payload).map_err(|error| { |
| 41 | + CoreError::new(ErrorCode::InvalidRequest, "Invalid announcement request.") |
| 42 | + .with_details(error.to_string()) |
| 43 | + })?; |
| 44 | + if !request.endpoint.is_announced() { |
| 45 | + return Err(CoreError::new( |
| 46 | + ErrorCode::InvalidRequest, |
| 47 | + "An announced endpoint names a stream, a one-line prefix, and port or hostPort.", |
| 48 | + )); |
| 49 | + } |
| 50 | + if !matches!(request.stream.as_str(), "stdout" | "stderr") { |
| 51 | + return Err(CoreError::new( |
| 52 | + ErrorCode::InvalidRequest, |
| 53 | + "A line comes from stdout or stderr.", |
| 54 | + )); |
| 55 | + } |
| 56 | + let endpoint = request.endpoint; |
| 57 | + let prefix = endpoint.prefix.as_deref().unwrap_or_default(); |
| 58 | + if endpoint.stream.as_deref() != Some(request.stream.as_str()) { |
| 59 | + return Ok(json!({ "status": "none" })); |
| 60 | + } |
| 61 | + let Some((_, rest)) = request.line.split_once(prefix) else { |
| 62 | + return Ok(json!({ "status": "none" })); |
| 63 | + }; |
| 64 | + let announced = rest.trim(); |
| 65 | + let parsed = match endpoint.value.as_deref() { |
| 66 | + Some("port") => announced_port(announced).map(|port| (ANNOUNCED_PORT_HOST, port)), |
| 67 | + _ => announced_host_port(announced), |
| 68 | + }; |
| 69 | + Ok(match parsed { |
| 70 | + Err(reason) => json!({ "status": "invalid", "reason": reason }), |
| 71 | + Ok((host, port)) => json!({ "status": "announced", "host": host, "port": port }), |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +/// Why an announcement is not an endpoint the host may connect to. |
| 76 | +type Refusal = &'static str; |
| 77 | + |
| 78 | +/// The text is not a port, or not `host:port` with a valid host. |
| 79 | +const MALFORMED: Refusal = "malformed"; |
| 80 | + |
| 81 | +/// The text names a host Core cannot tell is this machine. |
| 82 | +const NOT_LOOPBACK: Refusal = "notLoopback"; |
| 83 | + |
| 84 | +/// A port from 1 to 65535 written in decimal digits only. |
| 85 | +fn announced_port(text: &str) -> Result<u16, Refusal> { |
| 86 | + if text.is_empty() || text.len() > 5 || !text.bytes().all(|byte| byte.is_ascii_digit()) { |
| 87 | + return Err(MALFORMED); |
| 88 | + } |
| 89 | + match text.parse::<u16>() { |
| 90 | + Ok(port) if port >= 1 => Ok(port), |
| 91 | + _ => Err(MALFORMED), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// `host:port`, or `[address]:port` for IPv6, with a loopback host. |
| 96 | +fn announced_host_port(text: &str) -> Result<(&str, u16), Refusal> { |
| 97 | + if text.len() > MAX_ANNOUNCED_ENDPOINT { |
| 98 | + return Err(MALFORMED); |
| 99 | + } |
| 100 | + let (host, port) = match text.strip_prefix('[') { |
| 101 | + Some(bracketed) => { |
| 102 | + let (address, port) = bracketed.split_once("]:").ok_or(MALFORMED)?; |
| 103 | + if !address.contains(':') { |
| 104 | + return Err(MALFORMED); |
| 105 | + } |
| 106 | + (address, port) |
| 107 | + } |
| 108 | + None => { |
| 109 | + let (host, port) = text.rsplit_once(':').ok_or(MALFORMED)?; |
| 110 | + if host.contains(':') { |
| 111 | + return Err(MALFORMED); |
| 112 | + } |
| 113 | + (host, port) |
| 114 | + } |
| 115 | + }; |
| 116 | + let port = announced_port(port)?; |
| 117 | + if host_problem(host).is_some() { |
| 118 | + return Err(MALFORMED); |
| 119 | + } |
| 120 | + if !is_loopback_host(host) { |
| 121 | + return Err(NOT_LOOPBACK); |
| 122 | + } |
| 123 | + Ok((host, port)) |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod tests { |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn announcement_fixtures_match_the_reading() { |
| 132 | + let fixture: Value = serde_json::from_str(include_str!(concat!( |
| 133 | + env!("CARGO_MANIFEST_DIR"), |
| 134 | + "/../../fixtures/debug/adapter-announcements-v1.json" |
| 135 | + ))) |
| 136 | + .expect("announcement fixture is JSON"); |
| 137 | + let cases = fixture["cases"].as_array().expect("fixture cases"); |
| 138 | + assert!(!cases.is_empty()); |
| 139 | + for case in cases { |
| 140 | + let result = read_announcement(case["input"].clone()); |
| 141 | + match case.get("result") { |
| 142 | + Some(expected) => assert_eq!(result.expect("a reading"), *expected, "{case}"), |
| 143 | + None => assert!(result.is_err(), "{case}"), |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments