#parser #url

url-parse

🔗 A Rust library for parsing URLs

28 releases (4 stable)

new 1.0.4 Jan 27, 2023
1.0.1 Oct 11, 2022
0.5.15 Oct 10, 2022
0.5.6 Jul 21, 2022

#196 in Parser implementations

Download history 626/week @ 2022-10-10 185/week @ 2022-10-17 334/week @ 2022-10-24 372/week @ 2022-10-31 213/week @ 2022-11-07 158/week @ 2022-11-14 230/week @ 2022-11-21 122/week @ 2022-11-28 134/week @ 2022-12-05 214/week @ 2022-12-12 115/week @ 2022-12-19 29/week @ 2022-12-26 184/week @ 2023-01-02 233/week @ 2023-01-09 217/week @ 2023-01-16 282/week @ 2023-01-23

918 downloads per month
Used in aim

MIT license

69KB
1.5K SLoC

url-parse

CI CD Security Audit codecov crates.io Documentation

A library for parsing URLs.

Why?

No current other crate with support for i.e. special schemes. The reasoning (i.e. in rust-url) is that schemes need to be part of the whatwg standard to be supported.

url-parse provides some missing schemes (sftp, ssh, s3) and enables the user to specify custom schemes before parsing.

Usage

Basic

Create a new parser object with Parser::new(). You can then use parser.parse(url) which will return a public Url parsed structure back.

Its fields are then directly accessible:

let input = "https://user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let result = Parser::new(None).parse(input).unwrap();
assert_eq!(
    result,
    Url {
        scheme: Some("https".to_string()),
        user_pass: (Some("user".to_string()), Some("pass".to_string())),
        subdomain: Some("www".to_string()),
        domain: Some("example.co".to_string()),
        top_level_domain: Some("uk".to_string()),
        port: Some(443),
        path: Some(vec![
            "blog".to_string(),
            "article".to_string(),
            "search".to_string(),
        ]),
        query: Some("docid=720&hl=en#dayone".to_string()),
        anchor: Some("dayone".to_string()),
    }
)

Custom schemes

Passing a Some(HashMap) to Parser::new() can be used to create custom schemes.

The hashmap is a key,value pair representing the scheme name (key) to a port and description mapping (value).

let input = "myschema://user:pass@example.co.uk/path/to/file.txt";
let mut myport_mappings = HashMap::new();
myport_mappings.insert("myschema", (8888, "My custom schema"));
let result = Parser::new(Some(myport_mappings)).parse(input).unwrap();
assert_eq!(
    result,
    Url {
        scheme: Some("myschema".to_string()),
        user_pass: (Some("user".to_string()), Some("pass".to_string())),
        subdomain: Some("www".to_string()),
        domain: Some("example.co".to_string()),
        top_level_domain: Some("uk".to_string()),
        port: Some(8888),
        path: Some(vec![
            "path".to_string(),
            "to".to_string(),
            "file.txt".to_string(),
        ]),
        query: None,
        anchor: None,
    }
);

Dependencies

~0.9–1.3MB
~37K SLoC