Skip to main content

InterceptBuilder

Struct InterceptBuilder 

Source
pub struct InterceptBuilder<'tab> { /* private fields */ }
Expand description

Fluent builder for rule-based interception against a single tab session.

Construct via Tab::intercept() (gated feature = "interception", wired in Task 7). Chain configuration methods to register rules and declare CDP Fetch.enable patterns, then call start (Task 7) to activate the background actor or subscribe (Task 7) for the stream-driven escape hatch.

'tab ties the builder’s lifetime to the tab’s session — the borrow lasts only until start() / subscribe() consumes the builder.

Implementations§

Source§

impl<'tab> InterceptBuilder<'tab>

Source

pub fn new(tab: &'tab SessionHandle) -> Self

Construct a fresh builder bound to tab’s session.

pub so adapter crates (e.g. zendriver core’s Tab::intercept() shim) can construct it from a &SessionHandle without going through a trait. End users go through Tab::intercept() rather than calling this directly.

use zendriver_interception::InterceptBuilder;

let _handle = InterceptBuilder::new(tab)
    .block("*/tracker.js")?
    .start();
Source

pub fn handle_auth( self, user: impl Into<String>, pass: impl Into<String>, ) -> Self

Auto-respond to Fetch.authRequired challenges with the given credentials.

This is the proxy-auth (and HTTP basic-auth) path: Fetch.enable is sent with handleAuthRequests: true and every Fetch.authRequired event is answered with Fetch.continueWithAuth { authChallengeResponse: { response: "ProvideCredentials", username, password } }.

Compose with rules: an InterceptBuilder configured with handle_auth and block / redirect / respond rules handles both paths from the same actor. Combine with [BrowserBuilder::proxy_auth] in the zendriver crate if you want the wiring installed automatically on every tab.

See cdpdriver/zendriver#208.

Source

pub fn pattern(self, pattern: impl Into<String>) -> Self

Push a new pattern entry with the given URL pattern string.

Subsequent at_request / at_response / resource calls mutate this newest entry, so a chain like .pattern("*").at_response().resource(ResourceType::XHR) produces one RequestPattern with all three fields populated.

Source

pub fn at_request(self) -> Self

Pause matching requests at the Request stage on the most-recently pushed pattern.

If no pattern has been pushed yet, this creates an empty one (matches every URL by CDP default) and sets the stage on it.

Source

pub fn at_response(self) -> Self

Pause matching requests at the Response stage on the most-recently pushed pattern.

Source

pub fn resource(self, kind: ResourceType) -> Self

Restrict the most-recently pushed pattern to a single resource type.

Source

pub fn block( self, pattern: impl Into<String>, ) -> Result<Self, InterceptionError>

Register a Rule::Block for pattern.

Compiles pattern eagerly; an invalid pattern fails the builder chain with InterceptionError::InvalidPattern returned as Err(Self) via the Result wrapper.

Source

pub fn block_hosts(self, matcher: Arc<HostMatcher>) -> Self

Register a Rule::BlockHosts backed by matcher.

Every request whose host is in matcher (exact, or a parent domain on a dot boundary) is failed with BlockedByClient. Composes with other rules in registration order. zendriver core’s tracker-blocklist wiring uses this; most callers reach it via BrowserBuilder::block_trackers.

Source

pub fn redirect( self, from: impl Into<String>, to: impl Into<String>, ) -> Result<Self, InterceptionError>

Register a Rule::Redirect that rewrites fromto.

Source

pub fn respond( self, pattern: impl Into<String>, status: u16, headers: Vec<(String, String)>, body: Vec<u8>, ) -> Result<Self, InterceptionError>

Register a Rule::Respond serving a synthesized response.

Source

pub fn modify_request<F>( self, pattern: impl Into<String>, modify: F, ) -> Result<Self, InterceptionError>
where F: Fn(&RequestInfo) -> RequestOverrides + Send + Sync + 'static,

Register a Rule::Modify driven by a user closure.

The closure runs on the actor task per matching request — it must be Send + Sync and 'static. Wrap shared state in Arc if needed.

Source

pub fn modify_response<F>( self, pattern: impl Into<String>, modify: F, ) -> Result<Self, InterceptionError>
where F: Fn(&ResponseInfo) -> ResponseOverrides + Send + Sync + 'static,

Register a Rule::ModifyResponse driven by a user closure.

The closure rewrites an upstream response’s status/headers (keeping Chrome’s body) and only fires at the Response stage — pair this with at_response so Chrome actually pauses there. Header overrides are replacement, not merge (CDP semantics): return every header you want forwarded.

Like modify_request, the closure runs on the actor task per matching response, so it must be Send + Sync and 'static. Wrap shared state in Arc if needed.

Source

pub fn start(self) -> InterceptHandle

Activate the rule-based interception loop.

Spawns the background actor task with the registered rules and CDP RequestPattern list, and returns an InterceptHandle whose Drop (or explicit stop) tears the actor down.

If no pattern entries were added, a single match-all ("*") pattern is sent so Chrome actually pauses requests — without it, Fetch.enable would attach to nothing and the rule list would never fire.

Source

pub fn subscribe(self) -> impl Stream<Item = PausedRequest> + Send + use<>

Manual escape-hatch: subscribe to raw PausedRequest events.

Enables Fetch interception with the declared patterns (defaulting to a single match-all "*" pattern when none were added) and returns a Stream that yields one PausedRequest per Fetch.requestPaused CDP event. Callers must dispatch one of PausedRequest’s terminal methods (continue_ / abort / respond / modify_and_continue) to release each pause — Chrome holds the request open otherwise.

Rules registered via block / redirect / respond / modify_request are ignored on this path: stream consumers drive every paused request themselves. Use start when you want the actor to apply rules automatically.

The returned stream owns the underlying CDP subscription. Dropping the stream tears the subscription down — Chrome’s interception stays active until the session is closed, but no further pauses surface to the caller.

Trait Implementations§

Source§

impl<'tab> Debug for InterceptBuilder<'tab>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'tab> !RefUnwindSafe for InterceptBuilder<'tab>

§

impl<'tab> !UnwindSafe for InterceptBuilder<'tab>

§

impl<'tab> Freeze for InterceptBuilder<'tab>

§

impl<'tab> Send for InterceptBuilder<'tab>

§

impl<'tab> Sync for InterceptBuilder<'tab>

§

impl<'tab> Unpin for InterceptBuilder<'tab>

§

impl<'tab> UnsafeUnpin for InterceptBuilder<'tab>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more