Skip to main content

browser_protocol/fetch/
mod.rs

1//! A domain for letting clients substitute browser's network layer with client code.
2
3use serde::{Serialize, Deserialize};
4
5/// Unique request identifier.
6/// Note that this does not identify individual HTTP requests that are part of
7/// a network request.
8
9pub type RequestId = String;
10
11/// Stages of the request to handle. Request will intercept before the request is
12/// sent. Response will intercept after the response is received (but before response
13/// body is received).
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
16pub enum RequestStage {
17    #[default]
18    Request,
19    Response,
20}
21
22
23#[derive(Debug, Clone, Serialize, Deserialize, Default)]
24#[serde(rename_all = "camelCase")]
25pub struct RequestPattern {
26    /// Wildcards (''*'' -\> zero or more, ''?'' -\> exactly one) are allowed. Escape character is
27    /// backslash. Omitting is equivalent to '"*"'.
28
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub urlPattern: Option<String>,
31    /// If set, only requests for matching resource types will be intercepted.
32
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub resourceType: Option<crate::network::ResourceType>,
35    /// Stage at which to begin intercepting requests. Default is Request.
36
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub requestStage: Option<RequestStage>,
39}
40
41/// Response HTTP header entry
42
43#[derive(Debug, Clone, Serialize, Deserialize, Default)]
44#[serde(rename_all = "camelCase")]
45pub struct HeaderEntry {
46
47    pub name: String,
48
49    pub value: String,
50}
51
52/// Authorization challenge for HTTP status code 401 or 407.
53
54#[derive(Debug, Clone, Serialize, Deserialize, Default)]
55#[serde(rename_all = "camelCase")]
56pub struct AuthChallenge {
57    /// Source of the authentication challenge.
58
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub source: Option<String>,
61    /// Origin of the challenger.
62
63    pub origin: String,
64    /// The authentication scheme used, such as basic or digest
65
66    pub scheme: String,
67    /// The realm of the challenge. May be empty.
68
69    pub realm: String,
70}
71
72/// Response to an AuthChallenge.
73
74#[derive(Debug, Clone, Serialize, Deserialize, Default)]
75#[serde(rename_all = "camelCase")]
76pub struct AuthChallengeResponse {
77    /// The decision on what to do in response to the authorization challenge.  Default means
78    /// deferring to the default behavior of the net stack, which will likely either the Cancel
79    /// authentication or display a popup dialog box.
80
81    pub response: String,
82    /// The username to provide, possibly empty. Should only be set if response is
83    /// ProvideCredentials.
84
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub username: Option<String>,
87    /// The password to provide, possibly empty. Should only be set if response is
88    /// ProvideCredentials.
89
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub password: Option<String>,
92}
93
94/// Enables issuing of requestPaused events. A request will be paused until client
95/// calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
96
97#[derive(Debug, Clone, Serialize, Deserialize, Default)]
98#[serde(rename_all = "camelCase")]
99pub struct EnableParams {
100    /// If specified, only requests matching any of these patterns will produce
101    /// fetchRequested event and will be paused until clients response. If not set,
102    /// all requests will be affected.
103
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub patterns: Option<Vec<RequestPattern>>,
106    /// If true, authRequired events will be issued and requests will be paused
107    /// expecting a call to continueWithAuth.
108
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub handleAuthRequests: Option<bool>,
111}
112
113/// Causes the request to fail with specified reason.
114
115#[derive(Debug, Clone, Serialize, Deserialize, Default)]
116#[serde(rename_all = "camelCase")]
117pub struct FailRequestParams {
118    /// An id the client received in requestPaused event.
119
120    pub requestId: RequestId,
121    /// Causes the request to fail with the given reason.
122
123    pub errorReason: crate::network::ErrorReason,
124}
125
126/// Provides response to the request.
127
128#[derive(Debug, Clone, Serialize, Deserialize, Default)]
129#[serde(rename_all = "camelCase")]
130pub struct FulfillRequestParams {
131    /// An id the client received in requestPaused event.
132
133    pub requestId: RequestId,
134    /// An HTTP response code.
135
136    pub responseCode: i64,
137    /// Response headers.
138
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub responseHeaders: Option<Vec<HeaderEntry>>,
141    /// Alternative way of specifying response headers as a \0-separated
142    /// series of name: value pairs. Prefer the above method unless you
143    /// need to represent some non-UTF8 values that can't be transmitted
144    /// over the protocol as text. (Encoded as a base64 string when passed over JSON)
145
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub binaryResponseHeaders: Option<String>,
148    /// A response body. If absent, original response body will be used if
149    /// the request is intercepted at the response stage and empty body
150    /// will be used if the request is intercepted at the request stage. (Encoded as a base64 string when passed over JSON)
151
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub body: Option<String>,
154    /// A textual representation of responseCode.
155    /// If absent, a standard phrase matching responseCode is used.
156
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub responsePhrase: Option<String>,
159}
160
161/// Continues the request, optionally modifying some of its parameters.
162
163#[derive(Debug, Clone, Serialize, Deserialize, Default)]
164#[serde(rename_all = "camelCase")]
165pub struct ContinueRequestParams {
166    /// An id the client received in requestPaused event.
167
168    pub requestId: RequestId,
169    /// If set, the request url will be modified in a way that's not observable by page.
170
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub url: Option<String>,
173    /// If set, the request method is overridden.
174
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub method: Option<String>,
177    /// If set, overrides the post data in the request. (Encoded as a base64 string when passed over JSON)
178
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub postData: Option<String>,
181    /// If set, overrides the request headers. Note that the overrides do not
182    /// extend to subsequent redirect hops, if a redirect happens. Another override
183    /// may be applied to a different request produced by a redirect.
184
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub headers: Option<Vec<HeaderEntry>>,
187    /// If set, overrides response interception behavior for this request.
188
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub interceptResponse: Option<bool>,
191}
192
193/// Continues a request supplying authChallengeResponse following authRequired event.
194
195#[derive(Debug, Clone, Serialize, Deserialize, Default)]
196#[serde(rename_all = "camelCase")]
197pub struct ContinueWithAuthParams {
198    /// An id the client received in authRequired event.
199
200    pub requestId: RequestId,
201    /// Response to  with an authChallenge.
202
203    pub authChallengeResponse: AuthChallengeResponse,
204}
205
206/// Continues loading of the paused response, optionally modifying the
207/// response headers. If either responseCode or headers are modified, all of them
208/// must be present.
209
210#[derive(Debug, Clone, Serialize, Deserialize, Default)]
211#[serde(rename_all = "camelCase")]
212pub struct ContinueResponseParams {
213    /// An id the client received in requestPaused event.
214
215    pub requestId: RequestId,
216    /// An HTTP response code. If absent, original response code will be used.
217
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub responseCode: Option<i64>,
220    /// A textual representation of responseCode.
221    /// If absent, a standard phrase matching responseCode is used.
222
223    #[serde(skip_serializing_if = "Option::is_none")]
224    pub responsePhrase: Option<String>,
225    /// Response headers. If absent, original response headers will be used.
226
227    #[serde(skip_serializing_if = "Option::is_none")]
228    pub responseHeaders: Option<Vec<HeaderEntry>>,
229    /// Alternative way of specifying response headers as a \0-separated
230    /// series of name: value pairs. Prefer the above method unless you
231    /// need to represent some non-UTF8 values that can't be transmitted
232    /// over the protocol as text. (Encoded as a base64 string when passed over JSON)
233
234    #[serde(skip_serializing_if = "Option::is_none")]
235    pub binaryResponseHeaders: Option<String>,
236}
237
238/// Causes the body of the response to be received from the server and
239/// returned as a single string. May only be issued for a request that
240/// is paused in the Response stage and is mutually exclusive with
241/// takeResponseBodyForInterceptionAsStream. Calling other methods that
242/// affect the request or disabling fetch domain before body is received
243/// results in an undefined behavior.
244/// Note that the response body is not available for redirects. Requests
245/// paused in the _redirect received_ state may be differentiated by
246/// 'responseCode' and presence of 'location' response header, see
247/// comments to 'requestPaused' for details.
248
249#[derive(Debug, Clone, Serialize, Deserialize, Default)]
250#[serde(rename_all = "camelCase")]
251pub struct GetResponseBodyParams {
252    /// Identifier for the intercepted request to get body for.
253
254    pub requestId: RequestId,
255}
256
257/// Causes the body of the response to be received from the server and
258/// returned as a single string. May only be issued for a request that
259/// is paused in the Response stage and is mutually exclusive with
260/// takeResponseBodyForInterceptionAsStream. Calling other methods that
261/// affect the request or disabling fetch domain before body is received
262/// results in an undefined behavior.
263/// Note that the response body is not available for redirects. Requests
264/// paused in the _redirect received_ state may be differentiated by
265/// 'responseCode' and presence of 'location' response header, see
266/// comments to 'requestPaused' for details.
267
268#[derive(Debug, Clone, Serialize, Deserialize, Default)]
269#[serde(rename_all = "camelCase")]
270pub struct GetResponseBodyReturns {
271    /// Response body.
272
273    pub body: String,
274    /// True, if content was sent as base64.
275
276    pub base64Encoded: bool,
277}
278
279/// Returns a handle to the stream representing the response body.
280/// The request must be paused in the HeadersReceived stage.
281/// Note that after this command the request can't be continued
282/// as is -- client either needs to cancel it or to provide the
283/// response body.
284/// The stream only supports sequential read, IO.read will fail if the position
285/// is specified.
286/// This method is mutually exclusive with getResponseBody.
287/// Calling other methods that affect the request or disabling fetch
288/// domain before body is received results in an undefined behavior.
289
290#[derive(Debug, Clone, Serialize, Deserialize, Default)]
291#[serde(rename_all = "camelCase")]
292pub struct TakeResponseBodyAsStreamParams {
293
294    pub requestId: RequestId,
295}
296
297/// Returns a handle to the stream representing the response body.
298/// The request must be paused in the HeadersReceived stage.
299/// Note that after this command the request can't be continued
300/// as is -- client either needs to cancel it or to provide the
301/// response body.
302/// The stream only supports sequential read, IO.read will fail if the position
303/// is specified.
304/// This method is mutually exclusive with getResponseBody.
305/// Calling other methods that affect the request or disabling fetch
306/// domain before body is received results in an undefined behavior.
307
308#[derive(Debug, Clone, Serialize, Deserialize, Default)]
309#[serde(rename_all = "camelCase")]
310pub struct TakeResponseBodyAsStreamReturns {
311
312    pub stream: crate::io::StreamHandle,
313}