robotparser-rs 0.1.0

A faithful Rust port of Python's urllib.robotparser: parse robots.txt and answer can_fetch/crawl_delay/request_rate, byte-for-byte compatible with CPython.
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
//! A faithful Rust port of Python's [`urllib.robotparser`](https://docs.python.org/3/library/urllib.robotparser.html).
//!
//! Port target: the standard library `urllib/robotparser.py` (CPython 3.13). Behaviour is verified
//! against the reference module itself via differential testing.
//!
//! Unlike the existing `robotparser` crate (which follows the 1996 `norobots-rfc` draft), this
//! mirrors what CPython actually does today: a **prefix-match, first-match** algorithm (no `*`/`$`
//! wildcards, no longest-match precedence), with CPython's exact path normalization, parse state
//! machine, and user-agent substring matching.
//!
//! The network half of `RobotFileParser` (`read()` / `urlopen`) is out of scope — fetch the bytes
//! yourself and feed the lines to [`RobotFileParser::parse`], exactly as CPython's `read()` does.
//!
//! ```
//! use robotparser_rs::RobotFileParser;
//!
//! let mut rp = RobotFileParser::new("http://example.com/robots.txt");
//! rp.parse(&["User-agent: *", "Disallow: /private", "Allow: /private/ok"]);
//! assert_eq!(rp.can_fetch("MyBot", "http://example.com/public"), Ok(true));
//! assert_eq!(rp.can_fetch("MyBot", "http://example.com/private/x"), Ok(false));
//! ```

use std::fmt;

use urlparse_rs::{quote_from_bytes, unquote_to_bytes, urlsplit, urlunsplit, UrlError};

/// A `Request-rate` directive: `requests` per `seconds`. Port of the `RequestRate` namedtuple.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RequestRate {
    pub requests: u64,
    pub seconds: u64,
}

/// `quote(unquote(path, surrogateescape), surrogateescape)` — canonicalize percent-encoding. Done
/// on the raw decoded bytes (equivalent to CPython's surrogateescape round-trip), so any byte
/// sequence is preserved exactly. Port of `normalize`.
fn normalize(path: &str) -> String {
    quote_from_bytes(&unquote_to_bytes(path), "/")
}

/// Normalize the path, and within a `?query` normalize each maximal run of non-`=&` characters
/// (leaving the `=`/`&` separators). Port of `normalize_path`.
fn normalize_path(path: &str) -> String {
    match path.split_once('?') {
        Some((path, query)) => {
            let mut out = normalize(path);
            out.push('?');
            // Port of `re.sub(r'[^=&]+', lambda m: normalize(m[0]), query)`.
            let mut run = String::new();
            for ch in query.chars() {
                if ch == '=' || ch == '&' {
                    if !run.is_empty() {
                        out.push_str(&normalize(&run));
                        run.clear();
                    }
                    out.push(ch);
                } else {
                    run.push(ch);
                }
            }
            if !run.is_empty() {
                out.push_str(&normalize(&run));
            }
            out
        }
        None => normalize(path),
    }
}

/// A single `Allow:`/`Disallow:` rule. Port of `RuleLine`.
#[derive(Debug, Clone)]
struct RuleLine {
    path: String,
    allowance: bool,
}

impl RuleLine {
    fn new(path: &str, allowance: bool) -> Self {
        // An empty `Disallow:` value means allow all.
        let allowance = if path.is_empty() && !allowance { true } else { allowance };
        RuleLine { path: normalize_path(path), allowance }
    }

    fn applies_to(&self, filename: &str) -> bool {
        self.path == "*" || filename.starts_with(&self.path)
    }
}

impl fmt::Display for RuleLine {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", if self.allowance { "Allow" } else { "Disallow" }, self.path)
    }
}

/// One or more user-agents and their rules/delays. Port of `Entry`.
#[derive(Debug, Clone, Default)]
struct Entry {
    useragents: Vec<String>,
    rulelines: Vec<RuleLine>,
    delay: Option<i64>,
    req_rate: Option<RequestRate>,
}

impl Entry {
    /// Port of `Entry.applies_to`: the agent token (before the first `/`, lowercased) contains one
    /// of this entry's agents as a substring, or the entry has the catch-all `*`.
    fn applies_to(&self, useragent: &str) -> bool {
        let useragent = useragent.split('/').next().unwrap_or("").to_lowercase();
        for agent in &self.useragents {
            if agent == "*" {
                return true;
            }
            if useragent.contains(&agent.to_lowercase()) {
                return true;
            }
        }
        false
    }

    /// Port of `Entry.allowance`: the first rule whose path matches decides; default allow.
    fn allowance(&self, filename: &str) -> bool {
        for line in &self.rulelines {
            if line.applies_to(filename) {
                return line.allowance;
            }
        }
        true
    }
}

impl fmt::Display for Entry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut parts: Vec<String> = Vec::new();
        for agent in &self.useragents {
            parts.push(format!("User-agent: {agent}"));
        }
        if let Some(delay) = self.delay {
            parts.push(format!("Crawl-delay: {delay}"));
        }
        if let Some(rate) = self.req_rate {
            parts.push(format!("Request-rate: {}/{}", rate.requests, rate.seconds));
        }
        for line in &self.rulelines {
            parts.push(line.to_string());
        }
        f.write_str(&parts.join("\n"))
    }
}

/// Reads, parses and answers questions about a single robots.txt file. Port of `RobotFileParser`.
#[derive(Debug, Clone, Default)]
pub struct RobotFileParser {
    entries: Vec<Entry>,
    sitemaps: Vec<String>,
    default_entry: Option<Entry>,
    disallow_all: bool,
    allow_all: bool,
    url: String,
    host: String,
    path: String,
    // CPython tracks `last_checked` as a timestamp and only ever tests its truthiness; we model
    // that as "has parse()/modified() been called?".
    last_checked: bool,
}

impl RobotFileParser {
    /// Create a parser for the robots.txt at `url` (pass `""` for none). Port of `__init__`.
    pub fn new(url: &str) -> Self {
        let mut rp = RobotFileParser::default();
        rp.set_url(url);
        rp
    }

    /// Whether the file has been parsed yet. Port of `mtime` (truthiness only — no real clock).
    pub fn mtime(&self) -> bool {
        self.last_checked
    }

    /// Mark the file as just parsed. Port of `modified`.
    pub fn modified(&mut self) {
        self.last_checked = true;
    }

    /// Set the robots.txt URL, caching its host and path. Port of `set_url`. A malformed authority
    /// leaves host/path empty rather than propagating (CPython would raise here).
    pub fn set_url(&mut self, url: &str) {
        self.url = url.to_string();
        if let Ok(s) = urlsplit(url, "", true) {
            self.host = s.netloc;
            self.path = s.path;
        } else {
            self.host = String::new();
            self.path = String::new();
        }
    }

    /// The robots.txt URL set via [`new`](Self::new) / [`set_url`](Self::set_url).
    pub fn url(&self) -> &str {
        &self.url
    }

    /// The host of the robots.txt URL.
    pub fn host(&self) -> &str {
        &self.host
    }

    /// The path of the robots.txt URL.
    pub fn path(&self) -> &str {
        &self.path
    }

    fn add_entry(&mut self, entry: Entry) {
        if entry.useragents.iter().any(|a| a == "*") {
            // The first catch-all entry becomes the default and is considered last.
            if self.default_entry.is_none() {
                self.default_entry = Some(entry);
            }
        } else {
            self.entries.push(entry);
        }
    }

    /// Parse the lines of a robots.txt file (already split into lines, no trailing newlines). Port
    /// of `parse`.
    pub fn parse<S: AsRef<str>>(&mut self, lines: &[S]) {
        // states: 0 start, 1 saw user-agent, 2 saw allow/disallow
        let mut state = 0u8;
        let mut entry = Entry::default();
        self.modified();
        for raw in lines {
            let raw = raw.as_ref();
            if raw.is_empty() {
                match state {
                    1 => {
                        entry = Entry::default();
                        state = 0;
                    }
                    2 => {
                        self.add_entry(std::mem::take(&mut entry));
                        state = 0;
                    }
                    _ => {}
                }
            }
            // strip an optional comment, then surrounding whitespace
            let line = match raw.find('#') {
                Some(i) => &raw[..i],
                None => raw,
            };
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let (key, value) = match line.split_once(':') {
                Some((k, v)) => (k.trim().to_lowercase(), v.trim()),
                None => continue, // no ':' — CPython's len(line)==2 guard skips it
            };
            match key.as_str() {
                "user-agent" => {
                    if state == 2 {
                        self.add_entry(std::mem::take(&mut entry));
                    }
                    entry.useragents.push(value.to_string());
                    state = 1;
                }
                "disallow" => {
                    if state != 0 {
                        entry.rulelines.push(RuleLine::new(value, false));
                        state = 2;
                    }
                }
                "allow" => {
                    if state != 0 {
                        entry.rulelines.push(RuleLine::new(value, true));
                        state = 2;
                    }
                }
                "crawl-delay" => {
                    if state != 0 {
                        if is_ascii_digits(value)
                            && let Ok(n) = value.parse::<i64>()
                        {
                            entry.delay = Some(n);
                        }
                        state = 2;
                    }
                }
                "request-rate" => {
                    if state != 0 {
                        let nums: Vec<&str> = value.split('/').collect();
                        if nums.len() == 2
                            && is_ascii_digits(nums[0].trim())
                            && is_ascii_digits(nums[1].trim())
                            && let (Ok(r), Ok(s)) =
                                (nums[0].trim().parse::<u64>(), nums[1].trim().parse::<u64>())
                        {
                            entry.req_rate = Some(RequestRate { requests: r, seconds: s });
                        }
                        state = 2;
                    }
                }
                "sitemap" => {
                    // Independent of user-agent, so the state is left unchanged.
                    self.sitemaps.push(value.to_string());
                }
                _ => {}
            }
        }
        if state == 2 {
            self.add_entry(entry);
        }
    }

    /// Decide whether `useragent` may fetch `url`, per the parsed robots.txt. Port of `can_fetch`.
    ///
    /// Returns `Err` only when the URL's authority is malformed (CPython raises `ValueError` from
    /// the internal `urlsplit`).
    pub fn can_fetch(&self, useragent: &str, url: &str) -> Result<bool, UrlError> {
        if self.disallow_all {
            return Ok(false);
        }
        if self.allow_all {
            return Ok(true);
        }
        // Until the file has been parsed, assume nothing is allowed.
        if !self.last_checked {
            return Ok(false);
        }
        let parsed = urlsplit(url, "", true)?;
        let url = urlunsplit("", "", &parsed.path, &parsed.query, &parsed.fragment);
        let url = normalize_path(&url);
        let url = if url.is_empty() { "/".to_string() } else { url };
        for entry in &self.entries {
            if entry.applies_to(useragent) {
                return Ok(entry.allowance(&url));
            }
        }
        if let Some(default) = &self.default_entry {
            return Ok(default.allowance(&url));
        }
        // agent not found ==> access granted
        Ok(true)
    }

    /// The `Crawl-delay` for `useragent`, or `None`. Port of `crawl_delay`.
    pub fn crawl_delay(&self, useragent: &str) -> Option<i64> {
        if !self.last_checked {
            return None;
        }
        for entry in &self.entries {
            if entry.applies_to(useragent) {
                return entry.delay;
            }
        }
        self.default_entry.as_ref().and_then(|e| e.delay)
    }

    /// The `Request-rate` for `useragent`, or `None`. Port of `request_rate`.
    pub fn request_rate(&self, useragent: &str) -> Option<RequestRate> {
        if !self.last_checked {
            return None;
        }
        for entry in &self.entries {
            if entry.applies_to(useragent) {
                return entry.req_rate;
            }
        }
        self.default_entry.as_ref().and_then(|e| e.req_rate)
    }

    /// The `Sitemap` URLs, or `None` if there were none. Port of `site_maps`.
    pub fn site_maps(&self) -> Option<&[String]> {
        if self.sitemaps.is_empty() {
            None
        } else {
            Some(&self.sitemaps)
        }
    }
}

impl fmt::Display for RobotFileParser {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut entries: Vec<&Entry> = self.entries.iter().collect();
        if let Some(default) = &self.default_entry {
            entries.push(default);
        }
        let joined: Vec<String> = entries.iter().map(|e| e.to_string()).collect();
        f.write_str(&joined.join("\n\n"))
    }
}

/// Port of CPython's `str.isdigit()` for the cases the parser exercises: a non-empty run of ASCII
/// digits. (CPython also accepts some Unicode digit code points; those are not modelled.)
fn is_ascii_digits(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())
}

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

    fn parsed(text: &str) -> RobotFileParser {
        let mut rp = RobotFileParser::new("http://example.com/robots.txt");
        let lines: Vec<&str> = text.split('\n').collect();
        rp.parse(&lines);
        rp
    }

    #[test]
    fn basic_allow_disallow() {
        let rp = parsed("User-agent: *\nDisallow: /private\nAllow: /private/ok");
        assert_eq!(rp.can_fetch("Bot", "http://example.com/public"), Ok(true));
        assert_eq!(rp.can_fetch("Bot", "http://example.com/private/x"), Ok(false));
        // First-match wins: /private matches before /private/ok is considered.
        assert_eq!(rp.can_fetch("Bot", "http://example.com/private/ok"), Ok(false));
    }

    #[test]
    fn empty_disallow_allows_all() {
        let rp = parsed("User-agent: *\nDisallow:");
        assert_eq!(rp.can_fetch("Bot", "http://example.com/anything"), Ok(true));
    }

    #[test]
    fn not_parsed_denies() {
        let rp = RobotFileParser::new("http://example.com/robots.txt");
        assert_eq!(rp.can_fetch("Bot", "http://example.com/"), Ok(false));
    }

    #[test]
    fn agent_specific_overrides_default() {
        let rp = parsed("User-agent: BadBot\nDisallow: /\n\nUser-agent: *\nDisallow: /admin");
        assert_eq!(rp.can_fetch("BadBot", "http://example.com/x"), Ok(false));
        assert_eq!(rp.can_fetch("GoodBot", "http://example.com/x"), Ok(true));
        assert_eq!(rp.can_fetch("GoodBot", "http://example.com/admin"), Ok(false));
    }

    #[test]
    fn crawl_delay_and_request_rate() {
        let rp = parsed("User-agent: *\nCrawl-delay: 10\nRequest-rate: 1/5");
        assert_eq!(rp.crawl_delay("Bot"), Some(10));
        assert_eq!(rp.request_rate("Bot"), Some(RequestRate { requests: 1, seconds: 5 }));
    }

    #[test]
    fn sitemaps() {
        let rp = parsed("Sitemap: http://example.com/sitemap.xml\nUser-agent: *\nDisallow:");
        assert_eq!(rp.site_maps(), Some(&["http://example.com/sitemap.xml".to_string()][..]));
    }
}