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

Public urlutils and check type in set_scheme #83

Merged
merged 1 commit into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ use encoding::EncodingOverride;
mod encoding;
mod host;
mod parser;
mod urlutils;
pub mod urlutils;
pub mod percent_encoding;
pub mod form_urlencoded;
pub mod punycode;
Expand Down Expand Up @@ -434,14 +434,21 @@ pub enum SchemeType {
FileLike,
}


impl SchemeType {
pub fn default_port(&self) -> Option<u16> {
match self {
&SchemeType::Relative(default_port) => Some(default_port),
_ => None,
}
}
pub fn same_as(&self, other: SchemeType) -> bool {
match (self, other) {
(&SchemeType::NonRelative, SchemeType::NonRelative) => true,
(&SchemeType::Relative(_), SchemeType::Relative(_)) => true,
(&SchemeType::FileLike, SchemeType::FileLike) => true,
_ => false
}
}
}

/// http://url.spec.whatwg.org/#relative-scheme
Expand Down
12 changes: 7 additions & 5 deletions src/urlutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ use percent_encoding::{utf8_percent_encode_to, USERNAME_ENCODE_SET, PASSWORD_ENC


#[allow(dead_code)]
struct UrlUtilsWrapper<'a> {
url: &'a mut Url,
parser: &'a UrlParser<'a>,
pub struct UrlUtilsWrapper<'a> {
pub url: &'a mut Url,
pub parser: &'a UrlParser<'a>,
}


#[doc(hidden)]
trait UrlUtils {
pub trait UrlUtils {
fn set_scheme(&mut self, input: &str) -> ParseResult<()>;
fn set_username(&mut self, input: &str) -> ParseResult<()>;
fn set_password(&mut self, input: &str) -> ParseResult<()>;
Expand All @@ -40,6 +39,9 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
fn set_scheme(&mut self, input: &str) -> ParseResult<()> {
match ::parser::parse_scheme(input, Context::Setter) {
Some((scheme, _)) => {
if self.parser.get_scheme_type(&self.url.scheme).same_as(self.parser.get_scheme_type(&scheme)) {
return Err(ParseError::InvalidScheme);
}
self.url.scheme = scheme;
Ok(())
},
Expand Down