Remove unsafe 'from_utf8_unchecked'.

This commit is contained in:
Sergio Benitez 2018-02-25 19:47:06 -08:00
parent 15bce5907e
commit d0f002c3d7
1 changed files with 8 additions and 11 deletions

View File

@ -1,5 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::str::from_utf8_unchecked; use std::str::from_utf8;
use std::cmp::min; use std::cmp::min;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::io::{self, Write}; use std::io::{self, Write};
@ -193,21 +193,18 @@ impl Rocket {
let (min_len, max_len) = ("_method=get".len(), "_method=delete".len()); let (min_len, max_len) = ("_method=get".len(), "_method=delete".len());
let is_form = req.content_type().map_or(false, |ct| ct.is_form()); let is_form = req.content_type().map_or(false, |ct| ct.is_form());
if is_form && req.method() == Method::Post && data_len >= min_len { if is_form && req.method() == Method::Post && data_len >= min_len {
// We're only using this for comparison and throwing it away if let Ok(form) = from_utf8(&data.peek()[..min(data_len, max_len)]) {
// afterwards, so it doesn't matter if we have invalid UTF8. let method: Option<Result<Method, _>> = FormItems::from(form)
let form = unsafe { .filter(|&(key, _)| key.as_str() == "_method")
from_utf8_unchecked(&data.peek()[..min(data_len, max_len)]) .map(|(_, value)| value.parse())
}; .next();
if let Some((key, value)) = FormItems::from(form).next() { if let Some(Ok(method)) = method {
if key == "_method" {
if let Ok(method) = value.parse() {
req.set_method(method); req.set_method(method);
} }
} }
} }
} }
}
#[inline] #[inline]
pub(crate) fn dispatch<'s, 'r>(&'s self, pub(crate) fn dispatch<'s, 'r>(&'s self,