viewpoint_cdp/protocol/network/
mod.rs

1//! Network domain types.
2//!
3//! The Network domain allows tracking network activities of the page.
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// Unique request identifier.
9pub type RequestId = String;
10
11/// Unique loader identifier.
12pub type LoaderId = String;
13
14/// Unique frame identifier.
15pub type FrameId = String;
16
17/// Monotonically increasing time in seconds since an arbitrary point in the past.
18pub type MonotonicTime = f64;
19
20/// UTC time in seconds, counted from January 1, 1970.
21pub type TimeSinceEpoch = f64;
22
23/// Resource type as it was perceived by the rendering engine.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25#[derive(Default)]
26pub enum ResourceType {
27    /// Document resource.
28    Document,
29    /// Stylesheet resource.
30    Stylesheet,
31    /// Image resource.
32    Image,
33    /// Media resource.
34    Media,
35    /// Font resource.
36    Font,
37    /// Script resource.
38    Script,
39    /// Text track resource.
40    TextTrack,
41    /// `XMLHttpRequest` resource.
42    XHR,
43    /// Fetch API resource.
44    Fetch,
45    /// Prefetch resource.
46    Prefetch,
47    /// `EventSource` resource.
48    EventSource,
49    /// WebSocket resource.
50    WebSocket,
51    /// Manifest resource.
52    Manifest,
53    /// Signed exchange resource.
54    SignedExchange,
55    /// Ping resource.
56    Ping,
57    /// CSP violation report.
58    CSPViolationReport,
59    /// Preflight request.
60    Preflight,
61    /// Other resource type.
62    #[default]
63    Other,
64}
65
66
67/// HTTP request data.
68#[derive(Debug, Clone, Deserialize)]
69#[serde(rename_all = "camelCase")]
70pub struct Request {
71    /// Request URL.
72    pub url: String,
73    /// HTTP request method.
74    pub method: String,
75    /// HTTP request headers.
76    pub headers: HashMap<String, String>,
77    /// HTTP POST request data.
78    pub post_data: Option<String>,
79    /// Whether the request has POST data.
80    pub has_post_data: Option<bool>,
81    /// Request body mixed content type.
82    pub mixed_content_type: Option<String>,
83    /// The referrer policy of the request.
84    pub referrer_policy: Option<String>,
85    /// Whether is loaded via link preload.
86    pub is_link_preload: Option<bool>,
87    /// Priority of the resource request.
88    pub initial_priority: Option<String>,
89}
90
91/// HTTP response data.
92#[derive(Debug, Clone, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct Response {
95    /// Response URL.
96    pub url: String,
97    /// HTTP response status code.
98    pub status: u32,
99    /// HTTP response status text.
100    pub status_text: String,
101    /// HTTP response headers.
102    pub headers: HashMap<String, String>,
103    /// HTTP response headers text.
104    pub headers_text: Option<String>,
105    /// Resource mimeType.
106    pub mime_type: String,
107    /// Refined HTTP request headers that were actually transmitted over the network.
108    pub request_headers: Option<HashMap<String, String>>,
109    /// HTTP request headers text.
110    pub request_headers_text: Option<String>,
111    /// Whether the response was served from disk cache.
112    pub from_disk_cache: Option<bool>,
113    /// Whether the response was served from the prefetch cache.
114    pub from_prefetch_cache: Option<bool>,
115    /// Whether the response was served from `ServiceWorker`.
116    pub from_service_worker: Option<bool>,
117    /// Total number of bytes received.
118    pub encoded_data_length: Option<f64>,
119    /// Protocol for the request.
120    pub protocol: Option<String>,
121    /// Security state.
122    pub security_state: Option<String>,
123    /// Security details for HTTPS responses.
124    pub security_details: Option<SecurityDetails>,
125    /// Remote IP address.
126    #[serde(rename = "remoteIPAddress")]
127    pub remote_ip_address: Option<String>,
128    /// Remote port.
129    pub remote_port: Option<i32>,
130}
131
132/// Parameters for Network.enable.
133#[derive(Debug, Clone, Serialize, Default)]
134#[serde(rename_all = "camelCase")]
135pub struct EnableParams {
136    /// Buffer size in bytes to use for storing network data.
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub max_total_buffer_size: Option<i64>,
139    /// Per-resource buffer size in bytes.
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub max_resource_buffer_size: Option<i64>,
142    /// Max post data size in bytes.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub max_post_data_size: Option<i64>,
145}
146
147/// Event: Network.requestWillBeSent
148#[derive(Debug, Clone, Deserialize)]
149#[serde(rename_all = "camelCase")]
150pub struct RequestWillBeSentEvent {
151    /// Request identifier.
152    pub request_id: RequestId,
153    /// Loader identifier.
154    pub loader_id: LoaderId,
155    /// URL of the document this request is loaded for.
156    #[serde(default)]
157    pub document_url: Option<String>,
158    /// Request data.
159    pub request: Request,
160    /// Timestamp.
161    pub timestamp: f64,
162    /// Timestamp.
163    pub wall_time: f64,
164    /// Request initiator.
165    pub initiator: RequestInitiator,
166    /// Frame identifier.
167    pub frame_id: Option<FrameId>,
168    /// Whether this request is a navigation request.
169    pub has_user_gesture: Option<bool>,
170    /// Type of the request.
171    #[serde(rename = "type")]
172    pub resource_type: Option<String>,
173    /// Redirect response data. Present only if this request was triggered by a redirect.
174    pub redirect_response: Option<Response>,
175}
176
177/// Request initiator information.
178#[derive(Debug, Clone, Deserialize)]
179#[serde(rename_all = "camelCase")]
180pub struct RequestInitiator {
181    /// Type of initiator.
182    #[serde(rename = "type")]
183    pub initiator_type: String,
184    /// Initiator URL.
185    pub url: Option<String>,
186    /// Initiator line number.
187    pub line_number: Option<f64>,
188    /// Initiator column number.
189    pub column_number: Option<f64>,
190}
191
192/// Event: Network.responseReceived
193#[derive(Debug, Clone, Deserialize)]
194#[serde(rename_all = "camelCase")]
195pub struct ResponseReceivedEvent {
196    /// Request identifier.
197    pub request_id: RequestId,
198    /// Loader identifier.
199    pub loader_id: LoaderId,
200    /// Timestamp.
201    pub timestamp: f64,
202    /// Resource type.
203    #[serde(rename = "type")]
204    pub resource_type: String,
205    /// Response data.
206    pub response: Response,
207    /// Frame identifier.
208    pub frame_id: Option<FrameId>,
209}
210
211/// Event: Network.loadingFinished
212#[derive(Debug, Clone, Deserialize)]
213#[serde(rename_all = "camelCase")]
214pub struct LoadingFinishedEvent {
215    /// Request identifier.
216    pub request_id: RequestId,
217    /// Timestamp.
218    pub timestamp: f64,
219    /// Total number of bytes received.
220    pub encoded_data_length: f64,
221}
222
223/// Event: Network.loadingFailed
224#[derive(Debug, Clone, Deserialize)]
225#[serde(rename_all = "camelCase")]
226pub struct LoadingFailedEvent {
227    /// Request identifier.
228    pub request_id: RequestId,
229    /// Timestamp.
230    pub timestamp: f64,
231    /// Resource type.
232    #[serde(rename = "type")]
233    pub resource_type: String,
234    /// User friendly error message.
235    pub error_text: String,
236    /// True if loading was canceled.
237    pub canceled: Option<bool>,
238    /// The reason why loading was blocked.
239    pub blocked_reason: Option<String>,
240}
241
242/// Event: Network.requestServedFromCache
243#[derive(Debug, Clone, Deserialize)]
244#[serde(rename_all = "camelCase")]
245pub struct RequestServedFromCacheEvent {
246    /// Request identifier.
247    pub request_id: RequestId,
248}
249
250/// Timing information for the request.
251#[derive(Debug, Clone, Deserialize)]
252#[serde(rename_all = "camelCase")]
253pub struct ResourceTiming {
254    /// Timing's requestTime is a baseline in seconds.
255    pub request_time: f64,
256    /// Started resolving proxy.
257    pub proxy_start: f64,
258    /// Finished resolving proxy.
259    pub proxy_end: f64,
260    /// Started DNS address resolve.
261    pub dns_start: f64,
262    /// Finished DNS address resolve.
263    pub dns_end: f64,
264    /// Started connecting to the remote host.
265    pub connect_start: f64,
266    /// Connected to the remote host.
267    pub connect_end: f64,
268    /// Started SSL handshake.
269    pub ssl_start: f64,
270    /// Finished SSL handshake.
271    pub ssl_end: f64,
272    /// Started sending request.
273    pub send_start: f64,
274    /// Finished sending request.
275    pub send_end: f64,
276    /// Started receiving response headers.
277    pub receive_headers_start: Option<f64>,
278    /// Finished receiving response headers.
279    pub receive_headers_end: Option<f64>,
280}
281
282/// Security details about a request.
283#[derive(Debug, Clone, Deserialize)]
284#[serde(rename_all = "camelCase")]
285pub struct SecurityDetails {
286    /// Protocol name (e.g. "TLS 1.2" or "QUIC").
287    pub protocol: String,
288    /// Key Exchange used by the connection.
289    pub key_exchange: String,
290    /// (EC)DH group used by the connection, if applicable.
291    pub key_exchange_group: Option<String>,
292    /// Cipher name.
293    pub cipher: String,
294    /// TLS MAC.
295    pub mac: Option<String>,
296    /// Certificate subject name.
297    pub subject_name: String,
298    /// Subject Alternative Name (SAN) DNS names and IP addresses.
299    pub san_list: Vec<String>,
300    /// Name of the issuing CA.
301    pub issuer: String,
302    /// Certificate valid from date.
303    pub valid_from: TimeSinceEpoch,
304    /// Certificate valid to (expiration) date.
305    pub valid_to: TimeSinceEpoch,
306}
307
308/// Parameters for Network.getResponseBody.
309#[derive(Debug, Clone, Serialize)]
310#[serde(rename_all = "camelCase")]
311pub struct GetResponseBodyParams {
312    /// Identifier of the network request to get content for.
313    pub request_id: RequestId,
314}
315
316/// Result for Network.getResponseBody.
317#[derive(Debug, Clone, Deserialize)]
318#[serde(rename_all = "camelCase")]
319pub struct GetResponseBodyResult {
320    /// Response body.
321    pub body: String,
322    /// True, if content was sent as base64.
323    pub base64_encoded: bool,
324}
325
326/// Parameters for Network.setExtraHTTPHeaders.
327#[derive(Debug, Clone, Serialize)]
328#[serde(rename_all = "camelCase")]
329pub struct SetExtraHTTPHeadersParams {
330    /// Map with extra HTTP headers.
331    pub headers: HashMap<String, String>,
332}
333
334/// Parameters for Network.setCacheDisabled.
335#[derive(Debug, Clone, Serialize)]
336#[serde(rename_all = "camelCase")]
337pub struct SetCacheDisabledParams {
338    /// Cache disabled state.
339    pub cache_disabled: bool,
340}
341
342/// Parameters for Network.setBypassServiceWorker.
343#[derive(Debug, Clone, Serialize)]
344#[serde(rename_all = "camelCase")]
345pub struct SetBypassServiceWorkerParams {
346    /// Bypass service worker and load from network.
347    pub bypass: bool,
348}
349
350// =============================================================================
351// Network Conditions
352// =============================================================================
353
354/// Parameters for Network.emulateNetworkConditions.
355#[derive(Debug, Clone, Serialize)]
356#[serde(rename_all = "camelCase")]
357pub struct EmulateNetworkConditionsParams {
358    /// True to emulate internet disconnection.
359    pub offline: bool,
360    /// Minimum latency from request sent to response headers received (ms).
361    pub latency: f64,
362    /// Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
363    pub download_throughput: f64,
364    /// Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
365    pub upload_throughput: f64,
366    /// Connection type if known.
367    #[serde(skip_serializing_if = "Option::is_none")]
368    pub connection_type: Option<ConnectionType>,
369}
370
371impl EmulateNetworkConditionsParams {
372    /// Create params for offline mode.
373    pub fn offline() -> Self {
374        Self {
375            offline: true,
376            latency: 0.0,
377            download_throughput: -1.0,
378            upload_throughput: -1.0,
379            connection_type: None,
380        }
381    }
382
383    /// Create params for online mode (no throttling).
384    pub fn online() -> Self {
385        Self {
386            offline: false,
387            latency: 0.0,
388            download_throughput: -1.0,
389            upload_throughput: -1.0,
390            connection_type: None,
391        }
392    }
393}
394
395/// Connection type.
396#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
397#[serde(rename_all = "lowercase")]
398pub enum ConnectionType {
399    /// No connection.
400    None,
401    /// Cellular 2G.
402    Cellular2g,
403    /// Cellular 3G.
404    Cellular3g,
405    /// Cellular 4G.
406    Cellular4g,
407    /// Bluetooth.
408    Bluetooth,
409    /// Ethernet.
410    Ethernet,
411    /// `WiFi`.
412    Wifi,
413    /// `WiMAX`.
414    Wimax,
415    /// Other.
416    Other,
417}