Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Url::into_encoded_string #540

Closed
Keats opened this issue Aug 12, 2019 · 2 comments

Comments

@Keats
Copy link

commented Aug 12, 2019

Which would be the equivalent of urlencode in various languages.
To do something like:

let u = Url::parse("https://www.example.org/foo?a=b&c=d").unwrap();
assert_eq!(u.into_encoded_string(), "https://www.example.org/foo%3Fa%3Db%26c%3Dd")
@SimonSapin

This comment has been minimized.

Copy link
Member

commented Aug 16, 2019

I think there may be some confusion here. The urlencode function in PHP and Python for example is not intended to encode an entire URL string. Instead, it encodes a string for the purpose of using it as a component of an URL, for example in a query string. The example in PHP docs is:

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

The string https://www.example.org/foo%3Fa%3Db%26c%3Dd looks like a bug, not like a desirable output. Unless it is intended to be part of another URL, something like https:://example.net/redirect?next=https://www.example.org/foo%3Fa%3Db%26c%3Dd. To do that, existing APIs will already take care of encoding the value correctly:

url.query_pairs_mut().append_pair("next", other_url.as_str());

If you want to encode the URL (or some other string) for the purpose of some other format or protocol that happens to also use percent-encoding, the library for that protocol should take care of the encoding. If you’re implementing that protocol yourself, https://crates.io/crates/percent-encoding may be what you’re looking for.

@Keats

This comment has been minimized.

Copy link
Author

commented Aug 16, 2019

Yeah my bad, I got percent encoding and xml escaping confused in my head somehow.

@Keats Keats closed this Aug 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.