From 9be2bc9243398c9ddd1f4ff849beca6b4b9a682a Mon Sep 17 00:00:00 2001 From: Manel Montilla Date: Fri, 17 Nov 2023 22:25:24 +0100 Subject: [PATCH] WIP --- wruster/src/http/mod.rs | 29 +++++++++++------------ wruster/src/streams/cancellable_stream.rs | 3 +-- wruster_handlers/src/lib.rs | 1 - 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/wruster/src/http/mod.rs b/wruster/src/http/mod.rs index 1091f9d..eef8f9a 100644 --- a/wruster/src/http/mod.rs +++ b/wruster/src/http/mod.rs @@ -371,23 +371,22 @@ impl Body { pub fn write(&mut self, to: &mut T) -> HttpResult<()> { let src = &mut self.content; - // We consider to use different buffers of the default io::copy when you - // the length of the body is larger tha 2 MB. When the content to write - // is large, the io::copy function uses too short buffers by default. We - // use this technique: https://github.com/rust-lang/rust/pull/78641 to - // avoid that problem by setting the buffer size to 1 MB. - debug!("writing body with content length {:?}", self.content_length); - let mut dest = match self.content_length { - x if x > 2 ^ 20 * 2 => { - debug!("using BufWriter to write the body"); - BufWriter::with_capacity(20 * (2 ^ 20), to) + // When the content to write is large (>2MB's) the io::copy function + // uses buffers that are too short, to avoid that we use this technique: + // https://github.com/rust-lang/rust/pull/78641 to set the buffer to + // 1MB. + let res = match self.content_length { + x if x > u64::pow(2, 20) * 2 => { + let buff_size = usize::pow(2, 20); + let mut dest = BufWriter::with_capacity(buff_size, to); + io::copy(src, &mut dest) } - _ => BufWriter::new(to), + _ => io::copy(src, to), }; - if let Err(err) = io::copy(src, &mut dest) { - return Err(HttpError::Unknown(err.to_string())); - }; - Ok(()) + match res { + Err(err) => Err(HttpError::Unknown(err.to_string())), + Ok(_) => Ok(()), + } } /** diff --git a/wruster/src/streams/cancellable_stream.rs b/wruster/src/streams/cancellable_stream.rs index 8d3e44f..b64bd71 100644 --- a/wruster/src/streams/cancellable_stream.rs +++ b/wruster/src/streams/cancellable_stream.rs @@ -2,9 +2,8 @@ use super::BaseStream; use crate::log::debug; use polling::Event; use std::{ - io::{self, Write}, + io, net::Shutdown, - os::macos::raw, sync::{ atomic::{self, AtomicBool, Ordering}, Arc, RwLock, diff --git a/wruster_handlers/src/lib.rs b/wruster_handlers/src/lib.rs index 1dee85a..288acd9 100644 --- a/wruster_handlers/src/lib.rs +++ b/wruster_handlers/src/lib.rs @@ -68,7 +68,6 @@ pub fn serve_static(dir: &str, request: &Request) -> Response { }; let mime_type = mime_guess::from_path(path).first_or_octet_stream(); let mut headers = Headers::new(); - let content = BufReader::with_capacity(10 * 2 ^ 20, content); let content: Box = Box::new(content); headers.add(Header { name: String::from("Content-Length"),