#url #query-parameters #parse-url #origin #domain #root #server-response

urlable

A library base on url crate to parse url but provide more useful function

2 releases

0.1.1 Nov 21, 2023
0.1.0 Nov 21, 2023

#8 in #origin

Download history 3/week @ 2024-02-19 23/week @ 2024-02-26 7/week @ 2024-03-11 32/week @ 2024-04-01 32/week @ 2024-04-15

64 downloads per month

MIT license

15KB
266 lines

urlable

urlable is a utility library built on top of the url crate, primarily focused on handling operations related to URLs. This includes, but is not limited to:

  • get_query: Retrieve the query parameters from a URL and return them in the form of a HashMap.
  • with_query: Add query parameters to a URL, appending them to the existing ones.
  • root_domain: Obtain the root domain of a URL, commonly used when setting a root domain's Cookie in a Server Response.
  • origin: Fetch the origin of the URL, similar to location.origin in JavaScript.
  • ....

For more information, please refer to the docs.


lib.rs:

The parse method is used to parse the input into a UrlAble, and after being wrapped by UrlAble, you can access more methods related to the URL.

use urlable::UrlAble;
let url_able = UrlAble::parse("https://ihavecoke:github@foo.bar.com:8080/x/y/z?foo=bar&z=b#id_1");
assert_eq!(url_able.hostname(), "foo.bar.com");
assert_eq!(url_able.root_domain(), ".bar.com");
assert_eq!(url_able.pathname(), "/x/y/z");
assert_eq!(url_able.port(), Some(8080));
assert_eq!(url_able.password(), "github");
assert_eq!(url_able.search(), "?invite-code=ufo-test&channel=rust");
assert_eq!(url_able.hash(), "#id_1");
assert_eq!(url_able.protocol(), "https");

let query = url_able.get_query();
assert_eq!(query.get("foo"), Some(&"bar".to_string()));
assert_eq!(query.get("z"), Some(&"b".to_string()));

Dependencies

~3.5–5MB
~112K SLoC