(host: &str, port: u16, path: &str)
| 441 | } |
| 442 | |
| 443 | async fn fetch_devtools_json(host: &str, port: u16, path: &str) -> Result<Value, String> { |
| 444 | let address = format!("{host}:{port}"); |
| 445 | let mut stream = timeout(DEVTOOLS_DISCOVERY_TIMEOUT, TcpStream::connect(&address)) |
| 446 | .await |
| 447 | .map_err(|_| format!("Timed out connecting to DevTools endpoint at {address}."))? |
| 448 | .map_err(|error| format!("Unable to connect to DevTools endpoint at {address}: {error}"))?; |
| 449 | |
| 450 | let request = format!( |
| 451 | "GET {path} HTTP/1.1\r\nHost: {address}\r\nAccept: application/json\r\nConnection: close\r\n\r\n" |
| 452 | ); |
| 453 | timeout( |
| 454 | DEVTOOLS_DISCOVERY_TIMEOUT, |
| 455 | stream.write_all(request.as_bytes()), |
| 456 | ) |
| 457 | .await |
| 458 | .map_err(|_| format!("Timed out requesting DevTools {path}."))? |
| 459 | .map_err(|error| format!("Unable to request DevTools {path}: {error}"))?; |
| 460 | |
| 461 | let mut response = Vec::new(); |
| 462 | let mut chunk = [0_u8; 8192]; |
| 463 | loop { |
| 464 | let count = timeout(DEVTOOLS_DISCOVERY_TIMEOUT, stream.read(&mut chunk)) |
| 465 | .await |
| 466 | .map_err(|_| format!("Timed out reading DevTools {path}."))? |
| 467 | .map_err(|error| format!("Unable to read DevTools {path}: {error}"))?; |
| 468 | if count == 0 { |
| 469 | break; |
| 470 | } |
| 471 | response.extend_from_slice(&chunk[..count]); |
| 472 | if response.len() > DEVTOOLS_MAX_RESPONSE_BYTES { |
| 473 | return Err(format!("DevTools {path} response exceeded the size limit.")); |
| 474 | } |
| 475 | if response_has_complete_body(&response) { |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | let (headers, body) = split_http_response(&response) |
| 481 | .ok_or_else(|| format!("DevTools {path} returned a malformed HTTP response."))?; |
| 482 | let status = headers |
| 483 | .lines() |
| 484 | .next() |
| 485 | .and_then(|line| line.split_whitespace().nth(1)) |
| 486 | .and_then(|status| status.parse::<u16>().ok()) |
| 487 | .unwrap_or(0); |
| 488 | if !(200..300).contains(&status) { |
| 489 | return Err(format!("DevTools {path} returned HTTP {status}.")); |
| 490 | } |
| 491 | |
| 492 | let body = content_length(&headers) |
| 493 | .and_then(|length| body.get(..length)) |
| 494 | .unwrap_or(body); |
| 495 | serde_json::from_slice(body) |
| 496 | .map_err(|error| format!("DevTools {path} returned malformed JSON: {error}")) |
| 497 | } |
| 498 | |
| 499 | async fn connect_loopback_devtools_service( |
| 500 | port: u16, |
no test coverage detected