rust_scrapling 0.2.3

A Rust port of Scrapling - modern web scraping framework with CSS selectors, async HTTP, and spider-based crawling
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! robots.txt compliance: fetching, RFC 9309 parsing (multi-agent groups,
//! wildcard patterns, longest-match-wins with Allow winning ties), and
//! `Crawl-delay` extraction. Enabled per spider via
//! [`Spider::robots_txt_obey`](crate::spiders::spider::Spider::robots_txt_obey).

use std::collections::HashMap;
use std::time::Duration;
use url::Url;

/// Fetches, parses, and caches robots.txt rules per origin, and answers
/// whether a URL may be crawled.
///
/// Rules are cached per scheme+authority origin (RFC 9309 scoping). When
/// the manager lives behind a lock, use the [`RobotsTxtManager::fetch_rules`]
/// / [`RobotsTxtManager::insert_rules`] split so the network round-trip
/// runs without holding the lock; otherwise
/// [`RobotsTxtManager::fetch_robots`] does both in one call.
pub struct RobotsTxtManager {
    cache: HashMap<String, RobotsRules>,
    user_agent: String,
}

/// Parsed rules for one origin. `rules` holds (allow, pattern) pairs from
/// `Allow:`/`Disallow:` lines; evaluation is longest-match-wins with Allow
/// winning ties (Google's published robots.txt algorithm, which RFC 9309
/// follows).
struct RobotsRules {
    rules: Vec<(bool, String)>,
    crawl_delay: Option<f64>,
}

/// One contiguous `User-agent:` group from a robots.txt file: a set of
/// applicable agents followed by their directives.
struct RobotsGroup {
    agents: Vec<String>,
    rules: Vec<(bool, String)>,
    crawl_delay: Option<f64>,
}

/// Parsed robots.txt rules for one domain, produced by
/// [`RobotsTxtManager::fetch_rules`] and stored via
/// [`RobotsTxtManager::insert_rules`]. Opaque on purpose: the split exists so
/// callers can run the (slow) network fetch WITHOUT borrowing the manager —
/// i.e. without holding a lock on it — and hand the result back afterwards.
pub struct FetchedRobots(RobotsRules);

impl RobotsTxtManager {
    /// Create an empty manager. `user_agent` is the identity robots.txt
    /// `User-agent:` groups are matched against (its product token — the
    /// part before any `/` or space — also matches, per RFC 9309).
    pub fn new(user_agent: &str) -> Self {
        Self {
            cache: HashMap::new(),
            user_agent: user_agent.to_string(),
        }
    }

    /// The user agent string robots.txt groups are matched against.
    pub fn user_agent(&self) -> &str {
        &self.user_agent
    }

    /// Derive the robots.txt origin for a request URL, preserving the URL's
    /// actual scheme and any explicit port (`http://shop.example:8080`), so
    /// robots.txt is fetched from where the site actually lives instead of a
    /// hardcoded `https://host/`. The origin doubles as the rules cache key
    /// — RFC 9309 scopes robots.txt per scheme + authority, so
    /// `http://example.com` and `https://example.com` keep distinct rules.
    /// Returns `None` for URLs without a host (data:, mailto:, malformed),
    /// which skip robots handling entirely.
    pub fn origin_and_key(url: &str) -> Option<(String, String)> {
        let u = Url::parse(url).ok()?;
        let host = u.host_str()?;
        let origin = match u.port() {
            Some(port) => format!("{}://{}:{}", u.scheme(), host, port),
            None => format!("{}://{}", u.scheme(), host),
        };
        Some((origin.clone(), origin))
    }

    /// Fetch and parse robots.txt for an origin without borrowing a manager.
    /// `origin` is either a full origin (`http://host:8080`) or a bare host,
    /// in which case `https://` is assumed. Uses a purpose-built HTTP client
    /// with a short timeout so a hanging endpoint cannot block the caller
    /// indefinitely. Unreachable or non-2xx robots.txt endpoints yield
    /// allow-all rules.
    pub async fn fetch_rules(user_agent: &str, origin: &str) -> FetchedRobots {
        let url = if origin.contains("://") {
            format!("{}/robots.txt", origin)
        } else {
            format!("https://{}/robots.txt", origin)
        };
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .unwrap_or_else(|_| reqwest::Client::new());

        let rules = match client.get(&url).send().await {
            Ok(resp) if resp.status().is_success() => match resp.text().await {
                Ok(text) => Self::parse_robots(&text, user_agent),
                Err(_) => RobotsRules::allow_all(),
            },
            _ => RobotsRules::allow_all(),
        };
        FetchedRobots(rules)
    }

    /// Store previously fetched rules under a cache key (see
    /// [`Self::origin_and_key`]).
    pub fn insert_rules(&mut self, key: &str, rules: FetchedRobots) {
        self.cache.insert(key.to_string(), rules.0);
    }

    /// Fetch and store robots.txt for an origin (or bare domain, https
    /// assumed) in one step. Prefer the [`Self::fetch_rules`] +
    /// [`Self::insert_rules`] split when the manager lives behind a lock —
    /// this method borrows `self` for the whole network round-trip.
    pub async fn fetch_robots(&mut self, domain: &str) {
        // Normalize a bare domain to the https origin so the stored key
        // matches what is_allowed derives from https URLs.
        let key = if domain.contains("://") {
            domain.to_string()
        } else {
            format!("https://{}", domain)
        };
        let rules = Self::fetch_rules(&self.user_agent, domain).await;
        self.insert_rules(&key, rules);
    }

    /// Whether `url` is allowed by the stored rules for its origin. Origins
    /// without stored rules are allowed (robots.txt not fetched or absent).
    /// Matching runs against the URL's path plus query string, per the
    /// robots.txt matching rules.
    pub fn is_allowed(&self, url: &str) -> bool {
        let key = match Self::origin_and_key(url) {
            Some((_, key)) => key,
            None => return true,
        };
        let parsed = match Url::parse(url) {
            Ok(u) => u,
            Err(_) => return true,
        };
        let mut target = parsed.path().to_string();
        if let Some(q) = parsed.query() {
            target.push('?');
            target.push_str(q);
        }

        match self.cache.get(&key) {
            Some(rules) => rules.is_path_allowed(&target),
            None => true,
        }
    }

    /// Crawl-delay for a cache key (see [`Self::origin_and_key`]), if the
    /// origin's robots.txt declared one for our user agent.
    pub fn crawl_delay(&self, key: &str) -> Option<f64> {
        self.cache.get(key).and_then(|r| r.crawl_delay)
    }

    /// Whether robots.txt has already been fetched (cached) for this key.
    pub fn has_domain(&self, key: &str) -> bool {
        self.cache.contains_key(key)
    }

    fn parse_robots(text: &str, user_agent: &str) -> RobotsRules {
        let ua_lower = user_agent.to_lowercase();
        // RFC 9309 matches groups against the crawler's product token, so
        // "User-agent: MyBot" applies to a crawler identifying as
        // "MyBot/1.0 (+https://example.com/bot)".
        let ua_token = ua_lower
            .split(['/', ' '])
            .next()
            .unwrap_or(&ua_lower)
            .to_string();
        let groups = parse_groups(text);

        // Per RFC 9309 all groups matching the agent are combined. A
        // specific agent match wins over the wildcard groups.
        let specific: Vec<&RobotsGroup> = groups
            .iter()
            .filter(|g| g.agents.iter().any(|a| a == &ua_lower || a == &ua_token))
            .collect();
        let chosen: Vec<&RobotsGroup> = if specific.is_empty() {
            groups
                .iter()
                .filter(|g| g.agents.iter().any(|a| a == "*"))
                .collect()
        } else {
            specific
        };

        if chosen.is_empty() {
            return RobotsRules::allow_all();
        }
        RobotsRules {
            rules: chosen.iter().flat_map(|g| g.rules.clone()).collect(),
            crawl_delay: chosen.iter().find_map(|g| g.crawl_delay),
        }
    }
}

impl RobotsRules {
    fn allow_all() -> Self {
        Self {
            rules: Vec::new(),
            crawl_delay: None,
        }
    }

    /// Longest-match-wins over all matching Allow/Disallow patterns;
    /// specificity is the pattern's length and Allow wins exact ties. No
    /// matching rule means allowed.
    fn is_path_allowed(&self, target: &str) -> bool {
        let mut best: Option<(usize, bool)> = None;
        for (allow, pattern) in &self.rules {
            if pattern_matches(pattern, target) {
                let specificity = pattern.len();
                best = match best {
                    None => Some((specificity, *allow)),
                    Some((best_len, best_allow)) => {
                        if specificity > best_len
                            || (specificity == best_len && *allow && !best_allow)
                        {
                            Some((specificity, *allow))
                        } else {
                            Some((best_len, best_allow))
                        }
                    }
                };
            }
        }
        best.map(|(_, allow)| allow).unwrap_or(true)
    }
}

/// robots.txt pattern matching: patterns are path prefixes, `*` matches any
/// character sequence, and a trailing `$` anchors the match to the end of
/// the target.
fn pattern_matches(pattern: &str, target: &str) -> bool {
    let (pat, anchored) = match pattern.strip_suffix('$') {
        Some(p) => (p, true),
        None => (pattern, false),
    };
    let parts: Vec<&str> = pat.split('*').collect();

    if parts.len() == 1 {
        // No wildcards: plain prefix, or exact match when anchored.
        return if anchored {
            target == parts[0]
        } else {
            target.starts_with(parts[0])
        };
    }

    // First fragment is anchored at the start.
    if !target.starts_with(parts[0]) {
        return false;
    }
    let mut pos = parts[0].len();

    // For anchored patterns the LAST fragment must sit at the very end of
    // the target, so reserve that region and match the middle fragments
    // leftmost-first inside what remains. (A naive leftmost match of the
    // last fragment would wrongly reject e.g. pattern `/*.php$` against
    // `/a.php.php`.)
    let (middle, limit) = if anchored {
        let last = parts[parts.len() - 1];
        if !target.ends_with(last) || target.len() < pos + last.len() {
            return false;
        }
        (&parts[1..parts.len() - 1], target.len() - last.len())
    } else {
        (&parts[1..], target.len())
    };

    for part in middle {
        if part.is_empty() {
            continue; // consecutive '*' collapse
        }
        match target[pos..limit].find(part) {
            Some(idx) => pos += idx + part.len(),
            None => return false,
        }
    }
    pos <= limit
}

/// Parse robots.txt into groups. A group starts at one or more consecutive
/// `User-agent:` lines and ends when the next `User-agent:` line appears
/// after at least one directive (handling the standard multi-agent grouping
/// where several `User-agent:` lines share a single rule block).
fn parse_groups(text: &str) -> Vec<RobotsGroup> {
    let mut groups: Vec<RobotsGroup> = Vec::new();
    let mut current: Option<RobotsGroup> = None;
    let mut saw_directive = false;

    for raw in text.lines() {
        // Strip inline comments, then whitespace.
        let line = match raw.find('#') {
            Some(idx) => raw[..idx].trim(),
            None => raw.trim(),
        };
        if line.is_empty() {
            continue;
        }

        if let Some(rest) = strip_prefix_ci(line, "user-agent:") {
            if saw_directive {
                if let Some(g) = current.take() {
                    groups.push(g);
                }
                saw_directive = false;
            }
            let g = current.get_or_insert_with(|| RobotsGroup {
                agents: Vec::new(),
                rules: Vec::new(),
                crawl_delay: None,
            });
            g.agents.push(rest.trim().to_lowercase());
        } else if let Some(g) = current.as_mut() {
            if let Some(rest) = strip_prefix_ci(line, "disallow:") {
                let path = rest.trim();
                // An empty Disallow means "allow everything" — no rule.
                if !path.is_empty() {
                    g.rules.push((false, path.to_string()));
                }
                saw_directive = true;
            } else if let Some(rest) = strip_prefix_ci(line, "allow:") {
                let path = rest.trim();
                if !path.is_empty() {
                    g.rules.push((true, path.to_string()));
                }
                saw_directive = true;
            } else if let Some(rest) = strip_prefix_ci(line, "crawl-delay:") {
                // Reject non-finite, negative, and absurd values: f64
                // parsing accepts "inf"/"NaN"/"1e300", and a hostile
                // robots.txt must not be able to panic the crawler
                // (Duration::from_secs_f64 panics on non-finite/oversized
                // input) or stall it forever. 24h is already unreasonable.
                if let Ok(d) = rest.trim().parse::<f64>() {
                    if d.is_finite() && (0.0..=86_400.0).contains(&d) {
                        g.crawl_delay = Some(d);
                    }
                }
                saw_directive = true;
            }
        }
    }
    if let Some(g) = current.take() {
        groups.push(g);
    }
    groups
}

fn strip_prefix_ci<'a>(line: &'a str, prefix: &str) -> Option<&'a str> {
    if line.len() >= prefix.len()
        && line.as_bytes()[..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
    {
        Some(&line[prefix.len()..])
    } else {
        None
    }
}

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

    fn mgr_with(txt: &str, ua: &str, key: &str) -> RobotsTxtManager {
        let mut mgr = RobotsTxtManager::new(ua);
        mgr.insert_rules(key, FetchedRobots(RobotsTxtManager::parse_robots(txt, ua)));
        mgr
    }

    #[test]
    fn multi_agent_group_applies_to_each_agent() {
        let txt = "User-agent: MyBot\nUser-agent: Googlebot\nDisallow: /private\n";
        for ua in ["MyBot", "Googlebot"] {
            let mgr = mgr_with(txt, ua, "https://example.com");
            assert!(!mgr.is_allowed("https://example.com/private/x"));
            assert!(mgr.is_allowed("https://example.com/public"));
        }
    }

    #[test]
    fn specific_agent_wins_over_wildcard() {
        let txt = "User-agent: *\nDisallow: /all\n\nUser-agent: MyBot\nDisallow: /mine\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(!mgr.is_allowed("https://example.com/mine"));
        assert!(mgr.is_allowed("https://example.com/all"));
    }

    #[test]
    fn wildcard_applies_when_no_specific_match() {
        let txt = "User-agent: *\nDisallow: /all\n";
        let mgr = mgr_with(txt, "OtherBot", "https://example.com");
        assert!(!mgr.is_allowed("https://example.com/all"));
    }

    #[test]
    fn unknown_agent_with_no_wildcard_is_allow_all() {
        let txt = "User-agent: SomeoneElse\nDisallow: /\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(mgr.is_allowed("https://example.com/anything"));
    }

    #[test]
    fn crawl_delay_is_parsed() {
        let txt = "User-agent: *\nCrawl-delay: 5\n";
        let rules = RobotsTxtManager::parse_robots(txt, "MyBot");
        assert_eq!(rules.crawl_delay, Some(5.0));
    }

    #[test]
    fn hostile_crawl_delay_values_are_rejected() {
        // f64 parsing accepts "inf"/"NaN"/"1e300"; a hostile robots.txt
        // must not be able to panic the crawler (Duration::from_secs_f64
        // panics on non-finite/oversized input) or stall it forever.
        for bad in ["inf", "-inf", "NaN", "1e300", "-5", "999999999"] {
            let txt = format!("User-agent: *\nCrawl-delay: {}\n", bad);
            let rules = RobotsTxtManager::parse_robots(&txt, "MyBot");
            assert_eq!(rules.crawl_delay, None, "value {:?} must be rejected", bad);
        }
        // Sane values still parse.
        let rules = RobotsTxtManager::parse_robots("User-agent: *\nCrawl-delay: 2.5\n", "MyBot");
        assert_eq!(rules.crawl_delay, Some(2.5));
    }

    #[test]
    fn allow_overrides_broader_disallow() {
        // The classic pattern the old prefix-only matcher got wrong:
        // Disallow: / + Allow: /public must NOT block everything.
        let txt = "User-agent: *\nDisallow: /\nAllow: /public\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(mgr.is_allowed("https://example.com/public"));
        assert!(mgr.is_allowed("https://example.com/public/page"));
        assert!(!mgr.is_allowed("https://example.com/private"));
        assert!(!mgr.is_allowed("https://example.com/"));
    }

    #[test]
    fn longest_match_wins_regardless_of_order() {
        let txt = "User-agent: *\nAllow: /a\nDisallow: /a/b\nAllow: /a/b/c\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(mgr.is_allowed("https://example.com/a/x"));
        assert!(!mgr.is_allowed("https://example.com/a/b/x"));
        assert!(mgr.is_allowed("https://example.com/a/b/c/x"));
    }

    #[test]
    fn allow_wins_specificity_ties() {
        let txt = "User-agent: *\nDisallow: /page\nAllow: /page\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(mgr.is_allowed("https://example.com/page"));
    }

    #[test]
    fn star_wildcard_matches_inside_paths() {
        // `?` is a literal in robots patterns: `/*?sessionid` requires
        // sessionid directly after the `?`; `/*sessionid=` matches it
        // anywhere in the path-plus-query target.
        let txt = "User-agent: *\nDisallow: /*?sessionid\nDisallow: /*sessionid=\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        // Query strings are part of the match target.
        assert!(!mgr.is_allowed("https://example.com/page?sessionid=42"));
        assert!(!mgr.is_allowed("https://example.com/a/b?x=1&sessionid=2"));
        assert!(mgr.is_allowed("https://example.com/page"));

        let only_direct = mgr_with(
            "User-agent: *\nDisallow: /*?sessionid\n",
            "MyBot",
            "https://example.com",
        );
        // Literal `?sessionid` does not match `&sessionid`.
        assert!(only_direct.is_allowed("https://example.com/a/b?x=1&sessionid=2"));
    }

    #[test]
    fn dollar_anchors_to_end() {
        let txt = "User-agent: *\nDisallow: /*.php$\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(!mgr.is_allowed("https://example.com/index.php"));
        // A later occurrence of the suffix still anchors: /a.php.php ends
        // with .php even though the FIRST .php is mid-string.
        assert!(!mgr.is_allowed("https://example.com/a.php.php"));
        assert!(mgr.is_allowed("https://example.com/a.php.bak"));
    }

    #[test]
    fn pattern_matching_edge_cases() {
        assert!(pattern_matches("/", "/anything"));
        assert!(pattern_matches("/a*b*c", "/a-x-b-y-c-z"));
        assert!(!pattern_matches("/a*b$", "/a-b-tail"));
        assert!(pattern_matches("/a*b$", "/a-x-b"));
        assert!(pattern_matches("/a**b", "/ab"));
        assert!(pattern_matches("/exact$", "/exact"));
        assert!(!pattern_matches("/exact$", "/exact/more"));
        // Overlap guard: pattern /a$ with wildcard prefix cannot match a
        // target shorter than first+last fragments combined.
        assert!(!pattern_matches("/aa*aa$", "/aaa"));
    }

    #[test]
    fn inline_comments_are_stripped() {
        let txt = "User-agent: * # everyone\nDisallow: /private # keep out\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(!mgr.is_allowed("https://example.com/private/x"));
        assert!(mgr.is_allowed("https://example.com/open"));
    }

    #[test]
    fn multiple_groups_for_same_agent_are_combined() {
        let txt = "User-agent: MyBot\nDisallow: /one\n\nUser-agent: MyBot\nDisallow: /two\n";
        let mgr = mgr_with(txt, "MyBot", "https://example.com");
        assert!(!mgr.is_allowed("https://example.com/one"));
        assert!(!mgr.is_allowed("https://example.com/two"));
        assert!(mgr.is_allowed("https://example.com/three"));
    }

    #[test]
    fn origin_and_key_preserve_scheme_and_port() {
        assert_eq!(
            RobotsTxtManager::origin_and_key("http://shop.example:8080/x?y=1"),
            Some((
                "http://shop.example:8080".to_string(),
                "http://shop.example:8080".to_string()
            ))
        );
        assert_eq!(
            RobotsTxtManager::origin_and_key("https://example.com/path"),
            Some((
                "https://example.com".to_string(),
                "https://example.com".to_string()
            ))
        );
        // Default port is normalized away by the URL parser.
        assert_eq!(
            RobotsTxtManager::origin_and_key("https://example.com:443/x"),
            Some((
                "https://example.com".to_string(),
                "https://example.com".to_string()
            ))
        );
        assert_eq!(RobotsTxtManager::origin_and_key("data:text/plain,hi"), None);
    }

    #[test]
    fn distinct_ports_have_distinct_rules() {
        let mut mgr = RobotsTxtManager::new("MyBot");
        mgr.insert_rules(
            "http://example.com:8080",
            FetchedRobots(RobotsTxtManager::parse_robots(
                "User-agent: *\nDisallow: /\n",
                "MyBot",
            )),
        );
        assert!(!mgr.is_allowed("http://example.com:8080/x"));
        // No rules stored for the default-port origin -> allowed.
        assert!(mgr.is_allowed("http://example.com/x"));
    }

    #[test]
    fn distinct_schemes_have_distinct_rules() {
        // RFC 9309 scopes robots.txt per scheme + authority: rules fetched
        // for the http origin must not govern the https origin.
        let mut mgr = RobotsTxtManager::new("MyBot");
        mgr.insert_rules(
            "http://example.com",
            FetchedRobots(RobotsTxtManager::parse_robots(
                "User-agent: *\nDisallow: /\n",
                "MyBot",
            )),
        );
        assert!(!mgr.is_allowed("http://example.com/x"));
        assert!(mgr.is_allowed("https://example.com/x"));
    }

    #[test]
    fn user_agent_product_token_matches_group() {
        // A crawler identifying as "MyBot/1.0 (+url)" must match the
        // "User-agent: MyBot" group (RFC 9309 product-token matching).
        let txt = "User-agent: MyBot\nDisallow: /private\n";
        let mgr = mgr_with(
            txt,
            "MyBot/1.0 (+https://example.com/bot)",
            "https://example.com",
        );
        assert!(!mgr.is_allowed("https://example.com/private/x"));
        assert!(mgr.is_allowed("https://example.com/open"));
    }

    #[test]
    fn insert_rules_stores_fetched_rules_under_domain() {
        // The fetch_rules/insert_rules split exists so the (slow) network
        // fetch can run without borrowing the manager; verify the two-step
        // path behaves like the one-step fetch_robots.
        let mut mgr = RobotsTxtManager::new("MyBot");
        assert!(!mgr.has_domain("https://example.com"));
        assert_eq!(mgr.user_agent(), "MyBot");

        let rules = FetchedRobots(RobotsTxtManager::parse_robots(
            "User-agent: *\nDisallow: /private\nCrawl-delay: 2\n",
            "MyBot",
        ));
        mgr.insert_rules("https://example.com", rules);

        assert!(mgr.has_domain("https://example.com"));
        assert!(!mgr.is_allowed("https://example.com/private/page"));
        assert!(mgr.is_allowed("https://example.com/public"));
        assert_eq!(mgr.crawl_delay("https://example.com"), Some(2.0));
    }

    #[tokio::test]
    async fn fetch_robots_normalizes_bare_domain_to_https_key() {
        // The .invalid TLD (RFC 2606) can never resolve: the fetch must
        // fall back to allow-all rather than erroring or hanging. The bare
        // domain is stored under its https origin so https-URL lookups hit
        // the entry.
        let mut mgr = RobotsTxtManager::new("MyBot");
        mgr.fetch_robots("unreachable.invalid").await;
        assert!(mgr.has_domain("https://unreachable.invalid"));
        assert!(mgr.is_allowed("https://unreachable.invalid/anything"));
    }
}

#[cfg(test)]
mod proptests {
    //! Property-based verification of the hand-rolled wildcard matcher: for
    //! every generated (pattern, target) pair, `pattern_matches` must agree
    //! with a straightforward regex translation of the robots.txt pattern
    //! grammar (`*` -> `.*`, trailing `$` -> end anchor, everything else
    //! literal, implicit start anchor). The alphabets deliberately include
    //! `*`/`$`/`.` in both roles and a multibyte character, covering the
    //! anchored-last-fragment reservation, consecutive-star collapsing, and
    //! byte-index slicing edge cases.

    use super::pattern_matches;
    use proptest::prelude::*;

    fn regex_oracle(pattern: &str, target: &str) -> bool {
        let (pat, anchored) = match pattern.strip_suffix('$') {
            Some(p) => (p, true),
            None => (pattern, false),
        };
        let mut re = String::from("^");
        for c in pat.chars() {
            if c == '*' {
                re.push_str(".*");
            } else {
                re.push_str(&regex::escape(&c.to_string()));
            }
        }
        if anchored {
            re.push('$');
        }
        regex::Regex::new(&re)
            .expect("oracle regex is valid by construction")
            .is_match(target)
    }

    proptest! {
        #[test]
        fn wildcard_matcher_agrees_with_regex_oracle(
            pattern in "[ab/*.$ä]{0,10}",
            target in "[ab/.$ä]{0,14}",
        ) {
            prop_assert_eq!(
                pattern_matches(&pattern, &target),
                regex_oracle(&pattern, &target),
                "pattern={:?} target={:?}", pattern, target
            );
        }
    }
}