halldyll-robots 0.1.0

robots.txt parser and compliance for halldyll scraper
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
//! Types - Core types for robots.txt handling (RFC 9309)

use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use url::Url;

/// Cache key for robots.txt (scheme + authority)
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RobotsCacheKey {
    /// URL scheme (http/https)
    pub scheme: String,
    /// Authority (host + optional port)
    pub authority: String,
}

/// Request-rate directive (non-standard but common)
/// Format: "requests/seconds" e.g., "1/10" means 1 request per 10 seconds
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct RequestRate {
    /// Number of requests allowed
    pub requests: u32,
    /// Time period in seconds
    pub seconds: u32,
}

impl RequestRate {
    /// Create a new request rate
    pub fn new(requests: u32, seconds: u32) -> Self {
        Self { requests, seconds }
    }

    /// Get the minimum delay between requests in seconds
    pub fn delay_seconds(&self) -> f64 {
        if self.requests == 0 {
            f64::MAX
        } else {
            self.seconds as f64 / self.requests as f64
        }
    }

    /// Get the delay as Duration
    pub fn delay(&self) -> Duration {
        Duration::from_secs_f64(self.delay_seconds())
    }
}

impl RobotsCacheKey {
    /// Create a new cache key from a URL
    pub fn from_url(url: &Url) -> Option<Self> {
        let host = url.host_str()?;
        let authority = match url.port() {
            Some(port) => format!("{}:{}", host, port),
            None => host.to_string(),
        };
        Some(Self {
            scheme: url.scheme().to_lowercase(),
            authority: authority.to_lowercase(),
        })
    }

    /// Get the robots.txt URL for this key
    pub fn robots_url(&self) -> String {
        format!("{}://{}/robots.txt", self.scheme, self.authority)
    }
}

/// Fetch status distinguishing Unavailable vs Unreachable (RFC 9309)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FetchStatus {
    /// Successfully fetched and parsed
    Success,
    /// Not modified (304) - use cached version
    NotModified,
    /// Unavailable (4xx) - crawler can access everything
    Unavailable {
        /// HTTP status code
        status_code: u16,
    },
    /// Unreachable (5xx, network error) - assume DISALLOW all
    Unreachable {
        /// Error description
        reason: String,
    },
    /// Protected (401/403) - safe mode treats as deny
    Protected {
        /// HTTP status code
        status_code: u16,
    },
}

impl FetchStatus {
    /// Check if this status allows all paths (RFC 9309: Unavailable)
    pub fn allows_all(&self) -> bool {
        matches!(self, FetchStatus::Unavailable { .. })
    }

    /// Check if this status denies all paths (RFC 9309: Unreachable)
    pub fn denies_all(&self) -> bool {
        matches!(self, FetchStatus::Unreachable { .. } | FetchStatus::Protected { .. })
    }

    /// Check if this is a not-modified response (304)
    pub fn is_not_modified(&self) -> bool {
        matches!(self, FetchStatus::NotModified)
    }
}

/// A single rule (Allow or Disallow)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Rule {
    /// Rule kind
    pub kind: RuleKind,
    /// Original pattern from robots.txt
    pub pattern: String,
    /// Compiled pattern for matching (with wildcards expanded)
    #[serde(skip)]
    pub compiled: Option<CompiledPattern>,
}

impl Rule {
    /// Create a new rule
    pub fn new(kind: RuleKind, pattern: String) -> Self {
        let compiled = CompiledPattern::compile(&pattern);
        Self {
            kind,
            pattern,
            compiled: Some(compiled),
        }
    }

    /// Check if this rule matches a path
    pub fn matches(&self, path: &str) -> bool {
        match &self.compiled {
            Some(compiled) => compiled.matches(path),
            None => CompiledPattern::compile(&self.pattern).matches(path),
        }
    }

    /// Get the specificity (length) of this rule's pattern
    pub fn specificity(&self) -> usize {
        // Count actual characters, not wildcards
        self.pattern.chars().filter(|&c| c != '*').count()
    }
}

/// Rule kind
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RuleKind {
    /// Allow crawling
    Allow,
    /// Disallow crawling
    Disallow,
}

/// Compiled pattern for efficient matching
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompiledPattern {
    /// Pattern segments (between wildcards)
    segments: Vec<PatternSegment>,
    /// Whether pattern ends with $
    anchored_end: bool,
}

/// A segment of the pattern
#[derive(Debug, Clone, PartialEq, Eq)]
enum PatternSegment {
    /// Literal text to match
    Literal(String),
    /// Wildcard (matches 0+ characters)
    Wildcard,
}

impl CompiledPattern {
    /// Compile a pattern string
    pub fn compile(pattern: &str) -> Self {
        let anchored_end = pattern.ends_with('$');
        let pattern = if anchored_end {
            &pattern[..pattern.len() - 1]
        } else {
            pattern
        };

        let mut segments = Vec::new();
        let mut current = String::new();

        for c in pattern.chars() {
            if c == '*' {
                if !current.is_empty() {
                    segments.push(PatternSegment::Literal(current.clone()));
                    current.clear();
                }
                // Collapse consecutive wildcards
                if !matches!(segments.last(), Some(PatternSegment::Wildcard)) {
                    segments.push(PatternSegment::Wildcard);
                }
            } else {
                current.push(c);
            }
        }

        if !current.is_empty() {
            segments.push(PatternSegment::Literal(current));
        }

        Self {
            segments,
            anchored_end,
        }
    }

    /// Check if this pattern matches a path
    pub fn matches(&self, path: &str) -> bool {
        self.matches_recursive(path, 0)
    }

    fn matches_recursive(&self, remaining: &str, segment_idx: usize) -> bool {
        // Base case: no more segments
        if segment_idx >= self.segments.len() {
            return if self.anchored_end {
                remaining.is_empty()
            } else {
                true
            };
        }

        match &self.segments[segment_idx] {
            PatternSegment::Literal(lit) => {
                if remaining.starts_with(lit.as_str()) {
                    self.matches_recursive(&remaining[lit.len()..], segment_idx + 1)
                } else {
                    false
                }
            }
            PatternSegment::Wildcard => {
                // Try matching at every position
                if segment_idx + 1 >= self.segments.len() {
                    // Wildcard at end: matches everything
                    if self.anchored_end {
                        // Anchored: only match if this is the end
                        remaining.is_empty() || self.matches_recursive(remaining, segment_idx + 1)
                    } else {
                        true
                    }
                } else {
                    // Try matching the rest at each position
                    for i in 0..=remaining.len() {
                        if self.matches_recursive(&remaining[i..], segment_idx + 1) {
                            return true;
                        }
                    }
                    false
                }
            }
        }
    }
}

/// A group of rules for specific user-agents
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Group {
    /// User-agent tokens this group applies to
    pub user_agents: Vec<String>,
    /// Rules in this group (in order)
    pub rules: Vec<Rule>,
    /// Crawl-delay directive (optional, in seconds)
    pub crawl_delay: Option<f64>,
    /// Request-rate directive (optional, non-standard)
    pub request_rate: Option<RequestRate>,
}

impl Group {
    /// Check if this group matches a user-agent token
    pub fn matches_user_agent(&self, token: &str) -> bool {
        let token_lower = token.to_lowercase();
        self.user_agents.iter().any(|ua| {
            let ua_lower = ua.to_lowercase();
            // Prefix match: "Googlebot" matches "Googlebot-Image"
            token_lower.starts_with(&ua_lower) || ua_lower == "*"
        })
    }

    /// Check if this is the wildcard group
    pub fn is_wildcard(&self) -> bool {
        self.user_agents.iter().any(|ua| ua == "*")
    }
}

/// Complete robots.txt policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RobotsPolicy {
    /// When the robots.txt was fetched (Unix timestamp millis)
    pub fetched_at_ms: u64,
    /// When this policy expires (Unix timestamp millis)
    pub expires_at_ms: u64,
    /// Fetch status
    pub fetch_status: FetchStatus,
    /// Parsed groups
    pub groups: Vec<Group>,
    /// Sitemap URLs found
    pub sitemaps: Vec<String>,
    /// Original robots.txt size in bytes
    pub content_size: usize,
    /// ETag header for conditional GET
    #[serde(skip_serializing_if = "Option::is_none")]
    pub etag: Option<String>,
    /// Last-Modified header for conditional GET
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_modified: Option<String>,
}

impl RobotsPolicy {
    /// Get current timestamp in milliseconds
    fn now_millis() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64
    }

    /// Create a policy for unavailable robots.txt (allow all)
    pub fn unavailable(status_code: u16, ttl: Duration) -> Self {
        let now = Self::now_millis();
        Self {
            fetched_at_ms: now,
            expires_at_ms: now + ttl.as_millis() as u64,
            fetch_status: FetchStatus::Unavailable { status_code },
            groups: Vec::new(),
            sitemaps: Vec::new(),
            content_size: 0,
            etag: None,
            last_modified: None,
        }
    }

    /// Create a policy for unreachable robots.txt (deny all)
    pub fn unreachable(reason: String, ttl: Duration) -> Self {
        let now = Self::now_millis();
        Self {
            fetched_at_ms: now,
            expires_at_ms: now + ttl.as_millis() as u64,
            fetch_status: FetchStatus::Unreachable { reason },
            groups: Vec::new(),
            sitemaps: Vec::new(),
            content_size: 0,
            etag: None,
            last_modified: None,
        }
    }

    /// Create a policy for protected robots.txt (deny all in safe mode)
    pub fn protected(status_code: u16, ttl: Duration) -> Self {
        let now = Self::now_millis();
        Self {
            fetched_at_ms: now,
            expires_at_ms: now + ttl.as_millis() as u64,
            fetch_status: FetchStatus::Protected { status_code },
            groups: Vec::new(),
            sitemaps: Vec::new(),
            content_size: 0,
            etag: None,
            last_modified: None,
        }
    }

    /// Create a policy indicating 304 Not Modified
    /// The caller should extend the existing cached policy's TTL
    pub fn not_modified(ttl: Duration) -> Self {
        let now = Self::now_millis();
        Self {
            fetched_at_ms: now,
            expires_at_ms: now + ttl.as_millis() as u64,
            fetch_status: FetchStatus::NotModified,
            groups: Vec::new(),
            sitemaps: Vec::new(),
            content_size: 0,
            etag: None,
            last_modified: None,
        }
    }

    /// Extend the expiration time by the given TTL
    pub fn extend_ttl(&mut self, ttl: Duration) {
        self.expires_at_ms = Self::now_millis() + ttl.as_millis() as u64;
    }

    /// Check if this policy has expired
    pub fn is_expired(&self) -> bool {
        Self::now_millis() > self.expires_at_ms
    }

    /// Get time until expiration
    pub fn ttl(&self) -> Duration {
        let now = Self::now_millis();
        if self.expires_at_ms > now {
            Duration::from_millis(self.expires_at_ms - now)
        } else {
            Duration::ZERO
        }
    }
}

/// Decision returned by is_allowed
#[derive(Debug, Clone)]
pub struct Decision {
    /// Whether access is allowed
    pub allowed: bool,
    /// The rule that matched (if any)
    pub matched_rule: Option<Rule>,
    /// Reason for the decision
    pub reason: DecisionReason,
}

impl Decision {
    /// Create an allowed decision
    pub fn allow(reason: DecisionReason) -> Self {
        Self {
            allowed: true,
            matched_rule: None,
            reason,
        }
    }

    /// Create a denied decision
    pub fn deny(reason: DecisionReason) -> Self {
        Self {
            allowed: false,
            matched_rule: None,
            reason,
        }
    }

    /// Create a decision with a matched rule
    pub fn with_rule(mut self, rule: Rule) -> Self {
        self.matched_rule = Some(rule);
        self
    }
}

/// Reason for the decision
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecisionReason {
    /// Robots.txt unavailable (4xx), allow all
    RobotsUnavailable,
    /// Robots.txt unreachable (5xx/network), deny all
    RobotsUnreachable,
    /// Robots.txt protected (401/403 in safe mode), deny all
    RobotsProtected,
    /// No matching rule found, default allow
    NoMatchingRule,
    /// Matched an allow rule
    AllowRuleMatched,
    /// Matched a disallow rule
    DisallowRuleMatched,
    /// Path is /robots.txt (always allowed)
    RobotsTxtPath,
    /// Robots.txt respect is disabled
    RobotsDisabled,
}

/// Effective rules for a specific user-agent (after merging groups)
#[derive(Debug, Clone)]
pub struct EffectiveRules {
    /// Merged rules from all matching groups
    pub rules: Vec<Rule>,
    /// Crawl delay (from first group that specifies it)
    pub crawl_delay: Option<f64>,
    /// Request rate (from first group that specifies it)
    pub request_rate: Option<RequestRate>,
    /// Which user-agents were matched
    pub matched_agents: Vec<String>,
}

impl EffectiveRules {
    /// Create empty effective rules
    pub fn empty() -> Self {
        Self {
            rules: Vec::new(),
            crawl_delay: None,
            request_rate: None,
            matched_agents: Vec::new(),
        }
    }

    /// Get the effective delay between requests
    /// Uses crawl_delay if set, otherwise request_rate, otherwise None
    pub fn effective_delay(&self) -> Option<Duration> {
        if let Some(delay) = self.crawl_delay {
            return Some(Duration::from_secs_f64(delay));
        }
        if let Some(rate) = self.request_rate {
            return Some(rate.delay());
        }
        None
    }
}

/// Configuration for robots.txt handling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RobotsConfig {
    /// User-agent token to identify as
    pub user_agent: String,
    /// Cache TTL in seconds (max 24 hours per RFC)
    pub cache_ttl_secs: u64,
    /// Whether to respect robots.txt
    pub respect_robots: bool,
    /// Default crawl delay in milliseconds
    pub default_crawl_delay_ms: u64,
    /// Maximum robots.txt size in bytes (min 500 KiB per RFC)
    pub max_robots_size: usize,
    /// Maximum redirects to follow
    pub max_redirects: u32,
    /// Fetch timeout in seconds
    pub fetch_timeout_secs: u64,
    /// Use safe mode (treat 401/403 as deny)
    pub safe_mode: bool,
}

impl Default for RobotsConfig {
    fn default() -> Self {
        Self {
            user_agent: "HalldyllBot/1.0".to_string(),
            cache_ttl_secs: 3600, // 1 hour
            respect_robots: true,
            default_crawl_delay_ms: 100,
            max_robots_size: 512 * 1024, // 512 KiB (RFC minimum is 500 KiB)
            max_redirects: 5,
            fetch_timeout_secs: 10,
            safe_mode: true, // Safer default for production
        }
    }
}

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

    #[test]
    fn test_cache_key_from_url() {
        let url = Url::parse("https://example.com:8080/path").unwrap();
        let key = RobotsCacheKey::from_url(&url).unwrap();
        assert_eq!(key.scheme, "https");
        assert_eq!(key.authority, "example.com:8080");
        assert_eq!(key.robots_url(), "https://example.com:8080/robots.txt");
    }

    #[test]
    fn test_compiled_pattern_literal() {
        let pattern = CompiledPattern::compile("/admin/");
        assert!(pattern.matches("/admin/"));
        assert!(pattern.matches("/admin/users"));
        assert!(!pattern.matches("/administrator"));
    }

    #[test]
    fn test_compiled_pattern_wildcard() {
        let pattern = CompiledPattern::compile("/api/*/users");
        assert!(pattern.matches("/api/v1/users"));
        assert!(pattern.matches("/api/v2/users"));
        assert!(pattern.matches("/api//users"));
        assert!(!pattern.matches("/api/users"));
    }

    #[test]
    fn test_compiled_pattern_anchored() {
        let pattern = CompiledPattern::compile("/exact$");
        assert!(pattern.matches("/exact"));
        assert!(!pattern.matches("/exact/"));
        assert!(!pattern.matches("/exact/more"));
    }

    #[test]
    fn test_compiled_pattern_complex() {
        let pattern = CompiledPattern::compile("/*.php$");
        assert!(pattern.matches("/index.php"));
        assert!(pattern.matches("/admin/login.php"));
        assert!(!pattern.matches("/index.php5"));
        assert!(!pattern.matches("/index.php/extra"));
    }

    #[test]
    fn test_rule_specificity() {
        let rule1 = Rule::new(RuleKind::Disallow, "/admin".to_string());
        let rule2 = Rule::new(RuleKind::Allow, "/admin/public".to_string());
        assert!(rule2.specificity() > rule1.specificity());
    }

    #[test]
    fn test_group_matches_user_agent() {
        let group = Group {
            user_agents: vec!["Googlebot".to_string(), "Bingbot".to_string()],
            rules: vec![],
            crawl_delay: None,
            request_rate: None,
        };
        assert!(group.matches_user_agent("Googlebot"));
        assert!(group.matches_user_agent("googlebot")); // case insensitive
        assert!(group.matches_user_agent("Googlebot-Image")); // prefix match
        assert!(!group.matches_user_agent("Yandexbot"));
    }

    #[test]
    fn test_request_rate() {
        let rate = RequestRate::new(1, 10);
        assert_eq!(rate.requests, 1);
        assert_eq!(rate.seconds, 10);
        assert!((rate.delay_seconds() - 10.0).abs() < 0.001);
        assert_eq!(rate.delay(), Duration::from_secs(10));

        let rate2 = RequestRate::new(2, 10);
        assert!((rate2.delay_seconds() - 5.0).abs() < 0.001);
    }

    #[test]
    fn test_effective_rules_delay() {
        let mut rules = EffectiveRules::empty();
        assert!(rules.effective_delay().is_none());

        rules.crawl_delay = Some(2.5);
        assert_eq!(rules.effective_delay(), Some(Duration::from_secs_f64(2.5)));

        // crawl_delay takes precedence over request_rate
        rules.request_rate = Some(RequestRate::new(1, 10));
        assert_eq!(rules.effective_delay(), Some(Duration::from_secs_f64(2.5)));

        // Without crawl_delay, request_rate is used
        rules.crawl_delay = None;
        assert_eq!(rules.effective_delay(), Some(Duration::from_secs(10)));
    }

    #[test]
    fn test_fetch_status_not_modified() {
        let status = FetchStatus::NotModified;
        assert!(status.is_not_modified());
        assert!(!status.allows_all());
        assert!(!status.denies_all());
    }

    #[test]
    fn test_policy_extend_ttl() {
        let mut policy = RobotsPolicy::unavailable(404, Duration::from_secs(60));
        let original_expires = policy.expires_at_ms;
        
        std::thread::sleep(Duration::from_millis(10));
        policy.extend_ttl(Duration::from_secs(3600));
        
        assert!(policy.expires_at_ms > original_expires);
    }
}