1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::io;
use std::net::{SocketAddr, Shutdown};
use std::time::Duration;
#[cfg(feature = "tls")] use http::tls::{WrappedStream, ServerSession};
use http::hyper::net::{HttpStream, NetworkStream};
use self::NetStream::*;
#[cfg(feature = "tls")] pub type HttpsStream = WrappedStream<ServerSession>;
#[derive(Clone)]
pub enum NetStream {
Http(HttpStream),
#[cfg(feature = "tls")]
Https(HttpsStream),
Empty,
}
impl io::Read for NetStream {
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
trace_!("NetStream::read()");
let res = match *self {
Http(ref mut stream) => stream.read(buf),
#[cfg(feature = "tls")] Https(ref mut stream) => stream.read(buf),
Empty => Ok(0),
};
trace_!("NetStream::read() -- complete");
res
}
}
impl io::Write for NetStream {
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
trace_!("NetStream::write()");
match *self {
Http(ref mut stream) => stream.write(buf),
#[cfg(feature = "tls")] Https(ref mut stream) => stream.write(buf),
Empty => Ok(0),
}
}
#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
match *self {
Http(ref mut stream) => stream.flush(),
#[cfg(feature = "tls")] Https(ref mut stream) => stream.flush(),
Empty => Ok(()),
}
}
}
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(),
Empty => Err(io::Error::from(io::ErrorKind::AddrNotAvailable)),
}
}
#[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),
Empty => Ok(()),
}
}
#[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),
Empty => Ok(()),
}
}
#[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),
Empty => Ok(()),
}
}
}