Merge pull request #631 from hyperium/server-request-ssl
add ssl() method to server Requests
This commit is contained in:
		
							
								
								
									
										55
									
								
								src/net.rs
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								src/net.rs
									
									
									
									
									
								
							| @@ -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 { | impl NetworkStream + Send { | ||||||
|     unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T { |     unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T { | ||||||
|         mem::transmute(traitobject::data(self)) |         mem::transmute(traitobject::data(self)) | ||||||
|   | |||||||
| @@ -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. |     /// Deconstruct a Request into its constituent parts. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn deconstruct(self) -> (SocketAddr, Method, Headers, |     pub fn deconstruct(self) -> (SocketAddr, Method, Headers, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user