(path: &Path, accepts_response: impl FnOnce(&[u8]) -> bool)
| 247 | |
| 248 | #[cfg(unix)] |
| 249 | fn unix_socket_http_ping(path: &Path, accepts_response: impl FnOnce(&[u8]) -> bool) -> bool { |
| 250 | const PROBE_TIMEOUT: Duration = Duration::from_secs(1); |
| 251 | const PING_REQUEST: &[u8] = |
| 252 | b"GET /_ping HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; |
| 253 | |
| 254 | if !is_unix_socket(path) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | let Ok(mut stream) = std::os::unix::net::UnixStream::connect(path) else { |
| 259 | return false; |
| 260 | }; |
| 261 | if stream.set_read_timeout(Some(PROBE_TIMEOUT)).is_err() |
| 262 | || stream.set_write_timeout(Some(PROBE_TIMEOUT)).is_err() |
| 263 | || stream.write_all(PING_REQUEST).is_err() |
| 264 | { |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | let mut response = [0_u8; 512]; |
| 269 | let mut total = 0; |
| 270 | while total < response.len() { |
| 271 | let Ok(n) = stream.read(&mut response[total..]) else { |
| 272 | return false; |
| 273 | }; |
| 274 | if n == 0 { |
| 275 | break; |
| 276 | } |
| 277 | total += n; |
| 278 | if contains_ascii(&response[..total], b"\r\n\r\n") { |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | total > 0 && accepts_response(&response[..total]) |
| 283 | } |
| 284 | |
| 285 | #[cfg(unix)] |
| 286 | fn http_response_is_success(response: &[u8]) -> bool { |
no test coverage detected