pub struct Context<C> { /* private fields */ }Expand description
A request being crawled, with everything needed to handle it.
It holds the Request, a session to fetch it with, a Queue to add
the links it leads to, and the crawl’s datasets. Handlers borrow it, so
several of them can handle one request: the request is fetched once, by
the first response or body, and the
complete response is kept. Its body is a fixed snapshot, including for
browser sessions; live document operations go through the session.
The crawler learns how the fetch went, its time and status, from the task
the request runs on. Fetching after moving the context to another task,
e.g. with tokio::spawn, works, but adaptive host limits and
Stats::unchanged do not see it.
Implementations§
Source§impl<C> Context<C>
impl<C> Context<C>
Sourcepub fn new(request: Request, session: C) -> Self
pub fn new(request: Request, session: C) -> Self
Creates a standalone context, e.g. to test a handler.
It gets an empty queue of its own and no datasets; see
with_queue and
with_dataset.
Sourcepub fn with_queue(self, queue: &Queue) -> Self
pub fn with_queue(self, queue: &Queue) -> Self
Replaces the queue, binding it to the request.
Sourcepub fn with_dataset<T, D>(self, dataset: D) -> Self
pub fn with_dataset<T, D>(self, dataset: D) -> Self
Registers dataset for items of type T.
Sourcepub fn with_extension<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
pub fn with_extension<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
Adds a service to this request, replacing any value of the same type.
Sourcepub const fn request_mut(&mut self) -> &mut Request
pub const fn request_mut(&mut self) -> &mut Request
Returns the request mutably, e.g. to add headers before fetching.
Sourcepub async fn response(&self) -> Result<&Response>where
C: Session,
pub async fn response(&self) -> Result<&Response>where
C: Session,
Returns the complete response, fetching the request on the first call.
Later calls return the same response without fetching again. A failed fetch is not kept: the next call tries again. Once fetched, links followed through the queue, including clones taken earlier, resolve against the URI the response was served from.
§Errors
Returns an error if the session fails to fetch the request.
Sourcepub async fn body(&self) -> Result<Bytes>where
C: Session,
pub async fn body(&self) -> Result<Bytes>where
C: Session,
Returns a cheap clone of the cached response body, fetching first if needed. Browser sessions also return a fixed HTML snapshot; use the session’s document capability for live observations.
§Errors
Returns an error if the request cannot be fetched.