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>
impl<'tab> InterceptBuilder<'tab>
Sourcepub fn new(tab: &'tab SessionHandle) -> Self
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();Sourcepub fn handle_auth(
self,
user: impl Into<String>,
pass: impl Into<String>,
) -> Self
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.
Sourcepub fn pattern(self, pattern: impl Into<String>) -> Self
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.
Sourcepub fn at_request(self) -> Self
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.
Sourcepub fn at_response(self) -> Self
pub fn at_response(self) -> Self
Pause matching requests at the Response stage on the most-recently
pushed pattern.
Sourcepub fn resource(self, kind: ResourceType) -> Self
pub fn resource(self, kind: ResourceType) -> Self
Restrict the most-recently pushed pattern to a single resource type.
Sourcepub fn block(
self,
pattern: impl Into<String>,
) -> Result<Self, InterceptionError>
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.
Sourcepub fn block_hosts(self, matcher: Arc<HostMatcher>) -> Self
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.
Sourcepub fn redirect(
self,
from: impl Into<String>,
to: impl Into<String>,
) -> Result<Self, InterceptionError>
pub fn redirect( self, from: impl Into<String>, to: impl Into<String>, ) -> Result<Self, InterceptionError>
Register a Rule::Redirect that rewrites from → to.
Sourcepub fn respond(
self,
pattern: impl Into<String>,
status: u16,
headers: Vec<(String, String)>,
body: Vec<u8>,
) -> Result<Self, InterceptionError>
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.
Sourcepub fn modify_request<F>(
self,
pattern: impl Into<String>,
modify: F,
) -> Result<Self, InterceptionError>
pub fn modify_request<F>( self, pattern: impl Into<String>, modify: F, ) -> Result<Self, InterceptionError>
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.
Sourcepub fn modify_response<F>(
self,
pattern: impl Into<String>,
modify: F,
) -> Result<Self, InterceptionError>
pub fn modify_response<F>( self, pattern: impl Into<String>, modify: F, ) -> Result<Self, InterceptionError>
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.
Sourcepub fn start(self) -> InterceptHandle
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.
Sourcepub fn subscribe(self) -> impl Stream<Item = PausedRequest> + Send + use<>
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.