impl<'a> TryFrom<&'a [u8]> for Scheme { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> { useself::Scheme2::*;
match Scheme2::parse_exact(s)? {
None => Err(ErrorKind::InvalidScheme.into()),
Standard(p) => Ok(Standard(p).into()),
Other(_) => { let bytes = Bytes::copy_from_slice(s);
// Safety: postcondition on parse_exact() means that s and // hence bytes are valid UTF-8. let string = unsafe { ByteStr::from_utf8_unchecked(bytes) };
impl Scheme2<usize> { // Postcondition: On all Ok() returns, s is valid UTF-8 fn parse_exact(s: &[u8]) -> Result<Scheme2<()>, InvalidUri> { match s {
b"http" => Ok(Protocol::Http.into()),
b"https" => Ok(Protocol::Https.into()),
_ => { if s.len() > MAX_SCHEME_LEN { return Err(ErrorKind::SchemeTooLong.into());
}
// check that each byte in s is a SCHEME_CHARS which implies // that it is a valid single byte UTF-8 code point. for &b in s { match SCHEME_CHARS[b as usize] {
b':' => { // Don't want :// here return Err(ErrorKind::InvalidScheme.into());
} 0 => { return Err(ErrorKind::InvalidScheme.into());
}
_ => {}
}
}
Ok(Scheme2::Other(()))
}
}
}
pub(super) fn parse(s: &[u8]) -> Result<Scheme2<usize>, InvalidUri> { if s.len() >= 7 { // Check for HTTP if s[..7].eq_ignore_ascii_case(b"http://") { // Prefix will be striped return Ok(Protocol::Http.into());
}
}
if s.len() >= 8 { // Check for HTTPs if s[..8].eq_ignore_ascii_case(b"https://") { return Ok(Protocol::Https.into());
}
}
if s.len() > 3 { for i in0..s.len() { let b = s[i];
match SCHEME_CHARS[b as usize] {
b':' => { // Not enough data remaining if s.len() < i + 3 { break;
}
// Not a scheme if &s[i + 1..i + 3] != b"//" { break;
}
if i > MAX_SCHEME_LEN { return Err(ErrorKind::SchemeTooLong.into());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.