Bug 1620769 - Convert net_IsValidScheme function to Rust r=valentin

Differential Revision: https://phabricator.services.mozilla.com/D66871

--HG--
extra : moz-landing-system : lando
This commit is contained in:
undef1nd 2020-03-15 12:09:57 +00:00
Родитель 7b0de70e19
Коммит 4cba8fb8e6
5 изменённых файлов: 29 добавлений и 20 удалений

Просмотреть файл

@ -453,18 +453,8 @@ nsresult net_ExtractURLScheme(const nsACString& inURI, nsACString& scheme) {
return NS_OK;
}
bool net_IsValidScheme(const char* scheme, uint32_t schemeLen) {
// first char must be alpha
if (!IsAsciiAlpha(*scheme)) return false;
// nsCStrings may have embedded nulls -- reject those too
for (; schemeLen; ++scheme, --schemeLen) {
if (!(IsAsciiAlpha(*scheme) || IsAsciiDigit(*scheme) || *scheme == '+' ||
*scheme == '.' || *scheme == '-'))
return false;
}
return true;
bool net_IsValidScheme(const nsACString& scheme) {
return rust_net_is_valid_scheme(scheme);
}
bool net_IsAbsoluteURL(const nsACString& uri) {

Просмотреть файл

@ -94,11 +94,7 @@ bool net_IsAbsoluteURL(const nsACString& inURL);
nsresult net_ExtractURLScheme(const nsACString& inURI, nsACString& scheme);
/* check that the given scheme conforms to RFC 2396 */
bool net_IsValidScheme(const char* scheme, uint32_t schemeLen);
inline bool net_IsValidScheme(const nsCString& scheme) {
return net_IsValidScheme(scheme.get(), scheme.Length());
}
bool net_IsValidScheme(const nsACString& scheme);
/**
* This function strips out all C0 controls and space at the beginning and end

Просмотреть файл

@ -112,7 +112,8 @@ nsBaseURLParser::ParseURL(const char* spec, int32_t specLen,
// spec = <scheme>:<authority>
// spec = <scheme>:<path-no-slashes>
//
if (!net_IsValidScheme(spec, colon - spec) || (*(colon + 1) == ':')) {
if (!net_IsValidScheme(nsDependentCSubstring(spec, colon - spec)) ||
(*(colon + 1) == ':')) {
return NS_ERROR_MALFORMED_URI;
}
SET_RESULT(scheme, offset, colon - spec);

Просмотреть файл

@ -20,6 +20,8 @@ bool rust_net_is_valid_ipv4_addr(const nsACString& aAddr);
bool rust_net_is_valid_ipv6_addr(const nsACString& aAddr);
bool rust_net_is_valid_scheme_char(const char aChar);
bool rust_net_is_valid_scheme(const nsACString& scheme);
}
#endif // RUST_NS_NET_HELPER

Просмотреть файл

@ -286,6 +286,26 @@ pub fn is_valid_ipv6_addr<'a>(addr: &'a [u8]) -> bool {
#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn rust_net_is_valid_scheme_char(a_char: u8) -> bool {
let a_char = a_char as char;
a_char.is_ascii_alphanumeric() || a_char == '+' || a_char == '.' || a_char == '-'
is_valid_scheme_char(a_char)
}
#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn rust_net_is_valid_scheme<'a>(scheme: &'a nsACString) -> bool {
if scheme.is_empty() {
return false;
}
// first char must be alpha
if !scheme[0].is_ascii_alphabetic() {
return false;
}
scheme[1..]
.iter()
.all(|a_char| is_valid_scheme_char(*a_char))
}
fn is_valid_scheme_char(a_char: u8) -> bool {
a_char.is_ascii_alphanumeric() || a_char == b'+' || a_char == b'.' || a_char == b'-'
}