diff --git a/src/net.rs b/src/net.rs index 743195666e..b4f9ecf6e9 100644 --- a/src/net.rs +++ b/src/net.rs @@ -31,6 +31,22 @@ pub trait NetworkListener: Clone { fn incoming(&mut self) -> NetworkConnections { NetworkConnections(self) } + + /// Sets the read timeout for all streams that are accepted + fn set_read_timeout(&mut self, _: Option) { + // This default implementation is only here to prevent the addition of + // these methods from being a breaking change. They should be removed + // when the next breaking release is made. + warn!("Ignoring read timeout"); + } + + /// Sets the write timeout for all streams that are accepted + fn set_write_timeout(&mut self, _: Option) { + // This default implementation is only here to prevent the addition of + // these methods from being a breaking change. They should be removed + // when the next breaking release is made. + warn!("Ignoring write timeout"); + } } /// An iterator wrapper over a `NetworkAcceptor`. @@ -205,6 +221,9 @@ impl NetworkStream + Send { /// A `NetworkListener` for `HttpStream`s. pub struct HttpListener { listener: TcpListener, + + read_timeout : Option, + write_timeout: Option, } impl Clone for HttpListener { @@ -212,6 +231,9 @@ impl Clone for HttpListener { fn clone(&self) -> HttpListener { HttpListener { listener: self.listener.try_clone().unwrap(), + + read_timeout : self.read_timeout.clone(), + write_timeout: self.write_timeout.clone(), } } } @@ -220,6 +242,9 @@ impl From for HttpListener { fn from(listener: TcpListener) -> HttpListener { HttpListener { listener: listener, + + read_timeout : None, + write_timeout: None, } } } @@ -236,13 +261,24 @@ impl NetworkListener for HttpListener { #[inline] fn accept(&mut self) -> ::Result { - Ok(HttpStream(try!(self.listener.accept()).0)) + let stream = HttpStream(try!(self.listener.accept()).0); + try!(stream.set_read_timeout(self.read_timeout)); + try!(stream.set_write_timeout(self.write_timeout)); + Ok(stream) } #[inline] fn local_addr(&mut self) -> io::Result { self.listener.local_addr() } + + fn set_read_timeout(&mut self, duration: Option) { + self.read_timeout = duration; + } + + fn set_write_timeout(&mut self, duration: Option) { + self.write_timeout = duration; + } } #[cfg(windows)] @@ -537,6 +573,14 @@ impl NetworkListener for HttpsListener { fn local_addr(&mut self) -> io::Result { self.listener.local_addr() } + + fn set_read_timeout(&mut self, duration: Option) { + self.listener.set_read_timeout(duration) + } + + fn set_write_timeout(&mut self, duration: Option) { + self.listener.set_write_timeout(duration) + } } /// A connector that can protect HTTP streams using SSL. diff --git a/src/server/mod.rs b/src/server/mod.rs index 081fc740a8..46a420c1df 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -198,11 +198,13 @@ impl Server { /// Sets the read timeout for all Request reads. pub fn set_read_timeout(&mut self, dur: Option) { + self.listener.set_read_timeout(dur); self.timeouts.read = dur; } /// Sets the write timeout for all Response writes. pub fn set_write_timeout(&mut self, dur: Option) { + self.listener.set_write_timeout(dur); self.timeouts.write = dur; } }