Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
manelmontilla committed Nov 17, 2023
1 parent e833c4d commit 9be2bc9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
29 changes: 14 additions & 15 deletions wruster/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,23 +371,22 @@ impl Body {
pub fn write<T: io::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(()),
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions wruster/src/streams/cancellable_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion wruster_handlers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Read> = Box::new(content);
headers.add(Header {
name: String::from("Content-Length"),
Expand Down

0 comments on commit 9be2bc9

Please sign in to comment.