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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Fetch domain types.
//!
//! The Fetch domain allows intercepting network requests, modifying them,
//! and providing custom responses. It's the primary mechanism for request
//! routing and mocking in browser automation.

use serde::{Deserialize, Serialize};

use super::network::{Request, ResourceType};

/// Unique request identifier for the Fetch domain.
pub type RequestId = String;

/// Response HTTP header entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeaderEntry {
    /// Header name.
    pub name: String,
    /// Header value.
    pub value: String,
}

/// Stage at which to begin intercepting requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum RequestStage {
    /// Intercept before the request is sent.
    #[default]
    Request,
    /// Intercept after the response is received (but before response body is received).
    Response,
}

/// Request pattern for interception.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct RequestPattern {
    /// Wildcards ('*' -> zero or more, '?' -> exactly one) are allowed.
    /// Escape character is backslash. Omitting is equivalent to "*".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url_pattern: Option<String>,

    /// If set, only requests for matching resource types will be intercepted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_type: Option<ResourceType>,

    /// Stage at which to begin intercepting requests. Default is Request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_stage: Option<RequestStage>,
}

impl RequestPattern {
    /// Create a new request pattern matching all URLs.
    pub fn all() -> Self {
        Self::default()
    }

    /// Create a new request pattern matching the specified URL pattern.
    pub fn url(pattern: impl Into<String>) -> Self {
        Self {
            url_pattern: Some(pattern.into()),
            ..Default::default()
        }
    }

    /// Set the resource type filter.
    #[must_use]
    pub fn with_resource_type(mut self, resource_type: ResourceType) -> Self {
        self.resource_type = Some(resource_type);
        self
    }

    /// Set the request stage.
    #[must_use]
    pub fn with_stage(mut self, stage: RequestStage) -> Self {
        self.request_stage = Some(stage);
        self
    }
}

// =============================================================================
// Commands
// =============================================================================

/// Parameters for Fetch.enable.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EnableParams {
    /// If specified, only requests matching any of these patterns will produce
    /// fetchRequested event and will be paused until client's response.
    /// If not set, all requests will be affected.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patterns: Option<Vec<RequestPattern>>,

    /// If true, authRequired events will be issued and requests will be paused
    /// expecting a call to continueWithAuth.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle_auth_requests: Option<bool>,
}

/// Parameters for Fetch.disable.
#[derive(Debug, Clone, Serialize, Default)]
pub struct DisableParams {}

/// Parameters for Fetch.continueRequest.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ContinueRequestParams {
    /// An id the client received in requestPaused event.
    pub request_id: RequestId,

    /// If set, the request url will be modified in a way that's not observable by page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    /// If set, the request method is overridden.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,

    /// If set, overrides the post data in the request.
    /// (Encoded as a base64 string when passed over JSON)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post_data: Option<String>,

    /// If set, overrides the request headers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<Vec<HeaderEntry>>,

    /// If set, overrides response interception behavior for this request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub intercept_response: Option<bool>,
}

/// Parameters for Fetch.fulfillRequest.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FulfillRequestParams {
    /// An id the client received in requestPaused event.
    pub request_id: RequestId,

    /// An HTTP response code.
    pub response_code: i32,

    /// Response headers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_headers: Option<Vec<HeaderEntry>>,

    /// Alternative way of specifying response headers as a \0-separated
    /// series of name: value pairs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub binary_response_headers: Option<String>,

    /// A response body. If absent, original response body will be used if
    /// the request is intercepted at the response stage and empty body
    /// will be used if the request is intercepted at the request stage.
    /// (Encoded as a base64 string when passed over JSON)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,

    /// A textual representation of responseCode.
    /// If absent, a standard phrase matching responseCode is used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_phrase: Option<String>,
}

/// Parameters for Fetch.failRequest.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FailRequestParams {
    /// An id the client received in requestPaused event.
    pub request_id: RequestId,

    /// Causes the request to fail with the given reason.
    pub error_reason: ErrorReason,
}

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

/// Result for Fetch.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 Fetch.continueWithAuth.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueWithAuthParams {
    /// An id the client received in authRequired event.
    pub request_id: RequestId,

    /// Response to with an authChallenge.
    pub auth_challenge_response: AuthChallengeResponse,
}

/// Parameters for Fetch.continueResponse (experimental).
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ContinueResponseParams {
    /// An id the client received in requestPaused event.
    pub request_id: RequestId,

    /// An HTTP response code. If absent, original response code will be used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_code: Option<i32>,

    /// A textual representation of responseCode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_phrase: Option<String>,

    /// Response headers. If absent, original response headers will be used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_headers: Option<Vec<HeaderEntry>>,

    /// Alternative way of specifying response headers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub binary_response_headers: Option<String>,
}

// =============================================================================
// Events
// =============================================================================

/// Event: Fetch.requestPaused
///
/// Issued when the domain is enabled and the request URL matches the
/// specified filter. The request is paused until the client responds
/// with one of continueRequest, failRequest or fulfillRequest.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestPausedEvent {
    /// Each request the page makes will have a unique id.
    pub request_id: RequestId,

    /// The details of the request.
    pub request: Request,

    /// The id of the frame that initiated the request.
    pub frame_id: String,

    /// How the requested resource will be used.
    pub resource_type: ResourceType,

    /// Response error if intercepted at response stage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_error_reason: Option<ErrorReason>,

    /// Response code if intercepted at response stage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_status_code: Option<i32>,

    /// Response status text if intercepted at response stage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_status_text: Option<String>,

    /// Response headers if intercepted at the response stage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_headers: Option<Vec<HeaderEntry>>,

    /// If the intercepted request had a corresponding Network.requestWillBeSent event,
    /// then this networkId will be the same as the requestId in that event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_id: Option<String>,

    /// If the request is due to a redirect response from the server,
    /// the id of the request that has caused the redirect.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redirected_request_id: Option<RequestId>,
}

impl RequestPausedEvent {
    /// Check if this event is at the response stage.
    pub fn is_response_stage(&self) -> bool {
        self.response_error_reason.is_some() || self.response_status_code.is_some()
    }

    /// Check if this event is at the request stage.
    pub fn is_request_stage(&self) -> bool {
        !self.is_response_stage()
    }

    /// Check if this is a redirect response.
    pub fn is_redirect(&self) -> bool {
        if let Some(code) = self.response_status_code {
            matches!(code, 301 | 302 | 303 | 307 | 308)
                && self.response_headers.as_ref().is_some_and(|headers| {
                    headers
                        .iter()
                        .any(|h| h.name.eq_ignore_ascii_case("location"))
                })
        } else {
            false
        }
    }
}

/// Event: Fetch.authRequired
///
/// Issued when the domain is enabled with handleAuthRequests set to true.
/// The request is paused until client responds with continueWithAuth.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthRequiredEvent {
    /// Each request the page makes will have a unique id.
    pub request_id: RequestId,

    /// The details of the request.
    pub request: Request,

    /// The id of the frame that initiated the request.
    pub frame_id: String,

    /// How the requested resource will be used.
    pub resource_type: ResourceType,

    /// Details of the Authorization Challenge encountered.
    pub auth_challenge: AuthChallenge,
}

// =============================================================================
// Types
// =============================================================================

/// Network level fetch failure reason.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ErrorReason {
    /// Generic failure.
    #[default]
    Failed,
    /// Request was aborted.
    Aborted,
    /// Request timed out.
    TimedOut,
    /// Access was denied.
    AccessDenied,
    /// Connection was closed.
    ConnectionClosed,
    /// Connection was reset.
    ConnectionReset,
    /// Connection was refused.
    ConnectionRefused,
    /// Connection was aborted.
    ConnectionAborted,
    /// Connection failed.
    ConnectionFailed,
    /// Name could not be resolved.
    NameNotResolved,
    /// Internet is disconnected.
    InternetDisconnected,
    /// Address is unreachable.
    AddressUnreachable,
    /// Blocked by client.
    BlockedByClient,
    /// Blocked by response.
    BlockedByResponse,
}

impl ErrorReason {
    /// Get the CDP string representation of this error reason.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Failed => "Failed",
            Self::Aborted => "Aborted",
            Self::TimedOut => "TimedOut",
            Self::AccessDenied => "AccessDenied",
            Self::ConnectionClosed => "ConnectionClosed",
            Self::ConnectionReset => "ConnectionReset",
            Self::ConnectionRefused => "ConnectionRefused",
            Self::ConnectionAborted => "ConnectionAborted",
            Self::ConnectionFailed => "ConnectionFailed",
            Self::NameNotResolved => "NameNotResolved",
            Self::InternetDisconnected => "InternetDisconnected",
            Self::AddressUnreachable => "AddressUnreachable",
            Self::BlockedByClient => "BlockedByClient",
            Self::BlockedByResponse => "BlockedByResponse",
        }
    }
}

/// Authorization challenge for HTTP status code 401 or 407.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthChallenge {
    /// Source of the authentication challenge.
    pub source: AuthChallengeSource,

    /// Origin of the challenger.
    pub origin: String,

    /// The authentication scheme used, such as basic or digest.
    pub scheme: String,

    /// The realm of the challenge. May be empty.
    pub realm: String,
}

/// Source of the authentication challenge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthChallengeSource {
    /// Server authentication.
    Server,
    /// Proxy authentication.
    Proxy,
}

/// Response to an `AuthChallenge`.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthChallengeResponse {
    /// The decision on what to do in response to the authorization challenge.
    pub response: AuthChallengeResponseType,

    /// The username to provide, possibly empty.
    /// Should only be set if response is `ProvideCredentials`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,

    /// The password to provide, possibly empty.
    /// Should only be set if response is `ProvideCredentials`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,
}

/// The decision on what to do in response to the authorization challenge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthChallengeResponseType {
    /// Defer to the default behavior of the net stack.
    Default,
    /// Cancel the authentication.
    CancelAuth,
    /// Provide credentials.
    ProvideCredentials,
}

impl AuthChallengeResponse {
    /// Create a default response (defer to browser).
    pub fn default_response() -> Self {
        Self {
            response: AuthChallengeResponseType::Default,
            username: None,
            password: None,
        }
    }

    /// Create a cancel response.
    pub fn cancel() -> Self {
        Self {
            response: AuthChallengeResponseType::CancelAuth,
            username: None,
            password: None,
        }
    }

    /// Create a response providing credentials.
    pub fn provide_credentials(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self {
            response: AuthChallengeResponseType::ProvideCredentials,
            username: Some(username.into()),
            password: Some(password.into()),
        }
    }
}

// Unit tests moved to tests/integration_tests.rs