Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl BodyChannel {
pub fn send<T: Into<Vec<u8>>>(&self, data: T) -> io::Result<()> {
self.0
.send(Ok(data.into().into()))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "body closed"))
.map_err(|_| io::Error::other("body closed"))
}

/// Send a trailer header. Note that trailers are buffered, and are only sent after the last
Expand All @@ -82,14 +82,12 @@ impl BodyChannel {
pub fn send_trailers(&self, trailers: HeaderMap) -> io::Result<()> {
self.0
.send(Ok(Chunk::Trailers(trailers)))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "body closed"))
.map_err(|_| io::Error::other("body closed"))
}

/// Aborts the body in an abnormal fashion.
pub fn abort(self) {
self.0
.send(Err(io::Error::new(io::ErrorKind::Other, "aborted")))
.ok();
self.0.send(Err(io::Error::other("aborted"))).ok();
}
}

Expand Down Expand Up @@ -253,7 +251,7 @@ impl TryFrom<File> for Body {
fn try_from(file: File) -> Result<Self, Self::Error> {
match file.metadata() {
Ok(meta) if meta.is_file() => Ok(Body::from_reader(file, meta.len() as usize)),
Ok(_) => Err(io::Error::new(io::ErrorKind::Other, "not a file")),
Ok(_) => Err(io::Error::other("not a file")),
Err(err) => Err(err),
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ where
request::write_request(req, &mut writer)?;
writer.flush()?;

let res = response::parse_response(reader)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
let res = response::parse_response(reader).map_err(io::Error::other)?;

let asks_for_close = res
.headers()
Expand Down
15 changes: 3 additions & 12 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ pub(crate) fn write_request<B: HttpBody>(
match (content_length, body.len()) {
(Some(len), Some(body_len)) => {
if len.0 != body_len {
return Err(io::Error::new(
io::ErrorKind::Other,
"content-length doesn't match body length",
));
return Err(io::Error::other("content-length doesn't match body length"));
}
Encoding::FixedLength(len.0)
}
Expand All @@ -162,21 +159,15 @@ pub(crate) fn write_request<B: HttpBody>(
headers.typed_insert::<headers::TransferEncoding>(headers::TransferEncoding::chunked());
Encoding::Chunked
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"could not determine the size of the body",
));
return Err(io::Error::other("could not determine the size of the body"));
};

let version = if version == Version::HTTP_11 {
"HTTP/1.1"
} else if version == Version::HTTP_10 {
"HTTP/1.0"
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"unsupported http version",
));
return Err(io::Error::other("unsupported http version"));
};

stream.write_all(format!("{method} {uri} {version}\r\n").as_bytes())?;
Expand Down
5 changes: 1 addition & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ pub(crate) fn write_response<B: HttpBody>(
match (content_length, body.len()) {
(Some(len), Some(body_len)) => {
if len.0 != body_len {
return Err(io::Error::new(
io::ErrorKind::Other,
"content-length doesn't match body length",
));
return Err(io::Error::other("content-length doesn't match body length"));
}
Encoding::FixedLength(len.0)
}
Expand Down
16 changes: 8 additions & 8 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ where
/// A listening HTTP server that accepts HTTP 1 connections.
pub struct Server<'a> {
#[cfg(feature = "threadpool")]
thread_pool: ThreadPool,
max_threads: usize,
incoming: Box<dyn Iterator<Item = Connection> + 'a>,
}

Expand Down Expand Up @@ -165,9 +165,10 @@ impl Server<'_> {
S: Service,
S: Send + Clone + 'static,
{
let thread_pool = ThreadPool::new(self.max_threads);
for conn in self.incoming {
let mut app = service.clone();
self.thread_pool.execute(move || {
thread_pool.execute(move || {
serve(conn, &mut app).ok();
});
}
Expand Down Expand Up @@ -253,9 +254,10 @@ impl Server<'_> {
M: MakeService + 'static,
<M as MakeService>::Service: Send,
{
let thread_pool = ThreadPool::new(self.max_threads);
for conn in self.incoming {
if let Ok(mut handler) = make_service.call(&conn) {
self.thread_pool.execute(move || {
thread_pool.execute(move || {
serve(conn, &mut handler).ok();
});
}
Expand Down Expand Up @@ -407,7 +409,7 @@ impl ServerBuilder {
) -> Server<'a> {
Server {
#[cfg(feature = "threadpool")]
thread_pool: ThreadPool::new(self.max_threads),
max_threads: self.max_threads,
incoming: Box::new(conns.into_iter().filter_map(move |conn| {
let conn = conn.into();
conn.set_read_timeout(self.read_timeout).ok()?;
Expand Down Expand Up @@ -505,9 +507,7 @@ fn serve<A: Service>(conn: Connection, app: &mut A) -> io::Result<()> {
};
}

let mut res = app
.call(req)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
let mut res = app.call(req).map_err(io::Error::other)?;

*res.version_mut() = version;

Expand Down Expand Up @@ -540,7 +540,7 @@ fn serve<A: Service>(conn: Connection, app: &mut A) -> io::Result<()> {
}
}
Err(ParseError::ConnectionClosed) => break,
Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err)),
Err(err) => return Err(io::Error::other(err)),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ impl RustlsConnection {
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.0
.lock()
.map_err(|_err| io::Error::new(io::ErrorKind::Other, "Failed to aquire lock"))?
.map_err(|_err| io::Error::other("Failed to aquire lock"))?
.sock
.peer_addr()
}

pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.0
.lock()
.map_err(|_err| io::Error::new(io::ErrorKind::Other, "Failed to aquire lock"))?
.map_err(|_err| io::Error::other("Failed to aquire lock"))?
.sock
.local_addr()
}
Expand All @@ -59,7 +59,7 @@ impl Read for RustlsConnection {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0
.lock()
.map_err(|_err| io::Error::new(io::ErrorKind::Other, "Failed to aquire lock"))?
.map_err(|_err| io::Error::other("Failed to aquire lock"))?
.read(buf)
}
}
Expand All @@ -68,14 +68,14 @@ impl Write for RustlsConnection {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0
.lock()
.map_err(|_err| io::Error::new(io::ErrorKind::Other, "Failed to aquire lock"))?
.map_err(|_err| io::Error::other("Failed to aquire lock"))?
.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.0
.lock()
.map_err(|_err| io::Error::new(io::ErrorKind::Other, "Failed to aquire lock"))?
.map_err(|_err| io::Error::other("Failed to aquire lock"))?
.flush()
}
}