crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
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
676
677
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

use crate::impl_basic_error_conversions;
use crate::infrastructure::oxcache::CacheService;
use crate::utils::retry_policy::RetryPolicy;
use anyhow::Result;
use robotstxt::DefaultMatcher;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use tokio::sync::Mutex;
use url::Url;

use async_trait::async_trait;

/// Robots.txt 检查器错误
#[derive(Error, Debug)]
pub enum RobotsCheckerError {
    #[error("Cache lock acquisition failed: {0}")]
    CacheLockError(String),

    #[error("URL parse failed: {0}")]
    UrlParseError(String),

    #[error("Validation failed: {0}")]
    ValidationError(String),
}

impl_basic_error_conversions!(RobotsCheckerError, ValidationError);

impl From<crate::infrastructure::security::ssrf::SsrfError> for RobotsCheckerError {
    fn from(err: crate::infrastructure::security::ssrf::SsrfError) -> Self {
        RobotsCheckerError::ValidationError(err.to_string())
    }
}

/// Robots.txt缓存统计
#[derive(Default, Clone)]
pub struct CacheStats {
    pub hits: Arc<AtomicU64>,
    pub misses: Arc<AtomicU64>,
}

impl CacheStats {
    /// 获取缓存命中次数
    pub fn hits(&self) -> u64 {
        self.hits.load(Ordering::Relaxed)
    }

    /// 获取缓存未命中次数
    pub fn misses(&self) -> u64 {
        self.misses.load(Ordering::Relaxed)
    }

    /// 记录缓存命中
    pub fn record_hit(&self) {
        self.hits.fetch_add(1, Ordering::Relaxed);
    }

    /// 记录缓存未命中
    pub fn record_miss(&self) {
        self.misses.fetch_add(1, Ordering::Relaxed);
    }
}

/// Robots.txt检查器接口
#[async_trait]
pub trait RobotsCheckerTrait: Send + Sync {
    /// 检查URL是否被允许访问
    async fn is_allowed(&self, url_str: &str, user_agent: &str) -> Result<bool>;
    /// 获取爬取延迟
    async fn get_crawl_delay(&self, url_str: &str, user_agent: &str) -> Result<Option<Duration>>;
}

/// 缓存的Robots.txt内容
#[derive(Clone)]
struct CachedRobots {
    /// 内容(Arc 使缓存命中克隆为 O(1),不在锁内深拷贝 512 KiB)
    content: Arc<str>,

    /// 过期时间
    expires_at: Instant,

    /// 是否为获取失败标记(重试耗尽)。`fail_closed=true` 时命中该标记判禁,
    /// 避免对不可达目标站反复发起 robots.txt 请求。
    fetch_failed: bool,
}

/// robots.txt 获取结果:内容 + 是否为获取失败标记
#[derive(Clone)]
struct FetchedRobots {
    content: Arc<str>,
    fetch_failed: bool,
}

/// 内存缓存上限:条目数与总字节数双限,防恶意站点用 512 KiB robots.txt
/// 打爆进程内存(审查 H3)。
const MAX_MEMORY_CACHE_ENTRIES: usize = 4096;
const MAX_MEMORY_CACHE_BYTES: usize = 16 * 1024 * 1024;

/// robots.txt 内存缓存(条目 + 总字节计数,供 LRU/过期淘汰)。
#[derive(Default)]
struct MemoryCache {
    entries: HashMap<String, CachedRobots>,
    total_bytes: usize,
}

impl MemoryCache {
    fn remove(&mut self, key: &str) -> Option<CachedRobots> {
        let removed = self.entries.remove(key);
        if let Some(entry) = &removed {
            self.total_bytes = self.total_bytes.saturating_sub(entry.content.len());
        }
        removed
    }

    fn insert(&mut self, key: String, value: CachedRobots) {
        // 覆盖旧条目时先归还其字节配额。
        self.remove(&key);
        let incoming = value.content.len();
        if incoming > MAX_MEMORY_CACHE_BYTES {
            return;
        }
        if self.total_bytes + incoming > MAX_MEMORY_CACHE_BYTES
            || self.entries.len() >= MAX_MEMORY_CACHE_ENTRIES
        {
            self.make_room(incoming);
        }
        if self.total_bytes + incoming > MAX_MEMORY_CACHE_BYTES
            || self.entries.len() >= MAX_MEMORY_CACHE_ENTRIES
        {
            return;
        }
        self.total_bytes += incoming;
        self.entries.insert(key, value);
    }

    /// 一次扫描 + 排序后按最早过期顺序批量淘汰,避免每次只删一条、
    /// 反复全表扫描造成全局锁内毫秒级停顿(审查 M1)。
    fn make_room(&mut self, incoming: usize) {
        let mut candidates: Vec<(Instant, String)> = self
            .entries
            .iter()
            .map(|(key, entry)| (entry.expires_at, key.clone()))
            .collect();
        candidates.sort_by_key(|(expires_at, _)| *expires_at);
        for (_expires_at, key) in candidates {
            if self.total_bytes + incoming <= MAX_MEMORY_CACHE_BYTES
                && self.entries.len() < MAX_MEMORY_CACHE_ENTRIES
            {
                break;
            }
            self.remove(&key);
        }
    }
}

/// robots.txt 获取失败标记的缓存 TTL(短 TTL,防止对不可达目标站打爆)
const ROBOTS_FAILURE_CACHE_TTL: Duration = Duration::from_secs(60);

/// robots.txt 响应体上限(512 KiB;正常 robots.txt 远小于此值)
const MAX_ROBOTS_BODY_BYTES: usize = 512 * 1024;

/// 预分配上限(64 KiB):Content-Length 是不可信响应头,钳制初始容量,
/// 超出部分按 chunk 正常扩容(仍受 MAX_ROBOTS_BODY_BYTES 封顶)。
const MAX_ROBOTS_PREALLOC_BYTES: usize = 64 * 1024;

/// Robots.txt检查器
#[derive(Clone)]
pub struct RobotsChecker {
    /// robots.txt 抓取器(生产=直连 reqwest;测试可注入可控实现,
    /// 不走抓取引擎反爬链)
    fetcher: Arc<dyn RobotsFetcher>,

    /// robots.txt 匹配与抓取使用的 User-Agent(可配置,见 RobotsSettings)
    user_agent: String,

    /// 内存缓存(条目/字节双限)
    memory_cache: Arc<Mutex<MemoryCache>>,

    /// 缓存服务(可选,用于持久化缓存)
    cache_service: Option<Arc<dyn CacheService>>,

    /// robots.txt 缓存 TTL
    cache_ttl: Duration,

    /// 重试策略
    retry_policy: RetryPolicy,

    /// robots.txt 获取失败(重试耗尽)时是否判禁(fail-closed)。
    /// 默认 `false` 维持 fail-open 放行(见 `RobotsSettings::fail_closed`)。
    fail_closed: bool,

    /// 缓存统计
    cache_stats: Arc<CacheStats>,
}

#[async_trait]
impl RobotsCheckerTrait for RobotsChecker {
    async fn is_allowed(&self, url_str: &str, user_agent: &str) -> Result<bool> {
        let fetched = self.get_robots_content(url_str).await?;
        if fetched.fetch_failed && self.fail_closed {
            return Ok(false);
        }
        let url = Url::parse(url_str)?;
        let mut matcher = DefaultMatcher::default();
        // F6:匹配目标携带 query——`Disallow: /*?s=` 类规则以 query 为
        // 匹配特征,只传 path 会让此类规则永不命中
        let path_and_query = match url.query() {
            Some(q) => format!("{}?{}", url.path(), q),
            None => url.path().to_string(),
        };
        Ok(matcher.one_agent_allowed_by_robots(&fetched.content, user_agent, &path_and_query))
    }

    async fn get_crawl_delay(&self, url_str: &str, user_agent: &str) -> Result<Option<Duration>> {
        let fetched = self.get_robots_content(url_str).await?;
        Ok(self.parse_crawl_delay(&fetched.content, user_agent))
    }
}

impl RobotsChecker {
    /// 创建新的Robots检查器实例(通过依赖注入)
    ///
    /// # Arguments
    ///
    /// * `http_client` - HTTP 客户端(通过依赖注入)
    /// * `cache_service` - 缓存服务(可选,用于持久化缓存)
    /// * `cache_stats` - 缓存统计(可选,用于追踪缓存命中率)
    ///
    /// # Returns
    ///
    /// 返回新的Robots检查器实例
    pub fn new(
        http_client: Arc<reqwest::Client>,
        settings: &crate::config::settings::RobotsSettings,
        cache_service: Option<Arc<dyn CacheService>>,
        cache_stats: Option<Arc<CacheStats>>,
    ) -> Self {
        Self {
            fetcher: Arc::new(HttpRobotsFetcher {
                client: http_client,
            }),
            user_agent: settings.user_agent.clone(),
            memory_cache: Arc::new(Mutex::new(MemoryCache::default())),
            cache_service,
            cache_ttl: Duration::from_secs(settings.cache_ttl_secs),
            retry_policy: RetryPolicy {
                max_retries: settings.fetch_max_retries,
                initial_backoff: Duration::from_secs(settings.fetch_initial_backoff_secs),
                max_backoff: Duration::from_secs(settings.fetch_max_backoff_secs),
                ..Default::default()
            },
            fail_closed: settings.fail_closed,
            cache_stats: cache_stats.unwrap_or_else(|| Arc::new(CacheStats::default())),
        }
    }

    /// robots.txt 匹配与抓取使用的 User-Agent(来自 `robots.user_agent` 配置)
    pub fn user_agent(&self) -> &str {
        &self.user_agent
    }

    /// 获取Robots.txt内容(带缓存)
    ///
    /// 返回内容与是否为获取失败标记。获取失败(重试耗尽)时:
    /// - `fail_closed=true`:内容置空、`fetch_failed=true`,并按短 TTL
    ///   (60s)写入内存缓存防打爆;不写入 `cache_service`(其 TTL 长,
    ///   短时失败不应被长期缓存否决)。
    /// - `fail_closed=false`:维持现行 fail-open——空内容按正常 TTL 缓存。
    async fn get_robots_content(&self, url_str: &str) -> Result<FetchedRobots, RobotsCheckerError> {
        let robots_url = build_robots_url(url_str)?;

        // 1. Check memory cache
        if let Some(hit) = self.try_memory_cache(&robots_url).await {
            return Ok(hit);
        }

        self.cache_stats.record_miss();

        // 2. Check cache service(仅存成功获取的内容,fetch_failed 恒为 false)
        let cache_key = format!("robots_cache:{}", robots_url);
        if let Some(hit) = self.try_cache_service(&cache_key, &robots_url).await {
            return Ok(hit);
        }

        // SSRF protection
        crate::infrastructure::security::ssrf::validate_url(&robots_url).await?;

        // 3. Fetch robots.txt with retry
        match self.fetch_robots_text(&robots_url).await {
            RobotsFetch::FailClosed | RobotsFetch::Rejected => Ok(FetchedRobots {
                content: Arc::from(""),
                fetch_failed: true,
            }),
            RobotsFetch::Content(content) => {
                let content: Arc<str> = Arc::from(content);
                self.store_fetched_content(&robots_url, &cache_key, &content)
                    .await;

                Ok(FetchedRobots {
                    content,
                    fetch_failed: false,
                })
            }
        }
    }

    /// 成功抓取后的持久化:按正常 TTL 写内存缓存 + 24h 写持久缓存
    async fn store_fetched_content(&self, robots_url: &str, cache_key: &str, content: &Arc<str>) {
        // 4. Update memory cache
        self.store_memory_cache(robots_url, content).await;

        // 5. Update cache service
        if let Some(ref cache_service) = self.cache_service {
            let _ = cache_service.set(cache_key, content.as_ref(), 86400).await;
            // Cache for 24 hours
        }
    }

    /// 内存缓存查询:命中未过期 → 记 hit 并返回内容;过期条目移除
    async fn try_memory_cache(&self, robots_url: &str) -> Option<FetchedRobots> {
        let mut cache = self.memory_cache.lock().await;
        if let Some(cached) = cache.entries.get(robots_url) {
            if cached.expires_at > Instant::now() {
                let fetched = FetchedRobots {
                    content: Arc::clone(&cached.content),
                    fetch_failed: cached.fetch_failed,
                };
                drop(cache);
                self.cache_stats.record_hit();
                return Some(fetched);
            }
            cache.remove(robots_url);
        }
        None
    }

    /// 持久缓存查询:命中 → 回填内存缓存(正常 TTL)并记 hit
    async fn try_cache_service(&self, cache_key: &str, robots_url: &str) -> Option<FetchedRobots> {
        if let Some(ref cache_service) = self.cache_service {
            if let Ok(Some(content)) = cache_service.get(cache_key).await {
                let content: Arc<str> = Arc::from(content.as_str());
                let mut cache = self.memory_cache.lock().await;
                cache.insert(
                    robots_url.to_string(),
                    CachedRobots {
                        content: Arc::clone(&content),
                        expires_at: Instant::now() + self.cache_ttl,
                        fetch_failed: false,
                    },
                );
                drop(cache);
                self.cache_stats.record_hit();
                return Some(FetchedRobots {
                    content,
                    fetch_failed: false,
                });
            }
        }
        None
    }

    /// 按 retry_policy 重试抓取 robots.txt;含 fail-closed / fail-open 降级语义
    async fn fetch_robots_text(&self, robots_url: &str) -> RobotsFetch {
        let mut attempt = 0;
        let mut last_error = None;
        let mut rejected = false;

        while attempt < self.retry_policy.max_retries {
            attempt += 1;

            match self.fetch_robots_once(robots_url).await {
                RobotsFetchAttempt::Content(content) => {
                    return RobotsFetch::Content(content);
                }
                RobotsFetchAttempt::Retryable(err) => {
                    last_error = Some(err);
                }
                RobotsFetchAttempt::Rejected(err) => {
                    log::warn!("robots fetch rejected (no retry): {}", err);
                    last_error = Some(err);
                    rejected = true;
                    break;
                }
            }

            if attempt < self.retry_policy.max_retries {
                let backoff = self.retry_policy.calculate_backoff(attempt);
                tokio::time::sleep(backoff).await;
            }
        }

        if rejected {
            // 不可重试拒绝:无论 fail_closed 与否都写短 TTL 失败标记,
            // 避免空内容被持久缓存 24h(审查 L2)。
            self.store_failure_marker(robots_url).await;
            return RobotsFetch::Rejected;
        }
        match last_error {
            Some(err) => self.handle_robots_fetch_error(robots_url, err).await,
            // max_retries=0:从未发起尝试,等价于历史空内容返回
            None => RobotsFetch::Content(String::new()),
        }
    }

    /// 单次抓取尝试:成功/404/其余非 5xx 结束本轮(内容或空串);
    /// 5xx 与传输错误交给重试循环
    async fn fetch_robots_once(&self, robots_url: &str) -> RobotsFetchAttempt {
        // D5 修复:robots.txt 直连 HTTP,不走抓取引擎链。此前经 EngineRouter
        // 抓取,极短纯文本(如 `User-agent: *\nDisallow: /`)被反爬启发式
        // StructuralBlock 误判为「JS 空壳」并强制 needs_js,无 JS 引擎环境下
        // 抓取必败 → fail-open 放行全站 Disallow(E2E SB-17 实测)。SSRF 校验
        // 仍在调用方(get_robots_content)以 validate_url 前置执行。
        self.fetcher.fetch(robots_url, &self.user_agent).await
    }

    /// 写短 TTL 失败标记(60s),防止对不可达/非法目标站打爆。
    async fn store_failure_marker(&self, robots_url: &str) {
        let mut cache = self.memory_cache.lock().await;
        cache.insert(
            robots_url.to_string(),
            CachedRobots {
                content: Arc::from(""),
                expires_at: Instant::now() + ROBOTS_FAILURE_CACHE_TTL,
                fetch_failed: true,
            },
        );
    }

    /// 重试耗尽后的降级:fail-closed 写短 TTL 失败标记并判禁;fail-open 告警并放行空内容
    async fn handle_robots_fetch_error(&self, robots_url: &str, err: anyhow::Error) -> RobotsFetch {
        if self.fail_closed {
            log::warn!(
                "{}",
                crate::i18n::tr_log_args(
                    "robots-fetch-fail-closed",
                    &[("url", robots_url.to_string()), ("error", err.to_string()),],
                )
            );
            self.store_failure_marker(robots_url).await;
            return RobotsFetch::FailClosed;
        }
        log::warn!("Failed to fetch robots.txt from {}: {}", robots_url, err);
        // Default to empty content on persistent error
        RobotsFetch::Content(String::new())
    }

    /// 按正常 TTL 写入内存缓存(成功获取 / fail-open 降级共用)
    async fn store_memory_cache(&self, robots_url: &str, content: &Arc<str>) {
        let mut cache = self.memory_cache.lock().await;
        cache.insert(
            robots_url.to_string(),
            CachedRobots {
                content: Arc::clone(content),
                expires_at: Instant::now() + self.cache_ttl, // Cache for 1 hour
                fetch_failed: false,
            },
        );
    }

    /// 解析Crawl-delay指令
    fn parse_crawl_delay(&self, content: &str, user_agent: &str) -> Option<Duration> {
        // 简单的解析逻辑:查找适用于该 User-Agent 的 Crawl-delay
        // 注意:这是一个简化的实现,不完全符合 RFC 规范,但足以处理大多数情况
        // 逻辑:
        // 1. 找到匹配的 User-agent 块
        // 2. 在块内查找 Crawl-delay

        let mut current_agent_matched = false;
        let mut delay: Option<f64> = None;
        let mut specific_agent_found = false;

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            match classify_robots_line(line) {
                RobotsLine::UserAgent(agent) => {
                    if agent == "*" {
                        current_agent_matched = !specific_agent_found;
                    } else if user_agent.to_lowercase().contains(&agent.to_lowercase()) {
                        current_agent_matched = true;
                        specific_agent_found = true;
                        // Reset delay if we found a more specific agent
                        delay = None;
                    } else {
                        current_agent_matched = false;
                    }
                }
                RobotsLine::CrawlDelay(raw) if current_agent_matched => {
                    apply_crawl_delay_value(&mut delay, raw);
                }
                RobotsLine::Other | RobotsLine::CrawlDelay(_) => {}
            }
        }

        delay.map(Duration::from_secs_f64)
    }

    /// 获取缓存统计信息
    pub fn get_cache_stats(&self) -> (u64, u64) {
        (self.cache_stats.hits(), self.cache_stats.misses())
    }
}

/// 从目标 URL 构造 robots.txt URL(保留 scheme 与显式端口,缺省端口取知名默认)
fn build_robots_url(url_str: &str) -> Result<String, RobotsCheckerError> {
    let url = Url::parse(url_str).map_err(|e| RobotsCheckerError::UrlParseError(e.to_string()))?;
    let host = url
        .host_str()
        .ok_or_else(|| RobotsCheckerError::UrlParseError("Invalid URL: no host".to_string()))?;
    let scheme = url.scheme();
    let port = url.port_or_known_default().unwrap_or(80);

    Ok(format!("{}://{}:{}/robots.txt", scheme, host, port))
}

/// 重试抓取 robots.txt 的分类结果
enum RobotsFetch {
    /// fail_closed=true 且重试耗尽:内容置空、失败标记已按短 TTL 写入内存缓存
    FailClosed,
    /// 抓取成功 / 404 / fail-open 降级(内容可能为空串)
    Content(String),
    /// 不可重试拒绝(超大响应体 / 私网地址):写短 TTL 失败标记,
    /// 不写 24h 持久缓存(避免恶意站点污染规则缓存)。
    Rejected,
}

/// 单次 robots.txt 抓取尝试的结果(重试循环据此判定是否继续)
pub enum RobotsFetchAttempt {
    /// 已结束本轮(成功内容 / 404 空串 / 其余非 5xx 空串)
    Content(String),
    /// 5xx 或传输错误:按 retry_policy 重试
    Retryable(anyhow::Error),
    /// 不可重试的拒绝(超大响应体 / 解析到内网地址):不再重试,避免
    /// 对同一非法目标反复请求;fail_closed=true 时按获取失败判禁。
    Rejected(anyhow::Error),
}

/// robots.txt 抓取器抽象。
///
/// 生产实现 [`HttpRobotsFetcher`] 直连 reqwest;测试注入可控实现
/// (本仓库约定:真实 trait 实现 + 测试专用行为,不引入 mock 库)。
#[async_trait]
pub(crate) trait RobotsFetcher: Send + Sync {
    /// 发起一次抓取尝试(不做重试与缓存)。
    async fn fetch(&self, robots_url: &str, user_agent: &str) -> RobotsFetchAttempt;
}

/// 生产抓取器:直连 HTTP(不走抓取引擎反爬链)。
struct HttpRobotsFetcher {
    client: Arc<reqwest::Client>,
}

#[async_trait]
impl RobotsFetcher for HttpRobotsFetcher {
    async fn fetch(&self, robots_url: &str, user_agent: &str) -> RobotsFetchAttempt {
        let response = self
            .client
            .get(robots_url)
            .header(reqwest::header::USER_AGENT, user_agent.to_string())
            .timeout(std::time::Duration::from_secs(5))
            .send()
            .await;
        match response {
            Ok(mut resp) => {
                // 审查 L5:DNS 重绑定缓解——连接建立后校验对端地址,
                // 命中内网/回环立即按可重试失败处理,不读取响应体。
                match resp.remote_addr() {
                    Some(addr)
                        if crate::infrastructure::security::ssrf::is_private_ip(addr.ip()) =>
                    {
                        return RobotsFetchAttempt::Rejected(anyhow::anyhow!(
                            "Robots fetch resolved to private/reserved address: {}",
                            addr.ip()
                        ));
                    }
                    Some(_) => {}
                    None => {
                        // HTTP/2 连接池等场景可能取不到对端地址;静态 DNS 校验
                        // 与逐跳重定向策略仍在,记录 debug 后继续。
                        log::debug!(
                            "robots fetch remote_addr unavailable; relying on DNS validation"
                        );
                    }
                }
                let status = resp.status().as_u16();
                if (200..300).contains(&status) {
                    // 审查 M2:robots.txt 响应体限长(防恶意站点大体积响应);
                    // 按 Content-Length 预分配,但钳到 64 KiB——该头不可信,
                    // 伪造大值不应触发大块分配(终验 INFO 项)。
                    let capacity =
                        resp.content_length()
                            .unwrap_or(0)
                            .min(MAX_ROBOTS_PREALLOC_BYTES as u64) as usize;
                    let mut body: Vec<u8> = Vec::with_capacity(capacity);
                    loop {
                        match resp.chunk().await {
                            Ok(Some(chunk)) => {
                                if body.len() + chunk.len() > MAX_ROBOTS_BODY_BYTES {
                                    return RobotsFetchAttempt::Rejected(anyhow::anyhow!(
                                        "robots.txt exceeds {} bytes",
                                        MAX_ROBOTS_BODY_BYTES
                                    ));
                                }
                                body.extend_from_slice(&chunk);
                            }
                            Ok(None) => break,
                            Err(e) => {
                                return RobotsFetchAttempt::Retryable(anyhow::anyhow!(
                                    "Read body failed: {e}"
                                ));
                            }
                        }
                    }
                    // 合法 UTF-8 直接接管 Vec,避免 from_utf8_lossy 的整串复制
                    RobotsFetchAttempt::Content(match String::from_utf8(body) {
                        Ok(text) => text,
                        Err(e) => String::from_utf8_lossy(e.as_bytes()).into_owned(),
                    })
                } else if status == 404 {
                    RobotsFetchAttempt::Content(String::new())
                } else if status >= 500 {
                    RobotsFetchAttempt::Retryable(anyhow::anyhow!("Server error: {status}"))
                } else {
                    RobotsFetchAttempt::Content(String::new())
                }
            }
            Err(e) => RobotsFetchAttempt::Retryable(anyhow::anyhow!("Request failed: {e}")),
        }
    }
}

/// robots.txt 行分类(指令前缀大小写不敏感)
enum RobotsLine<'a> {
    /// `user-agent:` 值(已 trim)
    UserAgent(&'a str),
    /// `crawl-delay:` 值(已 trim)
    CrawlDelay(&'a str),
    /// 其余行
    Other,
}

/// 按指令前缀分类一行 robots.txt(前缀比较大小写不敏感)
fn classify_robots_line(line: &str) -> RobotsLine<'_> {
    let lower_line = line.to_lowercase();
    if lower_line.starts_with("user-agent:") {
        RobotsLine::UserAgent(line[11..].trim())
    } else if lower_line.starts_with("crawl-delay:") {
        RobotsLine::CrawlDelay(line[12..].trim())
    } else {
        RobotsLine::Other
    }
}

/// 解析并应用 Crawl-delay 数值:负数 / inf / nan 视为无效,delay 维持原值
fn apply_crawl_delay_value(delay: &mut Option<f64>, raw: &str) {
    if let Ok(d) = raw.parse::<f64>() {
        // CWE-626:`Duration::from_secs_f64` 对负数/inf/nan 会 panic,
        // 恶意 robots.txt 不得中断爬取——视为无效指令,交由调用方默认策略
        if d.is_finite() && d >= 0.0 {
            *delay = Some(d);
        } else {
            log::warn!("Ignoring non-finite or negative Crawl-delay value: {}", d);
        }
    }
}

#[cfg(test)]
#[path = "tests/robots/mod.rs"]
mod tests;