viewpoint-cdp 0.4.3

Low-level Chrome DevTools Protocol implementation over WebSocket
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
//! Network domain types.
//!
//! The Network domain allows tracking network activities of the page.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Unique request identifier.
pub type RequestId = String;

/// Unique loader identifier.
pub type LoaderId = String;

/// Unique frame identifier.
pub type FrameId = String;

/// Monotonically increasing time in seconds since an arbitrary point in the past.
pub type MonotonicTime = f64;

/// UTC time in seconds, counted from January 1, 1970.
pub type TimeSinceEpoch = f64;

/// Resource type as it was perceived by the rendering engine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ResourceType {
    /// Document resource.
    Document,
    /// Stylesheet resource.
    Stylesheet,
    /// Image resource.
    Image,
    /// Media resource.
    Media,
    /// Font resource.
    Font,
    /// Script resource.
    Script,
    /// Text track resource.
    TextTrack,
    /// `XMLHttpRequest` resource.
    XHR,
    /// Fetch API resource.
    Fetch,
    /// Prefetch resource.
    Prefetch,
    /// `EventSource` resource.
    EventSource,
    /// WebSocket resource.
    WebSocket,
    /// Manifest resource.
    Manifest,
    /// Signed exchange resource.
    SignedExchange,
    /// Ping resource.
    Ping,
    /// CSP violation report.
    CSPViolationReport,
    /// Preflight request.
    Preflight,
    /// Other resource type.
    #[default]
    Other,
}

/// HTTP request data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
    /// Request URL.
    pub url: String,
    /// HTTP request method.
    pub method: String,
    /// HTTP request headers.
    pub headers: HashMap<String, String>,
    /// HTTP POST request data.
    pub post_data: Option<String>,
    /// Whether the request has POST data.
    pub has_post_data: Option<bool>,
    /// Request body mixed content type.
    pub mixed_content_type: Option<String>,
    /// The referrer policy of the request.
    pub referrer_policy: Option<String>,
    /// Whether is loaded via link preload.
    pub is_link_preload: Option<bool>,
    /// Priority of the resource request.
    pub initial_priority: Option<String>,
}

/// HTTP response data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
    /// Response URL.
    pub url: String,
    /// HTTP response status code.
    pub status: u32,
    /// HTTP response status text.
    pub status_text: String,
    /// HTTP response headers.
    pub headers: HashMap<String, String>,
    /// HTTP response headers text.
    pub headers_text: Option<String>,
    /// Resource mimeType.
    pub mime_type: String,
    /// Refined HTTP request headers that were actually transmitted over the network.
    pub request_headers: Option<HashMap<String, String>>,
    /// HTTP request headers text.
    pub request_headers_text: Option<String>,
    /// Whether the response was served from disk cache.
    pub from_disk_cache: Option<bool>,
    /// Whether the response was served from the prefetch cache.
    pub from_prefetch_cache: Option<bool>,
    /// Whether the response was served from `ServiceWorker`.
    pub from_service_worker: Option<bool>,
    /// Total number of bytes received.
    pub encoded_data_length: Option<f64>,
    /// Protocol for the request.
    pub protocol: Option<String>,
    /// Security state.
    pub security_state: Option<String>,
    /// Security details for HTTPS responses.
    pub security_details: Option<SecurityDetails>,
    /// Remote IP address.
    #[serde(rename = "remoteIPAddress")]
    pub remote_ip_address: Option<String>,
    /// Remote port.
    pub remote_port: Option<i32>,
}

/// Parameters for Network.enable.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EnableParams {
    /// Buffer size in bytes to use for storing network data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_total_buffer_size: Option<i64>,
    /// Per-resource buffer size in bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_resource_buffer_size: Option<i64>,
    /// Max post data size in bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_post_data_size: Option<i64>,
}

/// Event: Network.requestWillBeSent
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestWillBeSentEvent {
    /// Request identifier.
    pub request_id: RequestId,
    /// Loader identifier.
    pub loader_id: LoaderId,
    /// URL of the document this request is loaded for.
    #[serde(default)]
    pub document_url: Option<String>,
    /// Request data.
    pub request: Request,
    /// Timestamp.
    pub timestamp: f64,
    /// Timestamp.
    pub wall_time: f64,
    /// Request initiator.
    pub initiator: RequestInitiator,
    /// Frame identifier.
    pub frame_id: Option<FrameId>,
    /// Whether this request is a navigation request.
    pub has_user_gesture: Option<bool>,
    /// Type of the request.
    #[serde(rename = "type")]
    pub resource_type: Option<String>,
    /// Redirect response data. Present only if this request was triggered by a redirect.
    pub redirect_response: Option<Response>,
}

/// Request initiator information.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestInitiator {
    /// Type of initiator.
    #[serde(rename = "type")]
    pub initiator_type: String,
    /// Initiator URL.
    pub url: Option<String>,
    /// Initiator line number.
    pub line_number: Option<f64>,
    /// Initiator column number.
    pub column_number: Option<f64>,
}

/// Event: Network.responseReceived
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponseReceivedEvent {
    /// Request identifier.
    pub request_id: RequestId,
    /// Loader identifier.
    pub loader_id: LoaderId,
    /// Timestamp.
    pub timestamp: f64,
    /// Resource type.
    #[serde(rename = "type")]
    pub resource_type: String,
    /// Response data.
    pub response: Response,
    /// Frame identifier.
    pub frame_id: Option<FrameId>,
}

/// Event: Network.loadingFinished
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoadingFinishedEvent {
    /// Request identifier.
    pub request_id: RequestId,
    /// Timestamp.
    pub timestamp: f64,
    /// Total number of bytes received.
    pub encoded_data_length: f64,
}

/// Event: Network.loadingFailed
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoadingFailedEvent {
    /// Request identifier.
    pub request_id: RequestId,
    /// Timestamp.
    pub timestamp: f64,
    /// Resource type.
    #[serde(rename = "type")]
    pub resource_type: String,
    /// User friendly error message.
    pub error_text: String,
    /// True if loading was canceled.
    pub canceled: Option<bool>,
    /// The reason why loading was blocked.
    pub blocked_reason: Option<String>,
}

/// Event: Network.requestServedFromCache
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestServedFromCacheEvent {
    /// Request identifier.
    pub request_id: RequestId,
}

/// Timing information for the request.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceTiming {
    /// Timing's requestTime is a baseline in seconds.
    pub request_time: f64,
    /// Started resolving proxy.
    pub proxy_start: f64,
    /// Finished resolving proxy.
    pub proxy_end: f64,
    /// Started DNS address resolve.
    pub dns_start: f64,
    /// Finished DNS address resolve.
    pub dns_end: f64,
    /// Started connecting to the remote host.
    pub connect_start: f64,
    /// Connected to the remote host.
    pub connect_end: f64,
    /// Started SSL handshake.
    pub ssl_start: f64,
    /// Finished SSL handshake.
    pub ssl_end: f64,
    /// Started sending request.
    pub send_start: f64,
    /// Finished sending request.
    pub send_end: f64,
    /// Started receiving response headers.
    pub receive_headers_start: Option<f64>,
    /// Finished receiving response headers.
    pub receive_headers_end: Option<f64>,
}

/// Security details about a request.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SecurityDetails {
    /// Protocol name (e.g. "TLS 1.2" or "QUIC").
    pub protocol: String,
    /// Key Exchange used by the connection.
    pub key_exchange: String,
    /// (EC)DH group used by the connection, if applicable.
    pub key_exchange_group: Option<String>,
    /// Cipher name.
    pub cipher: String,
    /// TLS MAC.
    pub mac: Option<String>,
    /// Certificate subject name.
    pub subject_name: String,
    /// Subject Alternative Name (SAN) DNS names and IP addresses.
    pub san_list: Vec<String>,
    /// Name of the issuing CA.
    pub issuer: String,
    /// Certificate valid from date.
    pub valid_from: TimeSinceEpoch,
    /// Certificate valid to (expiration) date.
    pub valid_to: TimeSinceEpoch,
}

/// Parameters for Network.getResponseBody.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetResponseBodyParams {
    /// Identifier of the network request to get content for.
    pub request_id: RequestId,
}

/// Result for Network.getResponseBody.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetResponseBodyResult {
    /// Response body.
    pub body: String,
    /// True, if content was sent as base64.
    pub base64_encoded: bool,
}

/// Parameters for Network.setExtraHTTPHeaders.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetExtraHTTPHeadersParams {
    /// Map with extra HTTP headers.
    pub headers: HashMap<String, String>,
}

/// Parameters for Network.setCacheDisabled.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetCacheDisabledParams {
    /// Cache disabled state.
    pub cache_disabled: bool,
}

/// Parameters for Network.setBypassServiceWorker.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetBypassServiceWorkerParams {
    /// Bypass service worker and load from network.
    pub bypass: bool,
}

// =============================================================================
// Network Conditions
// =============================================================================

/// Parameters for Network.emulateNetworkConditions.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EmulateNetworkConditionsParams {
    /// True to emulate internet disconnection.
    pub offline: bool,
    /// Minimum latency from request sent to response headers received (ms).
    pub latency: f64,
    /// Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
    pub download_throughput: f64,
    /// Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
    pub upload_throughput: f64,
    /// Connection type if known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connection_type: Option<ConnectionType>,
}

impl EmulateNetworkConditionsParams {
    /// Create params for offline mode.
    pub fn offline() -> Self {
        Self {
            offline: true,
            latency: 0.0,
            download_throughput: -1.0,
            upload_throughput: -1.0,
            connection_type: None,
        }
    }

    /// Create params for online mode (no throttling).
    pub fn online() -> Self {
        Self {
            offline: false,
            latency: 0.0,
            download_throughput: -1.0,
            upload_throughput: -1.0,
            connection_type: None,
        }
    }
}

/// Connection type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ConnectionType {
    /// No connection.
    None,
    /// Cellular 2G.
    Cellular2g,
    /// Cellular 3G.
    Cellular3g,
    /// Cellular 4G.
    Cellular4g,
    /// Bluetooth.
    Bluetooth,
    /// Ethernet.
    Ethernet,
    /// `WiFi`.
    Wifi,
    /// `WiMAX`.
    Wimax,
    /// Other.
    Other,
}