2017-06-06 20:41:04 +00:00
|
|
|
use std::io;
|
2017-04-21 03:30:41 +00:00
|
|
|
use std::net::{SocketAddr, Shutdown};
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2018-06-07 13:34:47 +00:00
|
|
|
#[cfg(feature = "tls")] use http::tls::{WrappedStream, ServerSession};
|
2017-04-21 03:30:41 +00:00
|
|
|
use http::hyper::net::{HttpStream, NetworkStream};
|
|
|
|
|
|
|
|
use self::NetStream::*;
|
|
|
|
|
2017-04-23 05:17:21 +00:00
|
|
|
#[cfg(feature = "tls")] pub type HttpsStream = WrappedStream<ServerSession>;
|
|
|
|
|
2017-04-21 03:30:41 +00:00
|
|
|
// This is a representation of all of the possible network streams we might get.
|
|
|
|
// This really shouldn't be necessary, but, you know, Hyper.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum NetStream {
|
|
|
|
Http(HttpStream),
|
|
|
|
#[cfg(feature = "tls")]
|
2017-04-23 05:17:21 +00:00
|
|
|
Https(HttpsStream),
|
2017-06-06 20:41:04 +00:00
|
|
|
Empty,
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl io::Read for NetStream {
|
|
|
|
#[inline(always)]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2017-04-23 05:06:33 +00:00
|
|
|
trace_!("NetStream::read()");
|
|
|
|
let res = match *self {
|
2017-04-21 03:30:41 +00:00
|
|
|
Http(ref mut stream) => stream.read(buf),
|
2017-06-06 20:41:04 +00:00
|
|
|
#[cfg(feature = "tls")] Https(ref mut stream) => stream.read(buf),
|
|
|
|
Empty => Ok(0),
|
2017-04-23 05:06:33 +00:00
|
|
|
};
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2017-04-23 05:06:33 +00:00
|
|
|
trace_!("NetStream::read() -- complete");
|
|
|
|
res
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl io::Write for NetStream {
|
|
|
|
#[inline(always)]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2017-04-23 05:06:33 +00:00
|
|
|
trace_!("NetStream::write()");
|
2017-04-21 03:30:41 +00:00
|
|
|
match *self {
|
|
|
|
Http(ref mut stream) => stream.write(buf),
|
2017-06-06 20:41:04 +00:00
|
|
|
#[cfg(feature = "tls")] Https(ref mut stream) => stream.write(buf),
|
|
|
|
Empty => Ok(0),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Http(ref mut stream) => stream.flush(),
|
2017-06-06 20:41:04 +00:00
|
|
|
#[cfg(feature = "tls")] Https(ref mut stream) => stream.flush(),
|
|
|
|
Empty => Ok(()),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkStream for NetStream {
|
|
|
|
#[inline(always)]
|
|
|
|
fn peer_addr(&mut self) -> io::Result<SocketAddr> {
|
|
|
|
match *self {
|
|
|
|
Http(ref mut stream) => stream.peer_addr(),
|
|
|
|
#[cfg(feature = "tls")] Https(ref mut stream) => stream.peer_addr(),
|
2017-06-06 20:41:04 +00:00
|
|
|
Empty => Err(io::Error::from(io::ErrorKind::AddrNotAvailable)),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Http(ref stream) => stream.set_read_timeout(dur),
|
|
|
|
#[cfg(feature = "tls")] Https(ref stream) => stream.set_read_timeout(dur),
|
2017-06-06 20:41:04 +00:00
|
|
|
Empty => Ok(()),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Http(ref stream) => stream.set_write_timeout(dur),
|
|
|
|
#[cfg(feature = "tls")] Https(ref stream) => stream.set_write_timeout(dur),
|
2017-06-06 20:41:04 +00:00
|
|
|
Empty => Ok(()),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn close(&mut self, how: Shutdown) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Http(ref mut stream) => stream.close(how),
|
|
|
|
#[cfg(feature = "tls")] Https(ref mut stream) => stream.close(how),
|
2017-06-06 20:41:04 +00:00
|
|
|
Empty => Ok(()),
|
2017-04-21 03:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|