browser_protocol/fetch/mod.rs
1//! A domain for letting clients substitute browser's network layer with client code.
2use serde::{Serialize, Deserialize};
3use serde_json::Value as JsonValue;
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#[derive(Debug, Clone, Serialize, Deserialize, Default)]
95pub struct DisableParams {}
96
97impl DisableParams { pub const METHOD: &'static str = "Fetch.disable"; }
98
99impl crate::CdpCommand for DisableParams {
100 const METHOD: &'static str = "Fetch.disable";
101 type Response = crate::EmptyReturns;
102}
103
104/// Enables issuing of requestPaused events. A request will be paused until client
105/// calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
106
107#[derive(Debug, Clone, Serialize, Deserialize, Default)]
108#[serde(rename_all = "camelCase")]
109pub struct EnableParams {
110 /// If specified, only requests matching any of these patterns will produce
111 /// fetchRequested event and will be paused until clients response. If not set,
112 /// all requests will be affected.
113
114 #[serde(skip_serializing_if = "Option::is_none")]
115 pub patterns: Option<Vec<RequestPattern>>,
116 /// If true, authRequired events will be issued and requests will be paused
117 /// expecting a call to continueWithAuth.
118
119 #[serde(skip_serializing_if = "Option::is_none")]
120 pub handleAuthRequests: Option<bool>,
121}
122
123impl EnableParams { pub const METHOD: &'static str = "Fetch.enable"; }
124
125impl crate::CdpCommand for EnableParams {
126 const METHOD: &'static str = "Fetch.enable";
127 type Response = crate::EmptyReturns;
128}
129
130/// Causes the request to fail with specified reason.
131
132#[derive(Debug, Clone, Serialize, Deserialize, Default)]
133#[serde(rename_all = "camelCase")]
134pub struct FailRequestParams {
135 /// An id the client received in requestPaused event.
136
137 pub requestId: RequestId,
138 /// Causes the request to fail with the given reason.
139
140 pub errorReason: crate::network::ErrorReason,
141}
142
143impl FailRequestParams { pub const METHOD: &'static str = "Fetch.failRequest"; }
144
145impl crate::CdpCommand for FailRequestParams {
146 const METHOD: &'static str = "Fetch.failRequest";
147 type Response = crate::EmptyReturns;
148}
149
150/// Provides response to the request.
151
152#[derive(Debug, Clone, Serialize, Deserialize, Default)]
153#[serde(rename_all = "camelCase")]
154pub struct FulfillRequestParams {
155 /// An id the client received in requestPaused event.
156
157 pub requestId: RequestId,
158 /// An HTTP response code.
159
160 pub responseCode: i64,
161 /// Response headers.
162
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub responseHeaders: Option<Vec<HeaderEntry>>,
165 /// Alternative way of specifying response headers as a \0-separated
166 /// series of name: value pairs. Prefer the above method unless you
167 /// need to represent some non-UTF8 values that can't be transmitted
168 /// over the protocol as text. (Encoded as a base64 string when passed over JSON)
169
170 #[serde(skip_serializing_if = "Option::is_none")]
171 pub binaryResponseHeaders: Option<String>,
172 /// A response body. If absent, original response body will be used if
173 /// the request is intercepted at the response stage and empty body
174 /// will be used if the request is intercepted at the request stage. (Encoded as a base64 string when passed over JSON)
175
176 #[serde(skip_serializing_if = "Option::is_none")]
177 pub body: Option<String>,
178 /// A textual representation of responseCode.
179 /// If absent, a standard phrase matching responseCode is used.
180
181 #[serde(skip_serializing_if = "Option::is_none")]
182 pub responsePhrase: Option<String>,
183}
184
185impl FulfillRequestParams { pub const METHOD: &'static str = "Fetch.fulfillRequest"; }
186
187impl crate::CdpCommand for FulfillRequestParams {
188 const METHOD: &'static str = "Fetch.fulfillRequest";
189 type Response = crate::EmptyReturns;
190}
191
192/// Continues the request, optionally modifying some of its parameters.
193
194#[derive(Debug, Clone, Serialize, Deserialize, Default)]
195#[serde(rename_all = "camelCase")]
196pub struct ContinueRequestParams {
197 /// An id the client received in requestPaused event.
198
199 pub requestId: RequestId,
200 /// If set, the request url will be modified in a way that's not observable by page.
201
202 #[serde(skip_serializing_if = "Option::is_none")]
203 pub url: Option<String>,
204 /// If set, the request method is overridden.
205
206 #[serde(skip_serializing_if = "Option::is_none")]
207 pub method: Option<String>,
208 /// If set, overrides the post data in the request. (Encoded as a base64 string when passed over JSON)
209
210 #[serde(skip_serializing_if = "Option::is_none")]
211 pub postData: Option<String>,
212 /// If set, overrides the request headers. Note that the overrides do not
213 /// extend to subsequent redirect hops, if a redirect happens. Another override
214 /// may be applied to a different request produced by a redirect.
215
216 #[serde(skip_serializing_if = "Option::is_none")]
217 pub headers: Option<Vec<HeaderEntry>>,
218 /// If set, overrides response interception behavior for this request.
219
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub interceptResponse: Option<bool>,
222}
223
224impl ContinueRequestParams { pub const METHOD: &'static str = "Fetch.continueRequest"; }
225
226impl crate::CdpCommand for ContinueRequestParams {
227 const METHOD: &'static str = "Fetch.continueRequest";
228 type Response = crate::EmptyReturns;
229}
230
231/// Continues a request supplying authChallengeResponse following authRequired event.
232
233#[derive(Debug, Clone, Serialize, Deserialize, Default)]
234#[serde(rename_all = "camelCase")]
235pub struct ContinueWithAuthParams {
236 /// An id the client received in authRequired event.
237
238 pub requestId: RequestId,
239 /// Response to with an authChallenge.
240
241 pub authChallengeResponse: AuthChallengeResponse,
242}
243
244impl ContinueWithAuthParams { pub const METHOD: &'static str = "Fetch.continueWithAuth"; }
245
246impl crate::CdpCommand for ContinueWithAuthParams {
247 const METHOD: &'static str = "Fetch.continueWithAuth";
248 type Response = crate::EmptyReturns;
249}
250
251/// Continues loading of the paused response, optionally modifying the
252/// response headers. If either responseCode or headers are modified, all of them
253/// must be present.
254
255#[derive(Debug, Clone, Serialize, Deserialize, Default)]
256#[serde(rename_all = "camelCase")]
257pub struct ContinueResponseParams {
258 /// An id the client received in requestPaused event.
259
260 pub requestId: RequestId,
261 /// An HTTP response code. If absent, original response code will be used.
262
263 #[serde(skip_serializing_if = "Option::is_none")]
264 pub responseCode: Option<i64>,
265 /// A textual representation of responseCode.
266 /// If absent, a standard phrase matching responseCode is used.
267
268 #[serde(skip_serializing_if = "Option::is_none")]
269 pub responsePhrase: Option<String>,
270 /// Response headers. If absent, original response headers will be used.
271
272 #[serde(skip_serializing_if = "Option::is_none")]
273 pub responseHeaders: Option<Vec<HeaderEntry>>,
274 /// Alternative way of specifying response headers as a \0-separated
275 /// series of name: value pairs. Prefer the above method unless you
276 /// need to represent some non-UTF8 values that can't be transmitted
277 /// over the protocol as text. (Encoded as a base64 string when passed over JSON)
278
279 #[serde(skip_serializing_if = "Option::is_none")]
280 pub binaryResponseHeaders: Option<String>,
281}
282
283impl ContinueResponseParams { pub const METHOD: &'static str = "Fetch.continueResponse"; }
284
285impl crate::CdpCommand for ContinueResponseParams {
286 const METHOD: &'static str = "Fetch.continueResponse";
287 type Response = crate::EmptyReturns;
288}
289
290/// Causes the body of the response to be received from the server and
291/// returned as a single string. May only be issued for a request that
292/// is paused in the Response stage and is mutually exclusive with
293/// takeResponseBodyForInterceptionAsStream. Calling other methods that
294/// affect the request or disabling fetch domain before body is received
295/// results in an undefined behavior.
296/// Note that the response body is not available for redirects. Requests
297/// paused in the _redirect received_ state may be differentiated by
298/// 'responseCode' and presence of 'location' response header, see
299/// comments to 'requestPaused' for details.
300
301#[derive(Debug, Clone, Serialize, Deserialize, Default)]
302#[serde(rename_all = "camelCase")]
303pub struct GetResponseBodyParams {
304 /// Identifier for the intercepted request to get body for.
305
306 pub requestId: RequestId,
307}
308
309/// Causes the body of the response to be received from the server and
310/// returned as a single string. May only be issued for a request that
311/// is paused in the Response stage and is mutually exclusive with
312/// takeResponseBodyForInterceptionAsStream. Calling other methods that
313/// affect the request or disabling fetch domain before body is received
314/// results in an undefined behavior.
315/// Note that the response body is not available for redirects. Requests
316/// paused in the _redirect received_ state may be differentiated by
317/// 'responseCode' and presence of 'location' response header, see
318/// comments to 'requestPaused' for details.
319
320#[derive(Debug, Clone, Serialize, Deserialize, Default)]
321#[serde(rename_all = "camelCase")]
322pub struct GetResponseBodyReturns {
323 /// Response body.
324
325 pub body: String,
326 /// True, if content was sent as base64.
327
328 pub base64Encoded: bool,
329}
330
331impl GetResponseBodyParams { pub const METHOD: &'static str = "Fetch.getResponseBody"; }
332
333impl crate::CdpCommand for GetResponseBodyParams {
334 const METHOD: &'static str = "Fetch.getResponseBody";
335 type Response = GetResponseBodyReturns;
336}
337
338/// Returns a handle to the stream representing the response body.
339/// The request must be paused in the HeadersReceived stage.
340/// Note that after this command the request can't be continued
341/// as is -- client either needs to cancel it or to provide the
342/// response body.
343/// The stream only supports sequential read, IO.read will fail if the position
344/// is specified.
345/// This method is mutually exclusive with getResponseBody.
346/// Calling other methods that affect the request or disabling fetch
347/// domain before body is received results in an undefined behavior.
348
349#[derive(Debug, Clone, Serialize, Deserialize, Default)]
350#[serde(rename_all = "camelCase")]
351pub struct TakeResponseBodyAsStreamParams {
352
353 pub requestId: RequestId,
354}
355
356/// Returns a handle to the stream representing the response body.
357/// The request must be paused in the HeadersReceived stage.
358/// Note that after this command the request can't be continued
359/// as is -- client either needs to cancel it or to provide the
360/// response body.
361/// The stream only supports sequential read, IO.read will fail if the position
362/// is specified.
363/// This method is mutually exclusive with getResponseBody.
364/// Calling other methods that affect the request or disabling fetch
365/// domain before body is received results in an undefined behavior.
366
367#[derive(Debug, Clone, Serialize, Deserialize, Default)]
368#[serde(rename_all = "camelCase")]
369pub struct TakeResponseBodyAsStreamReturns {
370
371 pub stream: crate::io::StreamHandle,
372}
373
374impl TakeResponseBodyAsStreamParams { pub const METHOD: &'static str = "Fetch.takeResponseBodyAsStream"; }
375
376impl crate::CdpCommand for TakeResponseBodyAsStreamParams {
377 const METHOD: &'static str = "Fetch.takeResponseBodyAsStream";
378 type Response = TakeResponseBodyAsStreamReturns;
379}