eoka 0.17.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
//! Network Request Capture
//!
//! [`RequestCapture`] (from [`Page::capture_requests`](crate::Page::capture_requests))
//! records one page's HTTP requests and responses.

mod capture;
mod store;

pub use capture::{RequestCapture, RequestCaptureOptions, RequestCaptureStats};
use store::RequestStore;

use std::collections::HashMap;

/// A captured HTTP request with its response. `Debug` omits header values, the
/// request body and URL queries.
#[derive(Clone)]
pub struct CapturedRequest {
    /// CDP request ID (use with `get_response_body`).
    pub request_id: String,
    /// Request URL.
    pub url: String,
    /// HTTP method.
    pub method: String,
    /// Request headers.
    pub headers: HashMap<String, String>,
    /// Request body, if present.
    pub post_data: Option<String>,
    /// Resource type (e.g. "Document", "XHR", "Script").
    pub resource_type: Option<String>,
    /// Response status code, once received.
    pub status: Option<i32>,
    /// Response status text, once received.
    pub status_text: Option<String>,
    /// Response headers, once received.
    pub response_headers: Option<HashMap<String, String>>,
    /// Response MIME type, once received.
    pub mime_type: Option<String>,
    /// Time the request was initiated (CDP monotonic timestamp).
    pub timestamp: f64,
    /// Whether the response has finished loading.
    pub complete: bool,
}

impl std::fmt::Debug for CapturedRequest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CapturedRequest")
            .field("request_id", &self.request_id)
            .field("url", &crate::util::without_query(&self.url))
            .field("method", &self.method)
            .field("header_names", &self.headers.keys())
            .field("post_data_bytes", &self.post_data.as_ref().map(String::len))
            .field("resource_type", &self.resource_type)
            .field("status", &self.status)
            .field("status_text", &self.status_text)
            .field(
                "response_header_names",
                &self.response_headers.as_ref().map(HashMap::keys),
            )
            .field("mime_type", &self.mime_type)
            .field("timestamp", &self.timestamp)
            .field("complete", &self.complete)
            .finish()
    }
}

/// Response body - either text or binary. `Debug` prints only the length.
pub enum ResponseBody {
    /// Textual response body.
    Text(String),
    /// Binary response body.
    Binary(Vec<u8>),
}

impl std::fmt::Debug for ResponseBody {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (kind, bytes) = match self {
            Self::Text(text) => ("Text", text.len()),
            Self::Binary(bytes) => ("Binary", bytes.len()),
        };
        f.debug_struct("ResponseBody")
            .field("kind", &kind)
            .field("bytes", &bytes)
            .finish()
    }
}

impl ResponseBody {
    /// Get as text, or `None` for a binary response.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            ResponseBody::Text(s) => Some(s),
            ResponseBody::Binary(_) => None,
        }
    }

    /// Get as bytes
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            ResponseBody::Text(s) => s.as_bytes(),
            ResponseBody::Binary(b) => b,
        }
    }
}