From 77f4bbd778a26b4a3045967f42f8c6f5d398dc41 Mon Sep 17 00:00:00 2001 From: Valentin Gosu Date: Tue, 24 Feb 2015 14:10:29 +0200 Subject: [PATCH] Make urlutils public and disallow changing the scheme type to an incompatible type --- src/lib.rs | 11 +++++++++-- src/urlutils.rs | 12 +++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index da39f628b..a57d5e5f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -434,7 +434,6 @@ pub enum SchemeType { FileLike, } - impl SchemeType { pub fn default_port(&self) -> Option { match self { @@ -442,6 +441,14 @@ impl SchemeType { _ => 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 diff --git a/src/urlutils.rs b/src/urlutils.rs index 3421adf77..ffb156654 100644 --- a/src/urlutils.rs +++ b/src/urlutils.rs @@ -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<()>; @@ -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(()) },