mirror of
https://github.com/actix/actix-web.git
synced 2025-01-04 22:38:44 +00:00
remove unsafes
This commit is contained in:
parent
0be5448597
commit
445ea043dd
1 changed files with 18 additions and 16 deletions
34
src/fs.rs
34
src/fs.rs
|
@ -13,10 +13,10 @@ use std::os::unix::fs::MetadataExt;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::{Async, Future, Poll, Stream};
|
use futures::{Async, Future, Poll, Stream};
|
||||||
use futures_cpupool::{CpuFuture, CpuPool};
|
use futures_cpupool::{CpuFuture, CpuPool};
|
||||||
|
use htmlescape::encode_minimal as escape_html_entity;
|
||||||
use mime;
|
use mime;
|
||||||
use mime_guess::{get_mime_type, guess_mime_type};
|
use mime_guess::{get_mime_type, guess_mime_type};
|
||||||
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
|
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
|
||||||
use htmlescape::encode_minimal as escape_html_entity;
|
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
|
use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
|
||||||
|
@ -442,14 +442,11 @@ impl Stream for ChunkedReadFile {
|
||||||
let max_bytes: usize;
|
let max_bytes: usize;
|
||||||
max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize;
|
max_bytes = cmp::min(size.saturating_sub(counter), 65_536) as usize;
|
||||||
let mut buf = Vec::with_capacity(max_bytes);
|
let mut buf = Vec::with_capacity(max_bytes);
|
||||||
// safe because memory is initialized/overwritten immediately
|
|
||||||
unsafe { buf.set_len(max_bytes); }
|
|
||||||
file.seek(io::SeekFrom::Start(offset))?;
|
file.seek(io::SeekFrom::Start(offset))?;
|
||||||
let nbytes = file.read(buf.as_mut_slice())?;
|
let nbytes = file.by_ref().take(max_bytes as u64).read_to_end(&mut buf)?;
|
||||||
if nbytes == 0 {
|
if nbytes == 0 {
|
||||||
return Err(io::ErrorKind::UnexpectedEof.into());
|
return Err(io::ErrorKind::UnexpectedEof.into());
|
||||||
}
|
}
|
||||||
unsafe { buf.set_len(nbytes); }
|
|
||||||
Ok((file, Bytes::from(buf)))
|
Ok((file, Bytes::from(buf)))
|
||||||
}));
|
}));
|
||||||
self.poll()
|
self.poll()
|
||||||
|
@ -518,15 +515,13 @@ fn directory_listing<S>(
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
body,
|
body,
|
||||||
"<li><a href=\"{}\">{}/</a></li>",
|
"<li><a href=\"{}\">{}/</a></li>",
|
||||||
file_url,
|
file_url, file_name
|
||||||
file_name
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
body,
|
body,
|
||||||
"<li><a href=\"{}\">{}</a></li>",
|
"<li><a href=\"{}\">{}</a></li>",
|
||||||
file_url,
|
file_url, file_name
|
||||||
file_name
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -805,6 +800,8 @@ impl HttpRange {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use application::App;
|
use application::App;
|
||||||
use http::{header, Method, StatusCode};
|
use http::{header, Method, StatusCode};
|
||||||
|
@ -1130,14 +1127,19 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
{
|
||||||
|
let te = response
|
||||||
|
.headers()
|
||||||
|
.get(header::TRANSFER_ENCODING)
|
||||||
|
.unwrap()
|
||||||
|
.to_str()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(te, "chunked");
|
||||||
|
}
|
||||||
|
|
||||||
let te = response
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
.headers()
|
let data = Bytes::from(fs::read("tests/test.binary").unwrap());
|
||||||
.get(header::TRANSFER_ENCODING)
|
assert_eq!(bytes, data);
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(te, "chunked");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue