Skip to content

Commit

Permalink
Merge pull request #631 from hyperium/server-request-ssl
Browse files Browse the repository at this point in the history
add ssl() method to server Requests
  • Loading branch information
seanmonstar committed Aug 12, 2015
2 parents 0455663 + 7909829 commit e1307b0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,61 @@ impl fmt::Debug for Box<NetworkStream + Send> {
}
}

impl NetworkStream {
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(traitobject::data(self))
}

unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
mem::transmute(traitobject::data_mut(self))
}

unsafe fn downcast_unchecked<T: 'static>(self: Box<NetworkStream>) -> Box<T> {
let raw: *mut NetworkStream = mem::transmute(self);
mem::transmute(traitobject::data_mut(raw))
}
}

impl NetworkStream {
/// Is the underlying type in this trait object a T?
#[inline]
pub fn is<T: Any>(&self) -> bool {
(*self).get_type() == TypeId::of::<T>()
}

/// If the underlying type is T, get a reference to the contained data.
#[inline]
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
Some(unsafe { self.downcast_ref_unchecked() })
} else {
None
}
}

/// If the underlying type is T, get a mutable reference to the contained
/// data.
#[inline]
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
Some(unsafe { self.downcast_mut_unchecked() })
} else {
None
}
}

/// If the underlying type is T, extract it.
#[inline]
pub fn downcast<T: Any>(self: Box<NetworkStream>)
-> Result<Box<T>, Box<NetworkStream>> {
if self.is::<T>() {
Ok(unsafe { self.downcast_unchecked() })
} else {
Err(self)
}
}
}

impl NetworkStream + Send {
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(traitobject::data(self))
Expand Down
32 changes: 32 additions & 0 deletions src/server/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ impl<'a, 'b: 'a> Request<'a, 'b> {
})
}

/// Get a reference to the underlying `NetworkStream`.
#[inline]
pub fn downcast_ref<T: NetworkStream>(&self) -> Option<&T> {
self.body.get_ref().get_ref().downcast_ref()
}

/// Get a reference to the underlying Ssl stream, if connected
/// over HTTPS.
///
/// # Example
///
/// ```rust
/// # extern crate hyper;
/// # #[cfg(feature = "openssl")]
/// extern crate openssl;
/// # #[cfg(feature = "openssl")]
/// use openssl::ssl::SslStream;
/// # fn main() {}
/// # #[cfg(feature = "openssl")]
/// # fn doc_ssl(req: hyper::server::Request) {
/// let maybe_ssl = req.ssl::<SslStream>();
/// # }
/// ```
#[inline]
pub fn ssl<T: NetworkStream>(&self) -> Option<&T> {
use ::net::HttpsStream;
match self.downcast_ref() {
Some(&HttpsStream::Https(ref s)) => Some(s),
_ => None
}
}

/// Deconstruct a Request into its constituent parts.
#[inline]
pub fn deconstruct(self) -> (SocketAddr, Method, Headers,
Expand Down

0 comments on commit e1307b0

Please sign in to comment.