-
Notifications
You must be signed in to change notification settings - Fork 344
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.includes_credentials() and url.is_special(). #520
Open
o0Ignition0o
wants to merge
2
commits into
servo:main
Choose a base branch
from
o0Ignition0o:url_misc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -477,7 +477,7 @@ impl Url { | |
assert_eq!(host_str, h.to_string()) | ||
} | ||
HostInternal::Domain => { | ||
if SchemeType::from(self.scheme()).is_special() { | ||
if self.is_special() { | ||
assert!(!host_str.is_empty()) | ||
} | ||
} | ||
|
@@ -1635,7 +1635,7 @@ impl Url { | |
} | ||
|
||
if let Some(host) = host { | ||
if host == "" && SchemeType::from(self.scheme()).is_special() { | ||
if host == "" && self.is_special() { | ||
return Err(ParseError::EmptyHost); | ||
} | ||
let mut host_substr = host; | ||
|
@@ -1653,14 +1653,13 @@ impl Url { | |
None => {} | ||
} | ||
} | ||
if SchemeType::from(self.scheme()).is_special() { | ||
if self.is_special() { | ||
self.set_host_internal(Host::parse(host_substr)?, None); | ||
} else { | ||
self.set_host_internal(Host::parse_opaque(host_substr)?, None); | ||
} | ||
} else if self.has_host() { | ||
let scheme_type = SchemeType::from(self.scheme()); | ||
if scheme_type.is_special() { | ||
if self.is_special() { | ||
return Err(ParseError::EmptyHost); | ||
} else { | ||
if self.serialization.len() == self.path_start as usize { | ||
|
@@ -2103,6 +2102,68 @@ impl Url { | |
Ok(()) | ||
} | ||
|
||
/// Return whether the URL is special. | ||
/// | ||
/// A URL is special if its scheme is one of the following: | ||
/// | ||
/// "http" | "https" | "ws" | "wss" | "ftp" | "gopher" | "file" | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let url = Url::parse("ftp://[email protected]")?; | ||
/// assert!(url.is_special()); | ||
/// | ||
/// let url = Url::parse("file://foo.bar")?; | ||
/// assert!(url.is_special()); | ||
/// | ||
/// let url = Url::parse("unix:/run/foo.socket")?; | ||
/// assert!(!url.is_special()); | ||
/// | ||
/// let url = Url::parse("data:text/plain,Stuff")?; | ||
/// assert!(!url.is_special()); | ||
/// # Ok(()) | ||
/// # } | ||
/// # run().unwrap(); | ||
/// ``` | ||
pub fn is_special(&self) -> bool { | ||
SchemeType::from(self.scheme()).is_special() | ||
} | ||
|
||
/// Return whether the URL includes credentials. | ||
/// | ||
/// A URL includes credentials if its username or password is not the empty string. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let url = Url::parse("https://username:[email protected]_site.com")?; | ||
/// assert!(url.includes_credentials()); | ||
/// | ||
/// let url = Url::parse("https://[email protected]_site.com")?; | ||
/// assert!(url.includes_credentials()); | ||
/// | ||
/// let url = Url::parse("https://www.my_site.com")?; | ||
/// assert!(!url.includes_credentials()); | ||
/// | ||
/// let url = Url::parse("https://@www.my_site.com")?; | ||
/// assert!(!url.includes_credentials()); | ||
/// # Ok(()) | ||
/// # } | ||
/// # run().unwrap(); | ||
/// ``` | ||
pub fn includes_credentials(&self) -> bool { | ||
self.username() != "" || self.password().unwrap_or(&"") != "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about this code, is it even possible to have a password without username? |
||
} | ||
|
||
/// Convert a file name as `std::path::Path` into an URL in the `file` scheme. | ||
/// | ||
/// This returns `Err` if the given path is not absolute or, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, "special" is really about the scheme (which matches the docs), whereas a
Url::is_special()
method seems to be about the whole URL. Should this be renamed tois_special_scheme()
oris_scheme_special()
orhas_special_scheme()
?