Skip to content

Commit

Permalink
Merge pull request #83 from valenting/master
Browse files Browse the repository at this point in the history
Public urlutils and check type in set_scheme
  • Loading branch information
SimonSapin committed Feb 26, 2015
2 parents 6a20f9b + 77f4bbd commit d65fdce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit d65fdce

Please sign in to comment.