From 311f0b23a9f787f3dc809babb81df4f8d59afd82 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 20 Jun 2018 00:36:32 +0600 Subject: [PATCH] cleanup more code --- src/fs.rs | 9 ++++----- src/httpmessage.rs | 6 ++---- src/httprequest.rs | 7 +++---- src/uri.rs | 2 ++ 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index e1cc58a5e..639626c57 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -10,7 +10,7 @@ use std::{cmp, io}; #[cfg(unix)] use std::os::unix::fs::MetadataExt; -use bytes::{BufMut, Bytes, BytesMut}; +use bytes::Bytes; use futures::{Async, Future, Poll, Stream}; use futures_cpupool::{CpuFuture, CpuPool}; use mime; @@ -439,14 +439,13 @@ impl Stream for ChunkedReadFile { self.fut = Some(self.cpu_pool.spawn_fn(move || { let max_bytes: usize; max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize; - let mut buf = BytesMut::from(Vec::with_capacity(max_bytes)); + let mut buf = Vec::with_capacity(max_bytes); file.seek(io::SeekFrom::Start(offset))?; - let nbytes = file.read(unsafe { buf.bytes_mut() })?; + let nbytes = file.read(buf.as_mut_slice())?; if nbytes == 0 { return Err(io::ErrorKind::UnexpectedEof.into()); } - unsafe { buf.advance_mut(nbytes) }; - Ok((file, buf.freeze())) + Ok((file, Bytes::from(buf))) })); self.poll() } diff --git a/src/httpmessage.rs b/src/httpmessage.rs index cac82f04c..5917e7fb3 100644 --- a/src/httpmessage.rs +++ b/src/httpmessage.rs @@ -634,13 +634,11 @@ mod tests { assert!(req.chunked().unwrap()); let mut headers = HeaderMap::new(); - let s = unsafe { - str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref()) - }; + let hdr = Bytes::from_static(b"some va\xadscc\xacas0xsdasdlue"); headers.insert( header::TRANSFER_ENCODING, - header::HeaderValue::from_str(s).unwrap(), + header::HeaderValue::from_shared(hdr).unwrap(), ); let req = HttpRequest::new( Method::GET, diff --git a/src/httprequest.rs b/src/httprequest.rs index 9f051aa86..d511d0902 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -1,9 +1,8 @@ //! HTTP Request message related code. -#![cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ptr))] use std::collections::HashMap; use std::net::SocketAddr; use std::rc::Rc; -use std::{cmp, fmt, io, mem, str}; +use std::{cmp, fmt, io, str}; use bytes::Bytes; use cookie::Cookie; @@ -446,13 +445,13 @@ impl HttpRequest { /// access the matched value for that segment. #[inline] pub fn match_info(&self) -> &Params { - unsafe { mem::transmute(&self.as_ref().params) } + &self.as_ref().params } /// Get mutable reference to request's Params. #[inline] pub(crate) fn match_info_mut(&mut self) -> &mut Params { - unsafe { mem::transmute(&mut self.as_mut().params) } + &mut self.as_mut().params } /// Checks if a connection should be kept alive. diff --git a/src/uri.rs b/src/uri.rs index 61ee93525..aa6f767dc 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -146,6 +146,8 @@ impl Quoter { } if let Some(data) = cloned { + // we get data from http::Uri, which does utf-8 checks already + // this code only decodes valid pct encoded values Some(unsafe { Rc::new(String::from_utf8_unchecked(data)) }) } else { None