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

username() -> Option<&str>? #234

Closed
ConnyOnny opened this issue Oct 23, 2016 · 7 comments
Closed

username() -> Option<&str>? #234

ConnyOnny opened this issue Oct 23, 2016 · 7 comments

Comments

@ConnyOnny
Copy link

Hey, just wondering why username() returns an empty string and password() returns None if not set.
This makes my code a little weird:

let mut username = parsed_url.username();
if username.is_empty() {
    username = "anonymous";
}
let password = parsed_url.password().unwrap_or("anonymous");

If there isn't a good reason for this, I'd like to propose that username() should also return an Option.

@SimonSapin
Copy link
Member

This reflects the data model in the spec:

https://url.spec.whatwg.org/#urls

A URL’s username is an ASCII string identifying a user. It is initially the empty string.

A URL’s password is either null or an ASCII string identifying a user’s credentials. It is initially null.

To be honest I don’t know the reason for this difference. @annevk, can you say more? If the username could also be null, I suppose it would be null in http://example.com/ but still the empty string in http://example.com@/.

@ConnyOnny
Copy link
Author

Some experiments:

"http://x.com"
username: ""
password: None

"http://@x.com"
username: ""
password: None

"http://:@x.com"
username: ""
password: Some("")

"http://example.com@/"
thread 'main' panicked at 'invalid url: EmptyHost'

This makes it impossible to specify the empty string as a username, as the application will always think, there was no username given at all.

@annevk
Copy link

annevk commented Oct 23, 2016

You can differentiate between password being present and not, but not with username.

@SimonSapin
Copy link
Member

@annevk yes, in the model as currently specified in https://url.spec.whatwg.org/#urls. But is there a specific reason it is specified that way?

@annevk
Copy link

annevk commented Oct 23, 2016

As far as I know that matches user agents and how the syntax works.

@SimonSapin
Copy link
Member

In fact the standard went in the opposite direction: removing the distinction between no password and an empty password whatwg/url#186

@blanham
Copy link

blanham commented May 26, 2017

@SimonSapin Would that indicate then that password should also not be Option<&str>? Also, would you be open to a PR that adds a convenience function getter for username (and perhaps password, if it is changed to match the standard) that returns an Option<&str>? Then you could do this, as @ConnyOnny was proposing:

let username = parsed_url.username_as_option().unwrap_or("anonymous");
let password = parsed_url.password_as_option().unwrap_or("anonymous");

With the current focus on ergonomics in the community I think it would be a nice addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants