Skip to main content

millipede_http/
nav.rs

1//! Concrete HTTP navigation hook contexts.
2//!
3//! These contexts follow INTERFACE ยง18 with two documented deviations. First, `request` is a
4//! read-only `&Request` rather than `&mut Request` because [`crate::HttpKind`] execution holds the
5//! request in an `Arc<Request>` (`env.request`) that cannot be mutated in place; the worked
6//! example only mutates `http_request`, so its documented usage is preserved. Second, the
7//! interface's `log` field is omitted because no `Log` type exists in the workspace yet. The
8//! context structs are `#[non_exhaustive]`, allowing that field to be added later without a
9//! breaking change.
10
11use futures_util::future::BoxFuture;
12use millipede_core::{
13    errors::CrawlError,
14    http_client::{HttpRequest, HttpResponse},
15    proxy::ProxyInfo,
16    request::Request,
17    session::Session,
18};
19use std::sync::Arc;
20
21/// State exposed immediately before an HTTP request is sent.
22#[non_exhaustive]
23pub struct HttpPreHookCtx<'a> {
24    /// Crawl request that produced the outgoing HTTP request.
25    pub request: &'a Request,
26    /// Mutable outgoing HTTP request.
27    pub http_request: &'a mut HttpRequest,
28    /// Session selected for this attempt, if enabled.
29    pub session: Option<&'a Session>,
30    /// Proxy selected for this attempt, if any.
31    pub proxy: Option<&'a ProxyInfo>,
32}
33
34/// State exposed after an HTTP response is received.
35#[non_exhaustive]
36pub struct HttpPostHookCtx<'a> {
37    /// Crawl request that produced the response.
38    pub request: &'a Request,
39    /// Received HTTP response.
40    pub response: &'a HttpResponse,
41    /// Session selected for this attempt, if enabled.
42    pub session: Option<&'a Session>,
43    /// Proxy selected for this attempt, if any.
44    pub proxy: Option<&'a ProxyInfo>,
45}
46
47/// Asynchronous hook run immediately before an HTTP request is sent.
48pub type HttpPreNavigationHook =
49    Arc<dyn for<'a> Fn(HttpPreHookCtx<'a>) -> BoxFuture<'a, Result<(), CrawlError>> + Send + Sync>;
50
51/// Asynchronous hook run after an HTTP response is received and anti-bot detection completes.
52pub type HttpPostNavigationHook =
53    Arc<dyn for<'a> Fn(HttpPostHookCtx<'a>) -> BoxFuture<'a, Result<(), CrawlError>> + Send + Sync>;