Skip to main content

chromiumoxide/handler/
http.rs

1use std::time::Instant;
2
3use chromiumoxide_cdp::cdp::browser_protocol::network::{InterceptionId, RequestId, Response};
4use chromiumoxide_cdp::cdp::browser_protocol::page::FrameId;
5
6#[derive(Debug, Clone)]
7pub struct HttpRequest {
8    /// Unique ID of the request.
9    pub request_id: RequestId,
10    /// Indicates if the response came from the memory cache.
11    pub from_memory_cache: bool,
12    /// Reason for failure, if any.
13    pub failure_text: Option<String>,
14    /// ID used for request interception, if applicable.
15    pub interception_id: Option<InterceptionId>,
16    /// Response data associated with the request.
17    pub response: Option<Response>,
18    /// HTTP headers of the request.
19    pub headers: std::collections::HashMap<String, String>,
20    /// ID of the frame that initiated the request.
21    pub frame: Option<FrameId>,
22    /// Whether this is a navigation request.
23    pub is_navigation_request: bool,
24    /// Whether interception is allowed for this request.
25    pub allow_interception: bool,
26    /// Whether the interception has already been handled.
27    pub interception_handled: bool,
28    /// HTTP method (e.g., GET, POST).
29    pub method: Option<String>,
30    /// Request URL.
31    pub url: Option<String>,
32    /// Resource type (e.g., "Document", "Script").
33    pub resource_type: Option<String>,
34    /// Raw post body, if present.
35    pub post_data: Option<String>,
36    /// List of redirect requests leading to this one.
37    pub redirect_chain: Vec<HttpRequest>,
38    /// When this entry was created — used to evict orphaned requests whose
39    /// `loadingFinished` / `loadingFailed` events were never received.
40    pub created_at: Instant,
41}
42
43impl Default for HttpRequest {
44    fn default() -> Self {
45        Self {
46            request_id: Default::default(),
47            from_memory_cache: false,
48            failure_text: None,
49            interception_id: None,
50            response: None,
51            headers: Default::default(),
52            frame: None,
53            is_navigation_request: false,
54            allow_interception: false,
55            interception_handled: false,
56            method: None,
57            url: None,
58            resource_type: None,
59            post_data: None,
60            redirect_chain: Vec::new(),
61            created_at: Instant::now(),
62        }
63    }
64}
65
66impl HttpRequest {
67    /// Creates a new `HttpRequest` with the given request ID and default values.
68    pub fn new(
69        request_id: RequestId,
70        frame: Option<FrameId>,
71        interception_id: Option<InterceptionId>,
72        allow_interception: bool,
73        redirect_chain: Vec<HttpRequest>,
74    ) -> Self {
75        Self {
76            request_id,
77            from_memory_cache: false,
78            failure_text: None,
79            interception_id,
80            response: None,
81            headers: Default::default(),
82            frame,
83            is_navigation_request: false,
84            allow_interception,
85            interception_handled: false,
86            method: None,
87            url: None,
88            resource_type: None,
89            post_data: None,
90            redirect_chain,
91            created_at: Instant::now(),
92        }
93    }
94    /// Returns the request ID.
95    pub fn request_id(&self) -> &RequestId {
96        &self.request_id
97    }
98    /// Sets the response for this request.
99    pub fn set_response(&mut self, response: Response) {
100        self.response = Some(response)
101    }
102}