millipede-browser 0.1.0

BrowserCrawler core, BrowserProvider trait, and BrowserPool for the Millipede web crawler.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Conservative HTTP-response detection for smart browser promotion.

use std::fmt;

use millipede_core::{errors::AntiBotTech, request::Request};
use millipede_html::{SynchronizedHtml, scraper::Selector};

/// A borrowed snapshot of one successful HTTP/HTML attempt.
#[non_exhaustive]
pub struct HttpAttemptSnapshot<'a> {
    /// Request that produced the response.
    pub request: &'a Request,
    /// Final response status.
    pub status: http::StatusCode,
    /// Final response headers.
    pub headers: &'a http::HeaderMap,
    /// Buffered response body.
    pub body: &'a [u8],
    /// Final response URL after redirects.
    pub final_url: &'a url::Url,
    /// Parsed HTML, when the response was parsed as HTML.
    pub html: Option<&'a SynchronizedHtml>,
}

impl<'a> HttpAttemptSnapshot<'a> {
    pub(crate) fn new(
        request: &'a Request,
        status: http::StatusCode,
        headers: &'a http::HeaderMap,
        body: &'a [u8],
        final_url: &'a url::Url,
        html: Option<&'a SynchronizedHtml>,
    ) -> Self {
        Self {
            request,
            status,
            headers,
            body,
            final_url,
            html,
        }
    }
}

/// Why an HTTP attempt should be repeated through a browser.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum PromotionReason {
    /// A successful HTML document has little visible text but contains JavaScript.
    EmptyBodyLikelyJs,
    /// A known anti-bot interstitial was detected.
    KnownAntiBot(AntiBotTech),
    /// A caller-required selector was absent from the parsed document.
    SelectorMissing {
        /// Selector that did not match any element.
        selector: String,
    },
    /// An HTTP error status was configured for browser promotion.
    StatusPromoted {
        /// Numeric HTTP status code.
        status: u16,
    },
    /// A custom detector-specific explanation.
    Custom(String),
}

impl fmt::Display for PromotionReason {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyBodyLikelyJs => {
                formatter.write_str("successful page is likely a JavaScript shell")
            }
            Self::KnownAntiBot(tech) => write!(formatter, "known anti-bot interstitial: {tech:?}"),
            Self::SelectorMissing { selector } => {
                write!(formatter, "required selector is missing: {selector}")
            }
            Self::StatusPromoted { status } => {
                write!(formatter, "configured HTTP status promoted: {status}")
            }
            Self::Custom(reason) => formatter.write_str(reason),
        }
    }
}

/// Decides whether a successful HTTP/HTML attempt needs browser execution.
pub trait BrowserPromotionDetector: Send + Sync + 'static {
    /// Returns the promotion reason, or `None` when the HTTP result should be kept.
    fn should_promote(&self, attempt: &HttpAttemptSnapshot<'_>) -> Option<PromotionReason>;
}

/// Conservative built-in promotion heuristics.
///
/// Anti-bot classification is delegated to the configured core detector.
#[derive(Debug, Clone)]
#[must_use = "detector configuration does nothing unless the detector is installed"]
pub struct DefaultPromotionDetector {
    required_selector: Option<String>,
    min_visible_text: usize,
    anti_bot: std::sync::Arc<dyn millipede_core::antibot::AntiBotDetector>,
}

impl DefaultPromotionDetector {
    /// Creates a detector with conservative defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Promotes parsed pages that do not contain `selector`.
    pub fn with_required_selector(mut self, selector: impl Into<String>) -> Self {
        self.required_selector = Some(selector.into());
        self
    }

    /// Sets the minimum visible body-text length for JavaScript-shell detection.
    pub fn with_min_visible_text(mut self, minimum: usize) -> Self {
        self.min_visible_text = minimum;
        self
    }

    /// Sets the anti-bot detector used to classify response signals.
    pub fn with_anti_bot_detector(
        mut self,
        detector: std::sync::Arc<dyn millipede_core::antibot::AntiBotDetector>,
    ) -> Self {
        self.anti_bot = detector;
        self
    }

    fn inspect_html(
        &self,
        status: http::StatusCode,
        document: &millipede_html::scraper::Html,
    ) -> Option<PromotionReason> {
        if let Some(selector_text) = &self.required_selector {
            if let Ok(selector) = Selector::parse(selector_text) {
                if document.select(&selector).next().is_none() {
                    return Some(PromotionReason::SelectorMissing {
                        selector: selector_text.clone(),
                    });
                }
            }
        }

        if status.is_success() {
            let body_selector = Selector::parse("body").expect("static body selector is valid");
            let script_selector =
                Selector::parse("script").expect("static script selector is valid");
            let visible_text_len = document
                .select(&body_selector)
                .next()
                .map(|body| body.text().collect::<Vec<_>>().join(" ").trim().len())
                .unwrap_or(0);
            let has_script = document.select(&script_selector).next().is_some();
            if visible_text_len < self.min_visible_text && has_script {
                return Some(PromotionReason::EmptyBodyLikelyJs);
            }
        }

        None
    }
}

impl Default for DefaultPromotionDetector {
    fn default() -> Self {
        Self {
            required_selector: None,
            min_visible_text: 40,
            anti_bot: std::sync::Arc::new(millipede_core::antibot::DefaultAntiBotDetector::new()),
        }
    }
}

impl BrowserPromotionDetector for DefaultPromotionDetector {
    fn should_promote(&self, attempt: &HttpAttemptSnapshot<'_>) -> Option<PromotionReason> {
        let signals = millipede_core::antibot::AntiBotSignals::new(
            attempt.status,
            attempt.headers,
            attempt.body,
            attempt.final_url,
        );
        if let Some(tech) = self.anti_bot.detect(&signals) {
            return Some(PromotionReason::KnownAntiBot(tech));
        }

        if let Some(html) = attempt.html {
            if let Some(reason) =
                html.with_html(|document| self.inspect_html(attempt.status, document))
            {
                return Some(reason);
            }
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use millipede_core::request::Request;

    fn snapshot<'a>(
        request: &'a Request,
        headers: &'a http::HeaderMap,
        body: &'a [u8],
        status: http::StatusCode,
    ) -> HttpAttemptSnapshot<'a> {
        HttpAttemptSnapshot::new(request, status, headers, body, &request.url, None)
    }

    fn request() -> Request {
        Request::get("https://example.com/")
            .build()
            .expect("valid test request")
    }

    fn html(source: &str) -> millipede_html::scraper::Html {
        millipede_html::scraper::Html::parse_document(source)
    }

    #[test]
    fn cloudflare_marker_is_promoted() {
        let request = request();
        let headers = http::HeaderMap::new();
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                b"<html><body>Just a moment...</body></html>",
                http::StatusCode::OK,
            )),
            Some(PromotionReason::KnownAntiBot(AntiBotTech::Cloudflare))
        );
    }

    #[test]
    fn contentful_page_stays_http() {
        let document = html(
            "<html><body>A long, genuinely contentful page with enough visible text for the conservative detector.</body></html>",
        );
        assert_eq!(
            DefaultPromotionDetector::new().inspect_html(http::StatusCode::OK, &document),
            None
        );
    }

    #[test]
    fn javascript_shell_is_promoted() {
        let document = html("<html><body><script src=\"app.js\"></script></body></html>");
        assert_eq!(
            DefaultPromotionDetector::new().inspect_html(http::StatusCode::OK, &document),
            Some(PromotionReason::EmptyBodyLikelyJs)
        );
    }

    #[test]
    fn required_selector_only_promotes_when_missing() {
        let missing = html("<html><body><main>content</main></body></html>");
        let present = html("<html><body><main id=\"app\">content</main></body></html>");
        let detector = DefaultPromotionDetector::new().with_required_selector("#app");
        assert_eq!(
            detector.inspect_html(http::StatusCode::OK, &missing),
            Some(PromotionReason::SelectorMissing {
                selector: "#app".to_owned(),
            })
        );
        assert_eq!(detector.inspect_html(http::StatusCode::OK, &present), None);
    }

    #[test]
    fn markers_after_inspection_cap_are_ignored() {
        let request = request();
        let headers = http::HeaderMap::new();
        // Mirrors the core detector's default inspection window.
        let mut body =
            vec![b'x'; millipede_core::antibot::DefaultAntiBotDetector::DEFAULT_INSPECTION_LIMIT];
        body.extend_from_slice(b"just a moment");
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                &body,
                http::StatusCode::OK,
            )),
            None
        );
    }

    #[test]
    fn perimeterx_fixture_is_promoted() {
        let body = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../millipede-core/tests/fixtures/antibot/perimeterx.html"
        ));
        let request = request();
        let headers = http::HeaderMap::new();
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                body.as_bytes(),
                http::StatusCode::OK,
            )),
            Some(PromotionReason::KnownAntiBot(AntiBotTech::PerimeterX))
        );
    }

    #[test]
    fn imperva_fixture_is_promoted() {
        let body = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../millipede-core/tests/fixtures/antibot/imperva.html"
        ));
        let request = request();
        let headers = http::HeaderMap::new();
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                body.as_bytes(),
                http::StatusCode::OK,
            )),
            Some(PromotionReason::KnownAntiBot(AntiBotTech::Imperva))
        );
    }

    #[test]
    fn cloudflare_fixture_is_promoted() {
        let body = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../millipede-core/tests/fixtures/antibot/cloudflare.html"
        ));
        let request = request();
        let headers = http::HeaderMap::new();
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                body.as_bytes(),
                http::StatusCode::OK,
            )),
            Some(PromotionReason::KnownAntiBot(AntiBotTech::Cloudflare))
        );
    }

    macro_rules! shared_vendor_fixture_test {
        ($name:ident, $fixture:literal, $expected:expr) => {
            #[test]
            fn $name() {
                let body = include_str!(concat!(
                    env!("CARGO_MANIFEST_DIR"),
                    "/../millipede-core/tests/fixtures/antibot/",
                    $fixture
                ));
                let request = request();
                let headers = http::HeaderMap::new();
                assert_eq!(
                    DefaultPromotionDetector::new().should_promote(&snapshot(
                        &request,
                        &headers,
                        body.as_bytes(),
                        http::StatusCode::OK,
                    )),
                    Some(PromotionReason::KnownAntiBot($expected))
                );
            }
        };
    }

    shared_vendor_fixture_test!(
        datadome_fixture_is_promoted,
        "datadome.html",
        AntiBotTech::DataDome
    );
    shared_vendor_fixture_test!(
        kasada_fixture_is_promoted,
        "kasada.html",
        AntiBotTech::Kasada
    );
    shared_vendor_fixture_test!(
        akamai_fixture_is_promoted,
        "akamai.html",
        AntiBotTech::Akamai
    );
    shared_vendor_fixture_test!(
        unknown_fixture_is_promoted,
        "unknown.html",
        AntiBotTech::Unknown
    );

    #[test]
    fn benign_contentful_fixture_stays_http() {
        let body = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../millipede-core/tests/fixtures/antibot/benign_contentful.html"
        ));
        let request = request();
        let headers = http::HeaderMap::new();
        assert_eq!(
            DefaultPromotionDetector::new().should_promote(&snapshot(
                &request,
                &headers,
                body.as_bytes(),
                http::StatusCode::OK,
            )),
            None
        );
    }

    #[test]
    fn contentful_forbidden_page_does_not_trip_body_detector() {
        let document = html(
            "<html><body>A long forbidden response with meaningful content and no JavaScript shell markers.</body></html>",
        );
        assert_eq!(
            DefaultPromotionDetector::new().inspect_html(http::StatusCode::FORBIDDEN, &document),
            None
        );
    }
}