Skip to content

Commit 7909829

Browse files
committed
feat(server): add Request.ssl() to get underlying ssl stream
Closes #627
1 parent 1a91835 commit 7909829

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/server/request.rs

+32
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ impl<'a, 'b: 'a> Request<'a, 'b> {
6464
})
6565
}
6666

67+
/// Get a reference to the underlying `NetworkStream`.
68+
#[inline]
69+
pub fn downcast_ref<T: NetworkStream>(&self) -> Option<&T> {
70+
self.body.get_ref().get_ref().downcast_ref()
71+
}
72+
73+
/// Get a reference to the underlying Ssl stream, if connected
74+
/// over HTTPS.
75+
///
76+
/// # Example
77+
///
78+
/// ```rust
79+
/// # extern crate hyper;
80+
/// # #[cfg(feature = "openssl")]
81+
/// extern crate openssl;
82+
/// # #[cfg(feature = "openssl")]
83+
/// use openssl::ssl::SslStream;
84+
/// # fn main() {}
85+
/// # #[cfg(feature = "openssl")]
86+
/// # fn doc_ssl(req: hyper::server::Request) {
87+
/// let maybe_ssl = req.ssl::<SslStream>();
88+
/// # }
89+
/// ```
90+
#[inline]
91+
pub fn ssl<T: NetworkStream>(&self) -> Option<&T> {
92+
use ::net::HttpsStream;
93+
match self.downcast_ref() {
94+
Some(&HttpsStream::Https(ref s)) => Some(s),
95+
_ => None
96+
}
97+
}
98+
6799
/// Deconstruct a Request into its constituent parts.
68100
#[inline]
69101
pub fn deconstruct(self) -> (SocketAddr, Method, Headers,

0 commit comments

Comments
 (0)