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
//! Parser - RFC 9309 compliant robots.txt parser
//!
//! This module provides a robust parser for robots.txt files that handles:
//! - UTF-8 BOM stripping
//! - Request-rate directive (non-standard but common)
//! - Proper percent-encoding normalization
//! - Size limits per RFC 9309

use crate::types::{Group, RequestRate, Rule, RuleKind, RobotsPolicy, FetchStatus};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tracing::{debug, warn};

/// Maximum robots.txt size (RFC 9309 requires at least 500 KiB)
pub const MAX_ROBOTS_SIZE: usize = 512 * 1024;

/// UTF-8 BOM bytes
const UTF8_BOM: &[u8] = &[0xEF, 0xBB, 0xBF];

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

/// Strip UTF-8 BOM if present
fn strip_bom(content: &str) -> &str {
    if content.as_bytes().starts_with(UTF8_BOM) {
        &content[3..]
    } else {
        content
    }
}

/// Parser for robots.txt files
pub struct RobotsParser {
    /// Maximum content size to parse
    max_size: usize,
}

impl Default for RobotsParser {
    fn default() -> Self {
        Self::new()
    }
}

impl RobotsParser {
    /// Create a new parser with default settings
    pub fn new() -> Self {
        Self {
            max_size: MAX_ROBOTS_SIZE,
        }
    }

    /// Create a parser with custom max size
    pub fn with_max_size(max_size: usize) -> Self {
        Self { max_size }
    }

    /// Parse robots.txt content into a policy
    pub fn parse(&self, content: &str, ttl: Duration) -> RobotsPolicy {
        let now = now_millis();
        
        // Strip UTF-8 BOM if present
        let content = strip_bom(content);
        
        // Enforce size limit
        let content = if content.len() > self.max_size {
            warn!(
                "robots.txt exceeds size limit ({} > {}), truncating",
                content.len(),
                self.max_size
            );
            &content[..self.max_size]
        } else {
            content
        };

        let content_size = content.len();
        let mut groups: Vec<Group> = Vec::new();
        let mut sitemaps: Vec<String> = Vec::new();
        let mut current_group: Option<Group> = None;

        for line in content.lines() {
            let line = self.clean_line(line);
            if line.is_empty() {
                continue;
            }

            // Parse the line
            if let Some((directive, value)) = self.parse_directive(&line) {
                match directive.to_lowercase().as_str() {
                    "user-agent" => {
                        // Start a new group or add to current
                        if let Some(ref mut group) = current_group {
                            if group.rules.is_empty() {
                                // No rules yet, can add more user-agents
                                group.user_agents.push(value.to_string());
                            } else {
                                // Rules exist, save current and start new
                                groups.push(current_group.take().unwrap());
                                current_group = Some(Group {
                                    user_agents: vec![value.to_string()],
                                    rules: Vec::new(),
                                    crawl_delay: None,
                                    request_rate: None,
                                });
                            }
                        } else {
                            current_group = Some(Group {
                                user_agents: vec![value.to_string()],
                                rules: Vec::new(),
                                crawl_delay: None,
                                request_rate: None,
                            });
                        }
                    }
                    "allow" => {
                        if let Some(ref mut group) = current_group {
                            let pattern = self.normalize_pattern(value);
                            if !pattern.is_empty() {
                                group.rules.push(Rule::new(RuleKind::Allow, pattern));
                            }
                        }
                    }
                    "disallow" => {
                        if let Some(ref mut group) = current_group {
                            let pattern = self.normalize_pattern(value);
                            // Empty disallow means allow all
                            if !pattern.is_empty() {
                                group.rules.push(Rule::new(RuleKind::Disallow, pattern));
                            }
                        }
                    }
                    "crawl-delay" => {
                        if let Some(ref mut group) = current_group {
                            if let Ok(delay) = value.trim().parse::<f64>() {
                                if delay >= 0.0 {
                                    group.crawl_delay = Some(delay);
                                }
                            }
                        }
                    }
                    "request-rate" => {
                        // Non-standard but common: "requests/seconds" e.g., "1/10"
                        if let Some(ref mut group) = current_group {
                            if let Some(rate) = Self::parse_request_rate(value) {
                                group.request_rate = Some(rate);
                            }
                        }
                    }
                    "sitemap" => {
                        // Sitemap is not part of a group
                        let sitemap_url = value.trim().to_string();
                        if !sitemap_url.is_empty() {
                            sitemaps.push(sitemap_url);
                        }
                    }
                    _ => {
                        // Unknown directive, ignore per RFC
                        debug!("Ignoring unknown robots.txt directive: {}", directive);
                    }
                }
            }
        }

        // Save the last group
        if let Some(group) = current_group {
            if !group.user_agents.is_empty() {
                groups.push(group);
            }
        }

        RobotsPolicy {
            fetched_at_ms: now,
            expires_at_ms: now + ttl.as_millis() as u64,
            fetch_status: FetchStatus::Success,
            groups,
            sitemaps,
            content_size,
            etag: None,
            last_modified: None,
        }
    }

    /// Parse a request-rate value like "1/10" (1 request per 10 seconds)
    fn parse_request_rate(value: &str) -> Option<RequestRate> {
        let parts: Vec<&str> = value.trim().split('/').collect();
        if parts.len() == 2 {
            let requests = parts[0].trim().parse::<u32>().ok()?;
            let seconds = parts[1].trim().parse::<u32>().ok()?;
            if requests > 0 && seconds > 0 {
                return Some(RequestRate::new(requests, seconds));
            }
        }
        None
    }

    /// Clean a line (remove comments, trim whitespace)
    fn clean_line(&self, line: &str) -> String {
        // Remove comments
        let line = match line.find('#') {
            Some(pos) => &line[..pos],
            None => line,
        };
        line.trim().to_string()
    }

    /// Parse a directive line into (directive, value)
    fn parse_directive<'a>(&self, line: &'a str) -> Option<(&'a str, &'a str)> {
        let colon_pos = line.find(':')?;
        let directive = line[..colon_pos].trim();
        let value = line[colon_pos + 1..].trim();
        
        if directive.is_empty() {
            return None;
        }

        Some((directive, value))
    }

    /// Normalize a pattern for matching
    fn normalize_pattern(&self, pattern: &str) -> String {
        let pattern = pattern.trim();
        
        // Handle empty pattern
        if pattern.is_empty() {
            return String::new();
        }

        // Ensure pattern starts with /
        if !pattern.starts_with('/') && !pattern.starts_with('*') {
            format!("/{}", pattern)
        } else {
            pattern.to_string()
        }
    }
}

/// Utility functions for percent-encoding handling
pub mod encoding {
    /// Decode percent-encoded characters for matching
    /// Only decodes unreserved characters per RFC 3986
    pub fn normalize_path_for_matching(path: &str) -> String {
        let mut result = String::with_capacity(path.len());
        let mut chars = path.chars().peekable();

        while let Some(c) = chars.next() {
            if c == '%' {
                // Try to decode
                let hex: String = chars.by_ref().take(2).collect();
                if hex.len() == 2 {
                    if let Ok(byte) = u8::from_str_radix(&hex, 16) {
                        let decoded = byte as char;
                        // Only decode unreserved characters
                        if is_unreserved(decoded) {
                            result.push(decoded);
                            continue;
                        }
                    }
                }
                // Keep as-is if can't decode
                result.push('%');
                result.push_str(&hex);
            } else {
                result.push(c);
            }
        }

        result
    }

    /// Check if a character is unreserved per RFC 3986
    fn is_unreserved(c: char) -> bool {
        c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' || c == '~'
    }

    /// Normalize both pattern and path for comparison
    pub fn normalize_for_comparison(s: &str) -> String {
        // First decode unreserved characters
        let decoded = normalize_path_for_matching(s);
        // Uppercase remaining percent-encoding for consistency
        uppercase_percent_encoding(&decoded)
    }

    /// Uppercase percent-encoding hex digits
    fn uppercase_percent_encoding(s: &str) -> String {
        let mut result = String::with_capacity(s.len());
        let mut chars = s.chars().peekable();

        while let Some(c) = chars.next() {
            if c == '%' {
                result.push('%');
                // Uppercase next two characters
                for _ in 0..2 {
                    if let Some(hex_char) = chars.next() {
                        result.push(hex_char.to_ascii_uppercase());
                    }
                }
            } else {
                result.push(c);
            }
        }

        result
    }
}

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

    #[test]
    fn test_parse_simple() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: *
Disallow: /private/
Allow: /private/public/
Crawl-delay: 2
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups.len(), 1);
        assert_eq!(policy.groups[0].user_agents, vec!["*"]);
        assert_eq!(policy.groups[0].rules.len(), 2);
        assert_eq!(policy.groups[0].crawl_delay, Some(2.0));
    }

    #[test]
    fn test_parse_multiple_groups() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: Googlebot
User-agent: Bingbot
Disallow: /search

User-agent: *
Disallow: /admin
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups.len(), 2);
        assert_eq!(policy.groups[0].user_agents, vec!["Googlebot", "Bingbot"]);
        assert_eq!(policy.groups[1].user_agents, vec!["*"]);
    }

    #[test]
    fn test_parse_sitemaps() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: *
Disallow:

Sitemap: https://example.com/sitemap.xml
Sitemap: https://example.com/sitemap2.xml
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.sitemaps.len(), 2);
        assert_eq!(policy.sitemaps[0], "https://example.com/sitemap.xml");
    }

    #[test]
    fn test_parse_comments() {
        let parser = RobotsParser::new();
        let content = r#"
# This is a comment
User-agent: * # inline comment
Disallow: /private # another comment
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups.len(), 1);
        assert_eq!(policy.groups[0].rules.len(), 1);
    }

    #[test]
    fn test_parse_empty_disallow() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: *
Disallow:
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        // Empty disallow should not create a rule
        assert_eq!(policy.groups[0].rules.len(), 0);
    }

    #[test]
    fn test_normalize_pattern() {
        let parser = RobotsParser::new();
        assert_eq!(parser.normalize_pattern("/path"), "/path");
        assert_eq!(parser.normalize_pattern("path"), "/path");
        assert_eq!(parser.normalize_pattern("*"), "*");
        assert_eq!(parser.normalize_pattern(""), "");
    }

    #[test]
    fn test_encoding_normalize() {
        use encoding::normalize_path_for_matching;
        
        // Unreserved characters should be decoded
        assert_eq!(normalize_path_for_matching("/path%2Dtest"), "/path-test");
        
        // Reserved characters should stay encoded
        assert_eq!(normalize_path_for_matching("/path%2Ftest"), "/path%2Ftest");
    }

    #[test]
    fn test_bom_stripping() {
        let parser = RobotsParser::new();
        // UTF-8 BOM followed by valid content
        let content = "\u{FEFF}User-agent: *\nDisallow: /private";
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups.len(), 1);
        assert_eq!(policy.groups[0].user_agents, vec!["*"]);
    }

    #[test]
    fn test_request_rate_parsing() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: *
Disallow: /private
Request-rate: 1/10
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups.len(), 1);
        let rate = policy.groups[0].request_rate.unwrap();
        assert_eq!(rate.requests, 1);
        assert_eq!(rate.seconds, 10);
        assert!((rate.delay_seconds() - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_crawl_delay_float() {
        let parser = RobotsParser::new();
        let content = r#"
User-agent: *
Crawl-delay: 0.5
"#;
        let policy = parser.parse(content, Duration::from_secs(3600));
        
        assert_eq!(policy.groups[0].crawl_delay, Some(0.5));
    }
}