#url #prefix #http #https #ftp

url-prefix

A library for creating URL prefix strings

17 stable releases

1.3.3 Jul 29, 2019
1.3.2 Dec 31, 2018
1.3.0 Nov 16, 2018
1.1.0 Oct 27, 2018

#12 in Value formatting

Download history 17/week @ 2019-03-28 16/week @ 2019-04-04 2/week @ 2019-04-11 18/week @ 2019-04-25 17/week @ 2019-05-02 31/week @ 2019-05-09 20/week @ 2019-05-16 44/week @ 2019-05-23 16/week @ 2019-06-06 16/week @ 2019-06-13 48/week @ 2019-06-20 217/week @ 2019-06-27 106/week @ 2019-07-04

182 downloads per month

MIT license

11KB
137 lines

URL Prefix

Build Status

This crate can be used to create URL prefix strings by inputting a protocol, a domain, a port number and a path.

Why We Need This?

Sometimes our web applications are run on different protocols(HTTP/HTTPS) and domains. And it is boring to write some code like below to format a URL:

let mut url_prefix = String::new();
if is_https {
    url_prefix.push_str("https://");
} else {
    url_prefix.push_str("http://");
}
url_prefix.push_str(domain);

if is_https && port != 443 || !is_https && port != 80{
    url_prefix.push_str(":");
    url_prefix.push_str(&port.to_string());
}

Instead, we can easily use this crate to create URL prefix strings. For examples,

extern crate url_prefix;

let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", None, None);

assert_eq!("https://magiclen.org", prefix);
extern crate url_prefix;

let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", Some(8100), Some("url-prefix"));

assert_eq!("https://magiclen.org:8100/url-prefix", prefix);

Validators Support

Validators is a crate which can help you validate user input, in order to create a safe URL prefix.

To use with Validators support, you have to enable the validator feature for this crate.

[dependencies.url-prefix]
version = "*"
features = ["validator"]

And the create_prefix_with_validated_domain, create_prefix_with_validated_ipv4, create_prefix_with_validated_ipv6, create_prefix_with_validated_host functions are available.

For example,

extern crate url_prefix;

let user_input = url_prefix::validators::domain::DomainLocalhostableWithPort::from_str("magiclen.org:443").unwrap();

let prefix = url_prefix::create_prefix_with_validated_domain(url_prefix::Protocol::HTTPS, user_input.as_domain(), Some("url-prefix"));

assert_eq!("https://magiclen.org/url-prefix", prefix);

Crates.io

https://crates.io/crates/url-prefix

Documentation

https://docs.rs/url-prefix

License

MIT

Dependencies

~0–410KB
~10K SLoC