chromiumoxide/handler/
network.rs

1use super::blockers::{
2    block_websites::block_xhr,
3    ignore_script_embedded, ignore_script_xhr, ignore_script_xhr_media,
4    intercept_manager::NetworkInterceptManager,
5    scripts::{
6        URL_IGNORE_SCRIPT_BASE_PATHS, URL_IGNORE_SCRIPT_STYLES_PATHS, URL_IGNORE_TRIE,
7        URL_IGNORE_TRIE_PATHS,
8    },
9    xhr::IGNORE_XHR_ASSETS,
10};
11use crate::auth::Credentials;
12use crate::cmd::CommandChain;
13use crate::handler::http::HttpRequest;
14use aho_corasick::AhoCorasick;
15use case_insensitive_string::CaseInsensitiveString;
16use chromiumoxide_cdp::cdp::browser_protocol::network::{
17    EmulateNetworkConditionsParams, EventLoadingFailed, EventLoadingFinished,
18    EventRequestServedFromCache, EventRequestWillBeSent, EventResponseReceived, Headers,
19    InterceptionId, RequestId, ResourceType, Response, SetCacheDisabledParams,
20    SetExtraHttpHeadersParams,
21};
22use chromiumoxide_cdp::cdp::browser_protocol::{
23    fetch::{
24        self, AuthChallengeResponse, AuthChallengeResponseResponse, ContinueRequestParams,
25        ContinueWithAuthParams, DisableParams, EventAuthRequired, EventRequestPaused,
26        RequestPattern,
27    },
28    network::SetBypassServiceWorkerParams,
29};
30use chromiumoxide_cdp::cdp::browser_protocol::{
31    network::EnableParams, security::SetIgnoreCertificateErrorsParams,
32};
33use chromiumoxide_types::{Command, Method, MethodId};
34use hashbrown::{HashMap, HashSet};
35use lazy_static::lazy_static;
36use reqwest::header::PROXY_AUTHORIZATION;
37use std::collections::VecDeque;
38use std::time::Duration;
39
40lazy_static! {
41    /// General patterns for popular libraries and resources
42    static ref JS_FRAMEWORK_ALLOW: Vec<&'static str> = vec![
43        "jquery",           // Covers jquery.min.js, jquery.js, etc.
44        "angular",
45        "react",            // Covers all React-related patterns
46        "vue",              // Covers all Vue-related patterns
47        "bootstrap",
48        "d3",
49        "lodash",
50        "ajax",
51        "application",
52        "app",              // Covers general app scripts like app.js
53        "main",
54        "index",
55        "bundle",
56        "vendor",
57        "runtime",
58        "polyfill",
59        "scripts",
60        "es2015.",
61        "es2020.",
62        "webpack",
63        "/wp-content/js/",  // Covers Wordpress content
64        // Verified 3rd parties for request
65        "https://m.stripe.network/",
66        "https://challenges.cloudflare.com/",
67        "https://www.google.com/recaptcha/api.js",
68        "https://google.com/recaptcha/api.js",
69        "https://js.stripe.com/",
70        "https://cdn.prod.website-files.com/", // webflow cdn scripts
71        "https://cdnjs.cloudflare.com/",        // cloudflare cdn scripts
72        "https://code.jquery.com/jquery-"
73    ];
74
75    /// Determine if a script should be rendered in the browser by name.
76    pub static ref ALLOWED_MATCHER: AhoCorasick = AhoCorasick::new(JS_FRAMEWORK_ALLOW.iter()).unwrap();
77
78    /// path of a js framework
79    pub static ref JS_FRAMEWORK_PATH: phf::Set<&'static str> = {
80        phf::phf_set! {
81            // Add allowed assets from JS_FRAMEWORK_ASSETS except the excluded ones
82            "_next/static/", "_astro/", "_app/immutable"
83        }
84    };
85
86    /// Ignore the content types.
87    pub static ref IGNORE_CONTENT_TYPES: phf::Set<&'static str> = phf::phf_set! {
88        "application/pdf",
89        "application/zip",
90        "application/x-rar-compressed",
91        "application/x-tar",
92        "image/png",
93        "image/jpeg",
94        "image/gif",
95        "image/bmp",
96        "image/svg+xml",
97        "video/mp4",
98        "video/x-msvideo",
99        "video/x-matroska",
100        "video/webm",
101        "audio/mpeg",
102        "audio/ogg",
103        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
104        "application/vnd.ms-excel",
105        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
106        "application/vnd.ms-powerpoint",
107        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
108        "application/x-7z-compressed",
109        "application/x-rpm",
110        "application/x-shockwave-flash",
111    };
112
113    /// Ignore the resources for visual content types.
114    pub static ref IGNORE_VISUAL_RESOURCE_MAP: phf::Set<&'static str> = phf::phf_set! {
115        "Image",
116        "Media",
117        "Font"
118    };
119
120    /// Ignore the resources for visual content types.
121    pub static ref IGNORE_NETWORKING_RESOURCE_MAP: phf::Set<&'static str> = phf::phf_set! {
122        "CspViolationReport",
123        "Manifest",
124        "Other",
125        "Prefetch",
126        "Ping",
127    };
128
129    /// Case insenstive css matching
130    pub static ref CSS_EXTENSION: CaseInsensitiveString = CaseInsensitiveString::from("css");
131
132
133    /// The command chain.
134    pub static ref INIT_CHAIN:Vec<(std::borrow::Cow<'static, str>, serde_json::Value)>  = {
135        let enable = EnableParams::default();
136
137        if let Ok(c) = serde_json::to_value(&enable) {
138            vec![(enable.identifier(), c)]
139        } else {
140            vec![]
141        }
142    };
143
144    /// The command chain with https ignore.
145    pub static ref INIT_CHAIN_IGNORE_HTTP_ERRORS:Vec<(std::borrow::Cow<'static, str>, serde_json::Value)>  = {
146        let enable = EnableParams::default();
147        let mut v = vec![];
148        if let Ok(c) = serde_json::to_value(&enable) {
149            v.push((enable.identifier(), c));
150        }
151        let ignore = SetIgnoreCertificateErrorsParams::new(true);
152        if let Ok(ignored) = serde_json::to_value(&ignore) {
153            v.push((ignore.identifier(), ignored));
154        }
155
156        v
157    };
158
159    /// Enable the fetch intercept command
160    pub static ref ENABLE_FETCH: chromiumoxide_cdp::cdp::browser_protocol::fetch::EnableParams = {
161        fetch::EnableParams::builder()
162        .handle_auth_requests(true)
163        .pattern(RequestPattern::builder().url_pattern("*").build())
164        .build()
165    };
166}
167
168#[derive(Debug)]
169pub struct NetworkManager {
170    queued_events: VecDeque<NetworkEvent>,
171    ignore_httpserrors: bool,
172    requests: HashMap<RequestId, HttpRequest>,
173    // TODO put event in an Arc?
174    requests_will_be_sent: HashMap<RequestId, EventRequestWillBeSent>,
175    extra_headers: std::collections::HashMap<String, String>,
176    request_id_to_interception_id: HashMap<RequestId, InterceptionId>,
177    user_cache_disabled: bool,
178    attempted_authentications: HashSet<RequestId>,
179    credentials: Option<Credentials>,
180    // unused atm for remote connections, needs to be used for self launches.
181    user_request_interception_enabled: bool,
182    protocol_request_interception_enabled: bool,
183    offline: bool,
184    request_timeout: Duration,
185    // made_request: bool,
186    /// Ignore visuals (no pings, prefetching, and etc).
187    pub ignore_visuals: bool,
188    /// Block CSS stylesheets.
189    pub block_stylesheets: bool,
190    /// Block javascript that is not critical to rendering.
191    pub block_javascript: bool,
192    /// Block analytics from rendering
193    pub block_analytics: bool,
194    /// Only html from loading.
195    pub only_html: bool,
196    /// The custom intercept handle logic to run on the website.
197    pub intercept_manager: NetworkInterceptManager,
198    /// Track the amount of times the document reloaded.
199    pub document_reload_tracker: u8,
200    /// The initial target domain.
201    pub document_target_domain: String,
202}
203
204impl NetworkManager {
205    pub fn new(ignore_httpserrors: bool, request_timeout: Duration) -> Self {
206        Self {
207            queued_events: Default::default(),
208            ignore_httpserrors,
209            requests: Default::default(),
210            requests_will_be_sent: Default::default(),
211            extra_headers: Default::default(),
212            request_id_to_interception_id: Default::default(),
213            user_cache_disabled: false,
214            attempted_authentications: Default::default(),
215            credentials: None,
216            user_request_interception_enabled: false,
217            protocol_request_interception_enabled: false,
218            offline: false,
219            request_timeout,
220            ignore_visuals: false,
221            block_javascript: false,
222            block_stylesheets: false,
223            block_analytics: true,
224            only_html: false,
225            intercept_manager: NetworkInterceptManager::Unknown,
226            document_reload_tracker: 0,
227            document_target_domain: String::new(),
228        }
229    }
230
231    pub fn init_commands(&self) -> CommandChain {
232        let cmds = if self.ignore_httpserrors {
233            INIT_CHAIN_IGNORE_HTTP_ERRORS.clone()
234        } else {
235            INIT_CHAIN.clone()
236        };
237
238        CommandChain::new(cmds, self.request_timeout)
239    }
240
241    fn push_cdp_request<T: Command>(&mut self, cmd: T) {
242        let method = cmd.identifier();
243        if let Ok(params) = serde_json::to_value(cmd) {
244            self.queued_events
245                .push_back(NetworkEvent::SendCdpRequest((method, params)));
246        }
247    }
248
249    /// The next event to handle
250    pub fn poll(&mut self) -> Option<NetworkEvent> {
251        self.queued_events.pop_front()
252    }
253
254    pub fn extra_headers(&self) -> &std::collections::HashMap<String, String> {
255        &self.extra_headers
256    }
257
258    pub fn set_extra_headers(&mut self, headers: std::collections::HashMap<String, String>) {
259        self.extra_headers = headers;
260        self.extra_headers.remove(PROXY_AUTHORIZATION.as_str());
261        if let Ok(headers) = serde_json::to_value(&self.extra_headers) {
262            self.push_cdp_request(SetExtraHttpHeadersParams::new(Headers::new(headers)));
263        }
264    }
265
266    pub fn set_service_worker_enabled(&mut self, bypass: bool) {
267        self.push_cdp_request(SetBypassServiceWorkerParams::new(bypass));
268    }
269
270    pub fn set_request_interception(&mut self, enabled: bool) {
271        self.user_request_interception_enabled = enabled;
272        self.update_protocol_request_interception();
273    }
274
275    pub fn set_cache_enabled(&mut self, enabled: bool) {
276        self.user_cache_disabled = !enabled;
277        self.update_protocol_cache_disabled();
278    }
279
280    pub fn update_protocol_cache_disabled(&mut self) {
281        self.push_cdp_request(SetCacheDisabledParams::new(
282            self.user_cache_disabled || self.protocol_request_interception_enabled,
283        ));
284    }
285
286    pub fn authenticate(&mut self, credentials: Credentials) {
287        self.credentials = Some(credentials);
288        self.update_protocol_request_interception()
289    }
290
291    fn update_protocol_request_interception(&mut self) {
292        let enabled = self.user_request_interception_enabled || self.credentials.is_some();
293
294        if enabled == self.protocol_request_interception_enabled {
295            return;
296        }
297
298        self.update_protocol_cache_disabled();
299
300        if enabled {
301            self.push_cdp_request(ENABLE_FETCH.clone())
302        } else {
303            self.push_cdp_request(DisableParams::default())
304        }
305    }
306
307    /// Url matches analytics that we want to ignore or trackers.
308    pub(crate) fn ignore_script(
309        &self,
310        url: &str,
311        block_analytics: bool,
312        intercept_manager: NetworkInterceptManager,
313    ) -> bool {
314        let mut ignore_script = block_analytics && URL_IGNORE_TRIE.contains_prefix(url);
315
316        if !ignore_script {
317            if let Some(index) = url.find("//") {
318                let pos = index + 2;
319
320                // Ensure there is something after `//`
321                if pos < url.len() {
322                    // Find the first slash after the `//`
323                    if let Some(slash_index) = url[pos..].find('/') {
324                        let base_path_index = pos + slash_index + 1;
325
326                        if url.len() > base_path_index {
327                            let new_url: &str = &url[base_path_index..];
328
329                            // ignore assets we do not need for frameworks
330                            if !ignore_script
331                                && intercept_manager == NetworkInterceptManager::Unknown
332                            {
333                                let hydration_file =
334                                    JS_FRAMEWORK_PATH.iter().any(|p| new_url.starts_with(p));
335
336                                // ignore astro paths
337                                if hydration_file && new_url.ends_with(".js") {
338                                    ignore_script = true;
339                                }
340                            }
341
342                            if !ignore_script
343                                && URL_IGNORE_SCRIPT_BASE_PATHS.contains_prefix(new_url)
344                            {
345                                ignore_script = true;
346                            }
347
348                            if !ignore_script
349                                && self.ignore_visuals
350                                && URL_IGNORE_SCRIPT_STYLES_PATHS.contains_prefix(new_url)
351                            {
352                                ignore_script = true;
353                            }
354                        }
355                    }
356                }
357            }
358        }
359
360        // fallback for file ending in analytics.js
361        if !ignore_script && block_analytics {
362            ignore_script = URL_IGNORE_TRIE_PATHS.contains_prefix(url);
363        }
364
365        ignore_script
366    }
367
368    /// Determine if the request should be skipped.
369    fn skip_xhr(
370        &self,
371        skip_networking: bool,
372        event: &EventRequestPaused,
373        network_event: bool,
374    ) -> bool {
375        // XHR check
376        if !skip_networking && network_event {
377            let request_url = event.request.url.as_str();
378
379            // check if part of ignore scripts.
380            let skip_analytics =
381                self.block_analytics && (ignore_script_xhr(request_url) || block_xhr(request_url));
382
383            if skip_analytics {
384                true
385            } else if self.block_stylesheets || self.ignore_visuals {
386                let block_css = self.block_stylesheets;
387                let block_media = self.ignore_visuals;
388
389                let mut block_request = false;
390
391                if let Some(position) = request_url.rfind('.') {
392                    let hlen = request_url.len();
393                    let has_asset = hlen - position;
394
395                    if has_asset >= 3 {
396                        let next_position = position + 1;
397
398                        if block_media
399                            && IGNORE_XHR_ASSETS.contains::<CaseInsensitiveString>(
400                                &request_url[next_position..].into(),
401                            )
402                        {
403                            block_request = true;
404                        } else if block_css {
405                            block_request =
406                                CaseInsensitiveString::from(request_url[next_position..].as_bytes())
407                                    .contains(&**CSS_EXTENSION)
408                        }
409                    }
410                }
411
412                if !block_request {
413                    block_request = ignore_script_xhr_media(request_url);
414                }
415
416                block_request
417            } else {
418                skip_networking
419            }
420        } else {
421            skip_networking
422        }
423    }
424
425    #[cfg(not(feature = "adblock"))]
426    pub fn on_fetch_request_paused(&mut self, event: &EventRequestPaused) {
427        use super::blockers::block_websites::block_website;
428
429        if !self.user_request_interception_enabled && self.protocol_request_interception_enabled {
430            self.push_cdp_request(ContinueRequestParams::new(event.request_id.clone()))
431        } else {
432            if let Some(network_id) = event.network_id.as_ref() {
433                if let Some(request_will_be_sent) =
434                    self.requests_will_be_sent.remove(network_id.as_ref())
435                {
436                    self.on_request(&request_will_be_sent, Some(event.request_id.clone().into()));
437                } else {
438                    let current_url = event.request.url.as_str();
439                    let javascript_resource = event.resource_type == ResourceType::Script;
440                    let document_resource = event.resource_type == ResourceType::Document;
441                    let network_resource = !document_resource
442                        && (event.resource_type == ResourceType::Xhr
443                            || event.resource_type == ResourceType::Fetch
444                            || event.resource_type == ResourceType::WebSocket);
445
446                    let skip_networking =
447                        IGNORE_NETWORKING_RESOURCE_MAP.contains(event.resource_type.as_ref());
448
449                    let skip_networking = skip_networking || self.document_reload_tracker >= 3;
450
451                    if document_resource {
452                        if self.document_target_domain == current_url {
453                            // this will prevent the domain from looping (3 times is enough).
454                            self.document_reload_tracker += 1;
455                        }
456                        self.document_target_domain = event.request.url.clone();
457                    }
458
459                    // main initial check
460                    let skip_networking = if !skip_networking {
461                        self.ignore_visuals
462                            && (IGNORE_VISUAL_RESOURCE_MAP.contains(event.resource_type.as_ref()))
463                            || self.block_stylesheets
464                                && ResourceType::Stylesheet == event.resource_type
465                            || self.block_javascript
466                                && javascript_resource
467                                && self.intercept_manager == NetworkInterceptManager::Unknown
468                                && !ALLOWED_MATCHER.is_match(current_url)
469                    } else {
470                        skip_networking
471                    };
472
473                    let skip_networking = if !skip_networking
474                        && (self.only_html || self.ignore_visuals)
475                        && (javascript_resource || document_resource)
476                    {
477                        ignore_script_embedded(current_url)
478                    } else {
479                        skip_networking
480                    };
481
482                    // analytics check
483                    let skip_networking = if !skip_networking && javascript_resource {
484                        self.ignore_script(
485                            current_url,
486                            self.block_analytics,
487                            self.intercept_manager,
488                        )
489                    } else {
490                        skip_networking
491                    };
492
493                    // XHR check
494                    let skip_networking = self.skip_xhr(skip_networking, &event, network_resource);
495
496                    // custom interception layer.
497                    let skip_networking = if !skip_networking
498                        && (javascript_resource || network_resource || document_resource)
499                    {
500                        self.intercept_manager.intercept_detection(
501                            &event.request.url,
502                            self.ignore_visuals,
503                            network_resource,
504                        )
505                    } else {
506                        skip_networking
507                    };
508
509                    let skip_networking =
510                        if !skip_networking && (javascript_resource || network_resource) {
511                            block_website(&event.request.url)
512                        } else {
513                            skip_networking
514                        };
515
516                    if skip_networking {
517                        tracing::debug!(
518                            "Blocked: {:?} - {}",
519                            event.resource_type,
520                            event.request.url
521                        );
522                        let fullfill_params =
523                            crate::handler::network::fetch::FulfillRequestParams::new(
524                                event.request_id.clone(),
525                                200,
526                            );
527                        self.push_cdp_request(fullfill_params);
528                    } else {
529                        tracing::debug!(
530                            "Allowed: {:?} - {}",
531                            event.resource_type,
532                            event.request.url
533                        );
534
535                        self.push_cdp_request(ContinueRequestParams::new(event.request_id.clone()))
536                    }
537                }
538            } else {
539                self.push_cdp_request(ContinueRequestParams::new(event.request_id.clone()))
540            }
541        }
542    }
543
544    #[cfg(feature = "adblock")]
545    pub fn on_fetch_request_paused(&mut self, event: &EventRequestPaused) {
546        if !self.user_request_interception_enabled && self.protocol_request_interception_enabled {
547            self.push_cdp_request(ContinueRequestParams::new(event.request_id.clone()))
548        } else {
549            if let Some(network_id) = event.network_id.as_ref() {
550                if let Some(request_will_be_sent) =
551                    self.requests_will_be_sent.remove(network_id.as_ref())
552                {
553                    self.on_request(&request_will_be_sent, Some(event.request_id.clone().into()));
554                } else {
555                    let current_url = event.request.url.as_str();
556                    let javascript_resource = event.resource_type == ResourceType::Script;
557                    let document_resource = event.resource_type == ResourceType::Document;
558                    let network_resource = !document_resource
559                        && (event.resource_type == ResourceType::Xhr
560                            || event.resource_type == ResourceType::Fetch
561                            || event.resource_type == ResourceType::WebSocket);
562
563                    // block all of these events.
564                    let skip_networking =
565                        IGNORE_NETWORKING_RESOURCE_MAP.contains(event.resource_type.as_ref());
566
567                    let skip_networking = skip_networking || self.document_reload_tracker >= 3;
568
569                    if document_resource {
570                        if self.document_target_domain == current_url {
571                            // this will prevent the domain from looping (3 times is enough).
572                            self.document_reload_tracker += 1;
573                        }
574                        self.document_target_domain = event.request.url.clone();
575                    }
576
577                    // main initial check
578                    let skip_networking = if !skip_networking {
579                        self.ignore_visuals
580                            && (IGNORE_VISUAL_RESOURCE_MAP.contains(event.resource_type.as_ref()))
581                            || self.block_stylesheets
582                                && ResourceType::Stylesheet == event.resource_type
583                            || self.block_javascript
584                                && javascript_resource
585                                && self.intercept_manager == NetworkInterceptManager::Unknown
586                                && !ALLOWED_MATCHER.is_match(current_url)
587                    } else {
588                        skip_networking
589                    };
590
591                    let skip_networking = if !skip_networking {
592                        self.detect_ad(event)
593                    } else {
594                        skip_networking
595                    };
596
597                    let skip_networking = if !skip_networking
598                        && (self.only_html || self.ignore_visuals)
599                        && (javascript_resource || document_resource)
600                    {
601                        ignore_script_embedded(current_url)
602                    } else {
603                        skip_networking
604                    };
605
606                    // analytics check
607                    let skip_networking = if !skip_networking && javascript_resource {
608                        self.ignore_script(
609                            current_url,
610                            self.block_analytics,
611                            self.intercept_manager,
612                        )
613                    } else {
614                        skip_networking
615                    };
616
617                    // XHR check
618                    let skip_networking = self.skip_xhr(skip_networking, &event, network_resource);
619
620                    // custom interception layer.
621                    let skip_networking = if !skip_networking
622                        && (javascript_resource || network_resource || document_resource)
623                    {
624                        self.intercept_manager.intercept_detection(
625                            &event.request.url,
626                            self.ignore_visuals,
627                            network_resource,
628                        )
629                    } else {
630                        skip_networking
631                    };
632
633                    let skip_networking =
634                        if !skip_networking && (javascript_resource || network_resource) {
635                            block_website(&event.request.url)
636                        } else {
637                            skip_networking
638                        };
639
640                    if skip_networking {
641                        let fullfill_params =
642                            crate::handler::network::fetch::FulfillRequestParams::new(
643                                event.request_id.clone(),
644                                200,
645                            );
646                        self.push_cdp_request(fullfill_params);
647                    } else {
648                        self.push_cdp_request(ContinueRequestParams::new(event.request_id.clone()))
649                    }
650                }
651            }
652        }
653
654        // if self.only_html {
655        //     self.made_request = true;
656        // }
657    }
658
659    /// Perform a page intercept for chrome
660    #[cfg(feature = "adblock")]
661    pub fn detect_ad(&self, event: &EventRequestPaused) -> bool {
662        use adblock::{
663            lists::{FilterSet, ParseOptions, RuleTypes},
664            Engine,
665        };
666
667        lazy_static::lazy_static! {
668            static ref AD_ENGINE: Engine = {
669                let mut filter_set = FilterSet::new(false);
670                let mut rules = ParseOptions::default();
671                rules.rule_types = RuleTypes::All;
672
673                filter_set.add_filters(
674                    &*crate::handler::blockers::adblock_patterns::ADBLOCK_PATTERNS,
675                    rules,
676                );
677
678                Engine::from_filter_set(filter_set, true)
679            };
680        };
681
682        let blockable = ResourceType::Image == event.resource_type
683            || event.resource_type == ResourceType::Media
684            || event.resource_type == ResourceType::Stylesheet
685            || event.resource_type == ResourceType::Document
686            || event.resource_type == ResourceType::Fetch
687            || event.resource_type == ResourceType::Xhr;
688
689        let u = &event.request.url;
690
691        let block_request = blockable
692            // set it to example.com for 3rd party handling is_same_site
693        && {
694            let request = adblock::request::Request::preparsed(
695                 &u,
696                 "example.com",
697                 "example.com",
698                 &event.resource_type.as_ref().to_lowercase(),
699                 !event.request.is_same_site.unwrap_or_default());
700
701            AD_ENGINE.check_network_request(&request).matched
702        };
703
704        block_request
705    }
706
707    pub fn on_fetch_auth_required(&mut self, event: &EventAuthRequired) {
708        let response = if self
709            .attempted_authentications
710            .contains(event.request_id.as_ref())
711        {
712            AuthChallengeResponseResponse::CancelAuth
713        } else if self.credentials.is_some() {
714            self.attempted_authentications
715                .insert(event.request_id.clone().into());
716            AuthChallengeResponseResponse::ProvideCredentials
717        } else {
718            AuthChallengeResponseResponse::Default
719        };
720
721        let mut auth = AuthChallengeResponse::new(response);
722        if let Some(creds) = self.credentials.clone() {
723            auth.username = Some(creds.username);
724            auth.password = Some(creds.password);
725        }
726        self.push_cdp_request(ContinueWithAuthParams::new(event.request_id.clone(), auth));
727    }
728
729    pub fn set_offline_mode(&mut self, value: bool) {
730        if self.offline == value {
731            return;
732        }
733        self.offline = value;
734        if let Ok(network) = EmulateNetworkConditionsParams::builder()
735            .offline(self.offline)
736            .latency(0)
737            .download_throughput(-1.)
738            .upload_throughput(-1.)
739            .build()
740        {
741            self.push_cdp_request(network);
742        }
743    }
744
745    /// Request interception doesn't happen for data URLs with Network Service.
746    pub fn on_request_will_be_sent(&mut self, event: &EventRequestWillBeSent) {
747        if self.protocol_request_interception_enabled && !event.request.url.starts_with("data:") {
748            if let Some(interception_id) = self
749                .request_id_to_interception_id
750                .remove(event.request_id.as_ref())
751            {
752                self.on_request(event, Some(interception_id));
753            } else {
754                // TODO remove the clone for event
755                self.requests_will_be_sent
756                    .insert(event.request_id.clone(), event.clone());
757            }
758        } else {
759            self.on_request(event, None);
760        }
761    }
762
763    pub fn on_request_served_from_cache(&mut self, event: &EventRequestServedFromCache) {
764        if let Some(request) = self.requests.get_mut(event.request_id.as_ref()) {
765            request.from_memory_cache = true;
766        }
767    }
768
769    pub fn on_response_received(&mut self, event: &EventResponseReceived) {
770        if let Some(mut request) = self.requests.remove(event.request_id.as_ref()) {
771            request.set_response(event.response.clone());
772            self.queued_events
773                .push_back(NetworkEvent::RequestFinished(request))
774        }
775    }
776
777    pub fn on_network_loading_finished(&mut self, event: &EventLoadingFinished) {
778        if let Some(request) = self.requests.remove(event.request_id.as_ref()) {
779            if let Some(interception_id) = request.interception_id.as_ref() {
780                self.attempted_authentications
781                    .remove(interception_id.as_ref());
782            }
783            self.queued_events
784                .push_back(NetworkEvent::RequestFinished(request));
785        }
786    }
787
788    pub fn on_network_loading_failed(&mut self, event: &EventLoadingFailed) {
789        if let Some(mut request) = self.requests.remove(event.request_id.as_ref()) {
790            request.failure_text = Some(event.error_text.clone());
791            if let Some(interception_id) = request.interception_id.as_ref() {
792                self.attempted_authentications
793                    .remove(interception_id.as_ref());
794            }
795            self.queued_events
796                .push_back(NetworkEvent::RequestFailed(request));
797        }
798    }
799
800    fn on_request(
801        &mut self,
802        event: &EventRequestWillBeSent,
803        interception_id: Option<InterceptionId>,
804    ) {
805        let mut redirect_chain = Vec::new();
806        if let Some(redirect_resp) = event.redirect_response.as_ref() {
807            if let Some(mut request) = self.requests.remove(event.request_id.as_ref()) {
808                self.handle_request_redirect(&mut request, redirect_resp.clone());
809                redirect_chain = std::mem::take(&mut request.redirect_chain);
810                redirect_chain.push(request);
811            }
812        }
813        let request = HttpRequest::new(
814            event.request_id.clone(),
815            event.frame_id.clone(),
816            interception_id,
817            self.user_request_interception_enabled,
818            redirect_chain,
819        );
820
821        self.requests.insert(event.request_id.clone(), request);
822        self.queued_events
823            .push_back(NetworkEvent::Request(event.request_id.clone()));
824    }
825
826    fn handle_request_redirect(&mut self, request: &mut HttpRequest, response: Response) {
827        request.set_response(response);
828        if let Some(interception_id) = request.interception_id.as_ref() {
829            self.attempted_authentications
830                .remove(interception_id.as_ref());
831        }
832    }
833}
834
835#[derive(Debug)]
836pub enum NetworkEvent {
837    SendCdpRequest((MethodId, serde_json::Value)),
838    Request(RequestId),
839    Response(RequestId),
840    RequestFailed(HttpRequest),
841    RequestFinished(HttpRequest),
842}