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
//! A robots.txt parser and applicability checker for Rust
//!
//! The Parser is implemented and tested after
//! <http://www.robotstxt.org/norobots-rfc.txt>
//!
//! # Usage
//!
//! Add it to your ``Cargo.toml``:
//!
//! ```toml
//! [dependencies]
//! robots-parser = "0.10"
//! ```
//!
//!
//! # Example
//!
//! ```rust,ignore
//!
//! use robots::RobotsParser;
//! use url::Url;
//!
//! fn main() {
//!     let parsed = RobotsParser::parse_url(Url::new("https://www.google.com/robots.txt"))?;
//!     assert!(parsed.can_fetch("*", "https://www.google.com/search/about"));
//! }
//! ```

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::bytes::complete::take_until;
use nom::bytes::complete::take_while;
use nom::bytes::complete::take_while1;
use nom::combinator::cond;
use nom::combinator::map_opt;
use nom::combinator::opt;
use nom::sequence::tuple;
use nom::IResult;

use url::percent_encoding::percent_decode;
use url::Url;

use std::fs;
use std::path::Path;

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct RobotsParser {
    rules: Vec<Robots>,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Robots {
    UserAgent(String, Vec<Rule>),
    GlobalRule(Rule),
}

impl Robots {
    fn is_applicable(&self, agent: &str, path: &str) -> bool {
        match self {
            Robots::UserAgent(s, _) => {
                let cleaned_user_agent = agent.split('/').nth(0).unwrap_or("");
                if s == "*" || *s == cleaned_user_agent.to_lowercase() {
                    true
                } else {
                    false
                }
            }
            Robots::GlobalRule(rule) => rule.is_applicable(path),
        }
    }

    // Precondition: Applicability has been proven
    fn is_allowed(&self, path: &str) -> bool {
        match self {
            Robots::UserAgent(_, rules) => {
                for rule in rules {
                    if rule.is_applicable(path) {
                        return rule.allowed();
                    }
                }
            }
            Robots::GlobalRule(rule) => return rule.allowed(),
        }
        false
    }
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Rule {
    Allow(String),
    Disallow(String),
    Extension,
}

impl Rule {
    fn is_applicable(&self, path: &str) -> bool {
        let own_path = match self {
            Rule::Allow(s) | Rule::Disallow(s) => s,
            _ => "",
        };

        own_path == "*" || path.starts_with(own_path)
    }

    // Precondition: Applicability has been proven
    fn allowed(&self) -> bool {
        match self {
            Rule::Allow(_) => true,
            _ => false,
        }
    }
}
impl RobotsParser {

    /// Creates a new `RobotsParser` from the given `Robots` Rules
    pub fn new(rules: Vec<Robots>) -> RobotsParser {
        RobotsParser { rules }
    }

    /// Parses a robots.txt input string
    pub fn parse<'a>(input: &'a str) -> Result<RobotsParser, &'static str> {
        let mut rules = vec![];
        let mut input = input;

        //Always add a Allow(/robots.txt) at the start
        rules.push(Robots::GlobalRule(Rule::Allow("/robots.txt".to_owned())));

        loop {
            let rulers = alt((
                RobotsParser::comment_line_parser(),
                map_opt(RobotsParser::crlf_parse(), |_| Some(None::<Robots>)),
                RobotsParser::parse_user_agent(),
                map_opt(RobotsParser::parse_rule(), |rule| {
                    Some(Some(Robots::GlobalRule(rule)))
                }),
            ))(input);
            input = match rulers {
                Ok((input, Some(rule))) => {
                    rules.push(rule);
                    input
                }
                Ok((input, None)) => input,
                Err(_) => {
                    return Err("Could not parse Robots.txt");
                }
            };

            // No more input -> Return
            if input.is_empty() {
                break;
            }
        }

        Ok(RobotsParser { rules: rules })
    }

    /// Parses a robots.txt file from the given path
    pub fn parse_path<P: AsRef<Path>>(path: P) -> Result<RobotsParser, &'static str> {
        let data = fs::read_to_string(path).expect("Unable to read file");
        RobotsParser::parse(&data)
    }

    /// Parses a robots.txt file from the given url
    #[cfg(feature = "web")]
    pub fn parse_url<U: Into<Url>>(url: U) -> Result<RobotsParser, &'static str> {
        let data = reqwest::get(url.into()).expect("Unable to read file from url").text().expect("Unable to rad file from url");
        RobotsParser::parse(&data)
    }

    /// Parses a space
    fn space_parser<'a>() -> impl Fn(&'a str) -> IResult<&'a str, &'a str> {
        take_while(|c| c == ' ' || c == '\t')
    }

    // Parses an alphanumeric token or `*`
    fn token_parser<'a>() -> impl Fn(&'a str) -> IResult<&'a str, &'a str> {
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '*')
    }

    /// Parses a comment and does not consume the linebreak
    fn comment_parser<'a>() -> impl Fn(&'a str) -> IResult<&'a str, (&'a str, &'a str)> {
        tuple((tag("#"), take_until("\r\n")))
    }

    /// Parses a line break
    fn crlf_parse<'a>() -> impl Fn(&'a str) -> IResult<&'a str, &'a str> {
        tag("\r\n")
    }

    /// Parses a comment line and returns an empty Robots.txt line
    fn comment_line_parser<'a>() -> impl Fn(&'a str) -> IResult<&'a str, Option<Robots>> {
        map_opt(
            tuple((RobotsParser::comment_parser(), RobotsParser::crlf_parse())),
            |_| Some(None),
        )
    }

    fn parse_user_agent<'a>() -> impl Fn(&'a str) -> IResult<&'a str, Option<Robots>> {
        move |input: &'a str| {
            let (input, _) = tag_no_case("user-agent:")(input)?;
            let (input, _) = RobotsParser::space_parser()(input)?;
            let (input, agent) = RobotsParser::token_parser()(input)?;
            // Parses optional comment after path
            let (input, _) = opt(RobotsParser::comment_parser())(input).unwrap_or((input, None));
            let (input, _) = RobotsParser::crlf_parse()(input)?;

            let (input, rules) = RobotsParser::parse_rules()(input)?;

            let rules = if rules.is_empty() {
                //There could be a second User-Agents
                let user_agent = RobotsParser::parse_user_agent()(input);

                let rules = match user_agent {
                    Ok((_, agent)) => match agent.unwrap() {
                        Robots::UserAgent(_, rules) => rules.clone(),
                        _ => panic!("User-Agent only retunrs a User-Agent"),
                    },
                    _ => rules,
                };
                rules
            } else {
                rules
            };
            Ok((input, Some(Robots::UserAgent(agent.to_owned(), rules))))
        }
    }

    /// Parses as many rules it can find
    fn parse_rules<'a>() -> impl Fn(&'a str) -> IResult<&'a str, Vec<Rule>> {
        move |input: &'a str| {
            let mut rules = vec![];
            let mut input = input;
            loop {
                input = match RobotsParser::parse_rule()(input) {
                    Ok((input, rule)) => {
                        rules.push(rule);
                        input
                    }
                    Err(_) => match RobotsParser::comment_line_parser()(input) {
                        Ok((input, _)) => input,
                        Err(_) => return Ok((input, rules)),
                    },
                };
            }
        }
    }

    /// Parses exactly one rule
    fn parse_rule<'a>() -> impl Fn(&'a str) -> IResult<&'a str, Rule> {
        move |input: &'a str| {
            let (input, allowence) = alt((tag("Allow:"), tag("Disallow:")))(input)?;
            let (input, _) = RobotsParser::space_parser()(input)?;
            let (input, path) = RobotsParser::parse_file_path(input)?;

            // Parses optional comment after path
            let (input, _) = opt(RobotsParser::comment_parser())(input).unwrap_or((input, None));

            // CRLF is optional, when the file is empty
            let (input, _) = cond(input.len() != 0, RobotsParser::crlf_parse())(input)?;

            // Empty Disallow means allow all
            if allowence == "Disallow:" && path.is_empty() {
                return Ok((input, Rule::Allow("*".to_owned())));
            }

            match allowence {
                "Allow:" => Ok((input, Rule::Allow(path))),
                "Disallow:" => Ok((input, Rule::Disallow(path))),
                _ => panic!("Rule must either be allowed or disallowed"),
            }
        }
    }

    /// Parses a path as specified
    /// Paths do not include `#` as they indicate a comment
    fn parse_file_path<'a>(input: &'a str) -> IResult<&'a str, String> {
        let (input, path) = take_while(|c: char| !c.is_whitespace() && c != '#')(input)?;
        Ok((input, path.to_owned()))
    }

    /// Decides if a path can be fetched by an agent
    pub fn can_fetch(&self, agent: &str, path: &str) -> bool {
        let url = Url::parse(path);
        match url {
            Ok(url) => {
                let path = percent_decode(url.path().as_bytes()).decode_utf8().unwrap();
                for rule in &*self.rules {
                    if rule.is_applicable(agent, &path) {
                        return rule.is_allowed(&path);
                    }
                }
                false
            }
            Err(_) => return false,
        }
    }
}

#[test]
fn user_agent_different_spellings() {
    assert!(RobotsParser::parse_user_agent()("User-Agent: test\r\n\r\n").is_ok());
    assert!(RobotsParser::parse_user_agent()("user-agent: test\r\n\r\n").is_ok());
    assert!(RobotsParser::parse_user_agent()("USER-AGENT: test\r\n\r\n").is_ok());
}

#[test]
fn user_agent_empty() {
    assert!(RobotsParser::parse_user_agent()("User-Agent:\r\n").is_err());
}

#[test]
fn comment() {
    assert!(RobotsParser::comment_parser()("# testtest\r\n").is_ok());
    assert!(RobotsParser::comment_parser()("testtest\r\n").is_err());
    assert!(RobotsParser::comment_parser()("#testtest").is_err());
    assert!(RobotsParser::comment_line_parser()("# testtest\r\n").is_ok());
    assert!(RobotsParser::comment_line_parser()("testtest\r\n").is_err());
    assert!(RobotsParser::comment_line_parser()("#testtest").is_err());
}

#[test]
fn rule() {
    assert!(RobotsParser::parse_rule()("Allow: /\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Disallow: /\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Allow: /#1234 \r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Disallow: /\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Disallow: \r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Disallow: /org/plans.html\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Disallow: /org/\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Allow: /serv\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Allow: /~mak\r\n").is_ok());
    assert!(RobotsParser::parse_rule()("Allow: /~mak\r\n").is_ok());
}

#[test]
fn rules() {
    let rules = "Disallow: /index.html?\r\nDisallow: /?\r
Allow: /?hl=\r
Disallow: /?hl=*&\r
Allow: /?hl=*&gws_rd=ssl$\r
Disallow: /?hl=*&*&gws_rd=ssl\r
Allow: /?gws_rd=ssl$";
    let result = vec![
        Rule::Disallow("/index.html?".to_owned()),
        Rule::Disallow("/?".to_owned()),
        Rule::Allow("/?hl=".to_owned()),
        Rule::Disallow("/?hl=*&".to_owned()),
        Rule::Allow("/?hl=*&gws_rd=ssl$".to_owned()),
        Rule::Disallow("/?hl=*&*&gws_rd=ssl".to_owned()),
        Rule::Allow("/?gws_rd=ssl$".to_owned()),
    ];
    let parsed = RobotsParser::parse_rules()(rules);
    assert!(parsed.is_ok());
    let (_, parsed) = parsed.unwrap();
    assert_eq!(parsed, result);
}