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