URL Prefix

Build Status 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 You Need This?

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

```rust let mut urlprefix = String::new(); if ishttps { urlprefix.pushstr("https://"); } else { urlprefix.pushstr("http://"); } urlprefix.pushstr(domain);

if ishttps && port != 443 || !ishttps && port != 80{ urlprefix.pushstr(":"); urlprefix.pushstr(&port.to_string()); } ```

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

```rust extern crate url_prefix;

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

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

```rust extern crate url_prefix;

let prefix = urlprefix::createprefix(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 validator feature for this crate.

toml [dependencies.url-prefix] version = "*" features = ["validator"] And the create_prefix_with_validated_domain is available.

For example,

```rust,ignore extern crate url_prefix;

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

let prefix = urlprefix::createprefixwithvalidateddomain(urlprefix::Protocol::HTTPS, userinput.asdomain(), 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