nickel-org / nickel.rs

An expressjs inspired web framework for Rust

Home Page:http://nickel-org.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Matcher has problem matching text

llacroix opened this issue · comments

Here's a route I made

    server.get("/test/:id", middleware!{|req| 
        let id = req.param("id").unwrap();
        format!("{}", id.to_string())
    });

If you try to browse the url /test/fun.js get param will only match fun and not fun.js. Also if you want to browse the url: /test/fun:time.js It will not match anything and fail with NotFound.

I guess it's related to this:

static ref REGEX_VAR_SEQ: Regex = Regex::new(r":([,a-zA-Z0-9_-]*)").unwrap();

Ah no, it's the other line.

format!("(?P<{}>[,a-zA-Z0-9%_-]*)", c.unwrap().as_str())

My guess is that somewhere it's matching the whole regex and matching even with dangling char. So that would explain why /test/fun.txt matches to ['id'] = "fun". But somewhere your global regex probably doesn't match for any possible char. And when we add :. It fails to match the whole route.

let line_regex = format!("^{}{}$", named_captures, REGEX_PARAM_SEQ);

Ok found it. Well obviously you could make the regex use unicode aware strings so instead of matching a-zA-Z.... it could be as simple as [\w\[\]]* as a regex or something similar.

You probably want a named capture that look like this: (?P<{}>[\w\s\p{P}\p{Pd}\p{S}--/]*)
It will match any unicode char except "/" including punctuations and such. So it should correctly match anything between "/" and "/".

What I don't understand is why some char will prevent the whole regex from matching and some will match at least the first part of the regex. Because "/test/fun.js" should fail to match "^/test/(?P<id>[,a-zA-Z0-9%_-]*)(\\?[a-zA-Z0-9%_=&-]*)?$"