chrome-cli 1.2.0

A CLI tool for browser automation via the Chrome DevTools Protocol
Documentation
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;

use serde::Deserialize;

use super::ChromeError;
use super::platform;

/// Browser version information returned by `/json/version`.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct BrowserVersion {
    /// The browser name and version (e.g. "Chrome/120.0.6099.71").
    #[serde(rename = "Browser")]
    pub browser: String,

    /// The CDP protocol version (e.g. "1.3").
    #[serde(rename = "Protocol-Version")]
    pub protocol_version: String,

    /// The browser-level WebSocket debugger URL.
    #[serde(rename = "webSocketDebuggerUrl")]
    pub ws_debugger_url: String,
}

/// Information about a single debuggable target (tab, service worker, etc.).
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct TargetInfo {
    /// Unique target identifier.
    pub id: String,

    /// Target type (e.g. "page", "`background_page`").
    #[serde(rename = "type")]
    pub target_type: String,

    /// Page title.
    pub title: String,

    /// Current URL.
    pub url: String,

    /// WebSocket URL to debug this specific target.
    #[serde(rename = "webSocketDebuggerUrl")]
    pub ws_debugger_url: Option<String>,
}

/// Query Chrome's `/json/version` endpoint.
///
/// # Errors
///
/// Returns `ChromeError::HttpError` on connection failure or `ChromeError::ParseError`
/// if the response cannot be deserialized.
pub async fn query_version(host: &str, port: u16) -> Result<BrowserVersion, ChromeError> {
    let body = http_get(host, port, "/json/version").await?;
    serde_json::from_str(&body).map_err(|e| ChromeError::ParseError(e.to_string()))
}

/// Query Chrome's `/json/list` endpoint for debuggable targets.
///
/// # Errors
///
/// Returns `ChromeError::HttpError` on connection failure or `ChromeError::ParseError`
/// if the response cannot be deserialized.
#[allow(dead_code)]
pub async fn query_targets(host: &str, port: u16) -> Result<Vec<TargetInfo>, ChromeError> {
    let body = http_get(host, port, "/json/list").await?;
    serde_json::from_str(&body).map_err(|e| ChromeError::ParseError(e.to_string()))
}

/// Activate a target via Chrome's `/json/activate/{id}` endpoint.
///
/// This uses the same HTTP server as `/json/list`, avoiding the cross-protocol
/// synchronization gap that occurs when using CDP `Target.activateTarget`
/// followed by an HTTP `/json/list` query.
///
/// # Errors
///
/// Returns `ChromeError::HttpError` on connection failure.
pub async fn activate_target(host: &str, port: u16, target_id: &str) -> Result<(), ChromeError> {
    let path = format!("/json/activate/{target_id}");
    let _body = http_get(host, port, &path).await?;
    Ok(())
}

/// Read the `DevToolsActivePort` file from the default user data directory.
///
/// Returns `(port, ws_path)` on success.
///
/// # Errors
///
/// Returns `ChromeError::NoActivePort` if the file is missing or unreadable,
/// or `ChromeError::ParseError` if the contents are malformed.
pub fn read_devtools_active_port() -> Result<(u16, String), ChromeError> {
    let data_dir = platform::default_user_data_dir().ok_or(ChromeError::NoActivePort)?;
    read_devtools_active_port_from(&data_dir)
}

/// Read the `DevToolsActivePort` file from a specific directory.
///
/// This is the parameterized version of [`read_devtools_active_port`] that accepts
/// an explicit data directory, enabling unit testing without relying on
/// platform-specific defaults.
///
/// # Errors
///
/// Returns `ChromeError::NoActivePort` if the file is missing or unreadable,
/// or `ChromeError::ParseError` if the contents are malformed.
pub fn read_devtools_active_port_from(
    data_dir: &std::path::Path,
) -> Result<(u16, String), ChromeError> {
    let path = data_dir.join("DevToolsActivePort");
    let contents = std::fs::read_to_string(&path).map_err(|_| ChromeError::NoActivePort)?;
    parse_devtools_active_port(&contents)
}

/// Parse the contents of a `DevToolsActivePort` file.
///
/// The file has two lines: a port number and a WebSocket path.
fn parse_devtools_active_port(contents: &str) -> Result<(u16, String), ChromeError> {
    let mut lines = contents.lines();
    let port_str = lines.next().ok_or(ChromeError::NoActivePort)?;
    let port: u16 = port_str.trim().parse().map_err(|_| {
        ChromeError::ParseError(format!("invalid port in DevToolsActivePort: {port_str}"))
    })?;
    let ws_path = lines
        .next()
        .ok_or(ChromeError::NoActivePort)?
        .trim()
        .to_string();
    Ok((port, ws_path))
}

/// Attempt to discover a running Chrome instance.
///
/// Tries `DevToolsActivePort` file first, then falls back to the given host/port.
/// Returns the WebSocket URL and port on success.
///
/// # Errors
///
/// Returns `ChromeError::NotRunning` if no Chrome instance can be discovered.
pub async fn discover_chrome(host: &str, port: u16) -> Result<(String, u16), ChromeError> {
    // Try DevToolsActivePort file first
    if let Ok((file_port, _ws_path)) = read_devtools_active_port() {
        if let Ok(version) = query_version("127.0.0.1", file_port).await {
            return Ok((version.ws_debugger_url, file_port));
        }
    }

    // Fall back to the explicitly given host/port
    query_version(host, port)
        .await
        .map(|version| (version.ws_debugger_url, port))
        .map_err(|e| ChromeError::NotRunning(format!("discovery failed on {host}:{port}: {e}")))
}

/// Check whether `buf` contains a complete HTTP response (headers + full body per Content-Length).
fn is_http_response_complete(buf: &[u8]) -> bool {
    let Some(header_end) = find_header_end(buf) else {
        return false;
    };
    let body_start = header_end + 4; // skip past \r\n\r\n
    let headers = &buf[..header_end];
    match parse_content_length(headers) {
        Some(cl) => buf.len() >= body_start + cl,
        None => true, // no Content-Length; headers are complete, assume body is too
    }
}

/// Find the byte offset of `\r\n\r\n` in `buf`, returning the position of the first `\r`.
fn find_header_end(buf: &[u8]) -> Option<usize> {
    buf.windows(4).position(|w| w == b"\r\n\r\n")
}

/// Parse `Content-Length` from raw header bytes (case-insensitive).
fn parse_content_length(headers: &[u8]) -> Option<usize> {
    let header_str = std::str::from_utf8(headers).ok()?;
    for line in header_str.lines() {
        if let Some((key, value)) = line.split_once(':') {
            if key.trim().eq_ignore_ascii_case("content-length") {
                return value.trim().parse().ok();
            }
        }
    }
    None
}

/// Parse a raw HTTP response buffer into the body string.
///
/// Validates the status line is 200 OK and extracts the body after headers.
fn parse_http_response(buf: &[u8]) -> Result<String, ChromeError> {
    let header_end = find_header_end(buf)
        .ok_or_else(|| ChromeError::HttpError("malformed HTTP response".into()))?;
    let body_start = header_end + 4;

    let headers = std::str::from_utf8(&buf[..header_end])
        .map_err(|e| ChromeError::HttpError(format!("invalid UTF-8 in headers: {e}")))?;

    // Check for HTTP 200 status
    let status_line = headers
        .lines()
        .next()
        .ok_or_else(|| ChromeError::HttpError("empty response".into()))?;
    if !status_line.contains(" 200 ") {
        return Err(ChromeError::HttpError(format!(
            "unexpected HTTP status: {status_line}"
        )));
    }

    // Extract body: use Content-Length if available, otherwise take everything after headers
    let body_bytes = if let Some(cl) = parse_content_length(&buf[..header_end]) {
        let end = (body_start + cl).min(buf.len());
        &buf[body_start..end]
    } else {
        &buf[body_start..]
    };

    String::from_utf8(body_bytes.to_vec())
        .map_err(|e| ChromeError::HttpError(format!("invalid UTF-8 in body: {e}")))
}

/// Perform a simple HTTP GET request using blocking I/O in a `spawn_blocking` context.
async fn http_get(host: &str, port: u16, path: &str) -> Result<String, ChromeError> {
    let addr = format!("{host}:{port}");
    let request = format!("GET {path} HTTP/1.1\r\nHost: {addr}\r\nConnection: close\r\n\r\n");

    let (addr_clone, request_clone) = (addr.clone(), request);
    tokio::task::spawn_blocking(move || {
        let mut stream = TcpStream::connect_timeout(
            &addr_clone
                .parse()
                .map_err(|e| ChromeError::HttpError(format!("invalid address: {e}")))?,
            Duration::from_secs(2),
        )
        .map_err(|e| ChromeError::HttpError(format!("connection failed to {addr_clone}: {e}")))?;

        stream.set_read_timeout(Some(Duration::from_secs(5))).ok();

        stream
            .write_all(request_clone.as_bytes())
            .map_err(|e| ChromeError::HttpError(format!("write failed: {e}")))?;

        // Read response incrementally, stopping once we have Content-Length bytes
        // of body. This avoids blocking on EOF when Chrome keeps the connection open.
        let mut buf = Vec::with_capacity(4096);
        let mut tmp = [0u8; 4096];
        loop {
            match stream.read(&mut tmp) {
                Ok(0) => break, // EOF
                Ok(n) => {
                    buf.extend_from_slice(&tmp[..n]);
                    if is_http_response_complete(&buf) {
                        break;
                    }
                }
                Err(e)
                    if e.kind() == std::io::ErrorKind::WouldBlock
                        || e.kind() == std::io::ErrorKind::TimedOut =>
                {
                    // Timeout/EAGAIN: if we already have a complete response, use it
                    if is_http_response_complete(&buf) {
                        break;
                    }
                    return Err(ChromeError::HttpError(format!("read timed out: {e}")));
                }
                Err(e) => {
                    return Err(ChromeError::HttpError(format!("read failed: {e}")));
                }
            }
        }

        parse_http_response(&buf)
    })
    .await
    .map_err(|e| ChromeError::HttpError(format!("task join failed: {e}")))?
}

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

    #[test]
    fn parse_browser_version() {
        let json = r#"{
            "Browser": "Chrome/120.0.6099.71",
            "Protocol-Version": "1.3",
            "User-Agent": "Mozilla/5.0",
            "V8-Version": "12.0.267.8",
            "WebKit-Version": "537.36",
            "webSocketDebuggerUrl": "ws://127.0.0.1:9222/devtools/browser/abc-123"
        }"#;
        let v: BrowserVersion = serde_json::from_str(json).unwrap();
        assert_eq!(v.browser, "Chrome/120.0.6099.71");
        assert_eq!(v.protocol_version, "1.3");
        assert!(v.ws_debugger_url.contains("ws://"));
    }

    #[test]
    fn parse_target_info() {
        let json = r#"[{
            "description": "",
            "devtoolsFrontendUrl": "/devtools/inspector.html",
            "id": "ABCDEF",
            "title": "New Tab",
            "type": "page",
            "url": "chrome://newtab/",
            "webSocketDebuggerUrl": "ws://127.0.0.1:9222/devtools/page/ABCDEF"
        }]"#;
        let targets: Vec<TargetInfo> = serde_json::from_str(json).unwrap();
        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].id, "ABCDEF");
        assert_eq!(targets[0].target_type, "page");
        assert_eq!(targets[0].title, "New Tab");
        assert!(targets[0].ws_debugger_url.is_some());
    }

    #[test]
    fn parse_devtools_active_port_valid() {
        let contents = "9222\n/devtools/browser/abc-123\n";
        let (port, path) = parse_devtools_active_port(contents).unwrap();
        assert_eq!(port, 9222);
        assert_eq!(path, "/devtools/browser/abc-123");
    }

    #[test]
    fn parse_devtools_active_port_empty() {
        let result = parse_devtools_active_port("");
        assert!(result.is_err());
    }

    #[test]
    fn parse_devtools_active_port_invalid_port() {
        let result = parse_devtools_active_port("notaport\n/ws/path\n");
        assert!(result.is_err());
    }

    #[test]
    fn read_devtools_active_port_from_dir() {
        let dir = std::env::temp_dir().join("chrome-cli-test-devtools-port");
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("DevToolsActivePort");
        std::fs::write(&file, "9333\n/devtools/browser/xyz-789\n").unwrap();

        let (port, path) = read_devtools_active_port_from(&dir).unwrap();
        assert_eq!(port, 9333);
        assert_eq!(path, "/devtools/browser/xyz-789");

        // Clean up
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn read_devtools_active_port_from_missing_dir() {
        let dir = std::path::Path::new("/nonexistent/chrome-cli-test");
        let result = read_devtools_active_port_from(dir);
        assert!(result.is_err());
    }

    #[test]
    fn parse_http_response_with_content_length() {
        let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
        let body = parse_http_response(raw).unwrap();
        assert_eq!(body, "Hello, world!");
    }

    #[test]
    fn parse_http_response_without_content_length() {
        let raw = b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n{\"ok\":true}";
        let body = parse_http_response(raw).unwrap();
        assert_eq!(body, "{\"ok\":true}");
    }

    #[test]
    fn parse_http_response_content_length_zero() {
        let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
        let body = parse_http_response(raw).unwrap();
        assert_eq!(body, "");
    }

    #[test]
    fn parse_http_response_malformed_no_separator() {
        let raw = b"HTTP/1.1 200 OK\nno double crlf here";
        let result = parse_http_response(raw);
        assert!(result.is_err());
    }

    #[test]
    fn parse_http_response_non_200_status() {
        let raw = b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
        let result = parse_http_response(raw);
        assert!(result.is_err());
    }

    #[test]
    fn is_http_response_complete_with_content_length() {
        let partial = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHe";
        assert!(!is_http_response_complete(partial));

        let complete = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello";
        assert!(is_http_response_complete(complete));
    }

    #[test]
    fn is_http_response_complete_no_headers_yet() {
        assert!(!is_http_response_complete(b"HTTP/1.1 200 OK\r\n"));
    }

    #[test]
    fn is_http_response_complete_without_content_length() {
        let response = b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nbody";
        assert!(is_http_response_complete(response));
    }
}