MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / unix_socket_http_ping

Function unix_socket_http_ping

crates/openshell-core/src/config.rs:249–283  ·  view source on GitHub ↗
(path: &Path, accepts_response: impl FnOnce(&[u8]) -> bool)

Source from the content-addressed store, hash-verified

247
248#[cfg(unix)]
249fn 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)]
286fn http_response_is_success(response: &[u8]) -> bool {

Callers 1

podman_socket_respondsFunction · 0.85

Calls 5

is_unix_socketFunction · 0.85
connectFunction · 0.85
contains_asciiFunction · 0.85
lenMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected