-
Notifications
You must be signed in to change notification settings - Fork 346
set_scheme should fail if url has host or port #366
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
|
@@ -301,7 +301,7 @@ impl Url { | |
/// ```rust | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let base = Url::parse("https://example.net/a/b.html")?; | ||
/// let url = base.join("c.png")?; | ||
|
@@ -729,6 +729,28 @@ impl Url { | |
!matches!(self.host, HostInternal::None) | ||
} | ||
|
||
/// Equivalent to `url.port().is_some()`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let url = Url::parse("http://example.com:8080")?; | ||
/// assert!(url.has_port()); | ||
/// | ||
/// let url = Url::parse("file:///tmp/something")?; | ||
/// assert!(!url.has_port()); | ||
/// # Ok(()) | ||
/// # } | ||
/// # run().unwrap(); | ||
/// ``` | ||
pub fn has_port(&self) -> bool { | ||
self.port.is_some() | ||
} | ||
|
||
/// Return the string representation of the host (domain or IP address) for this URL, if any. | ||
/// | ||
/// Non-ASCII domains are punycode-encoded per IDNA. | ||
|
@@ -1310,7 +1332,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("foo://example.net")?; | ||
/// let result = url.set_host(None); | ||
|
@@ -1326,7 +1348,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("https://example.net")?; | ||
/// let result = url.set_host(None); | ||
|
@@ -1342,7 +1364,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("mailto:[email protected]")?; | ||
/// | ||
|
@@ -1552,7 +1574,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("https://example.net")?; | ||
/// let result = url.set_scheme("foo"); | ||
|
@@ -1569,7 +1591,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("https://example.net")?; | ||
/// let result = url.set_scheme("foõ"); | ||
|
@@ -1585,7 +1607,7 @@ impl Url { | |
/// ``` | ||
/// use url::Url; | ||
/// # use url::ParseError; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let mut url = Url::parse("mailto:[email protected]")?; | ||
/// let result = url.set_scheme("https"); | ||
|
@@ -1598,8 +1620,8 @@ impl Url { | |
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> { | ||
let mut parser = Parser::for_setter(String::new()); | ||
let remaining = parser.parse_scheme(parser::Input::new(scheme))?; | ||
if !remaining.is_empty() || | ||
(!self.has_host() && SchemeType::from(&parser.serialization).is_special()) { | ||
if !(remaining.is_empty() && | ||
self.can_use_scheme(&parser.serialization)) { | ||
return Err(()) | ||
} | ||
let old_scheme_end = self.scheme_end; | ||
|
@@ -1622,6 +1644,14 @@ impl Url { | |
Ok(()) | ||
} | ||
|
||
fn can_use_scheme(&self, scheme: &str) -> bool { | ||
match SchemeType::from(scheme) { | ||
SchemeType::File => !(self.has_host() || self.has_port()), | ||
SchemeType::SpecialNotFile => self.has_host(), | ||
SchemeType::NotSpecial => true | ||
} | ||
} | ||
|
||
/// 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, | ||
|
@@ -1634,7 +1664,7 @@ impl Url { | |
/// ``` | ||
/// # if cfg!(unix) { | ||
/// use url::Url; | ||
/// | ||
/// | ||
/// # fn run() -> Result<(), ()> { | ||
/// let url = Url::from_file_path("/tmp/foo.txt")?; | ||
/// assert_eq!(url.as_str(), "file:///tmp/foo.txt"); | ||
|
This file contains hidden or 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
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.
I'm not sure if this is the best place to put the unit test.