|
| 1 | +//! Defines the `HttpMessage` trait that serves to encapsulate the operations of a single |
| 2 | +//! request-response cycle on any HTTP connection. |
| 3 | +
|
| 4 | +use std::fmt::Debug; |
| 5 | +use std::any::{Any, TypeId}; |
| 6 | +use std::io::{Read, Write}; |
| 7 | + |
| 8 | +use std::mem; |
| 9 | + |
| 10 | +use typeable::Typeable; |
| 11 | + |
| 12 | +use header::Headers; |
| 13 | +use http::RawStatus; |
| 14 | +use url::Url; |
| 15 | + |
| 16 | +use method; |
| 17 | +use version; |
| 18 | +use traitobject; |
| 19 | + |
| 20 | +/// Describes a request. |
| 21 | +#[derive(Clone, Debug)] |
| 22 | +pub struct RequestHead { |
| 23 | + /// The headers of the request |
| 24 | + pub headers: Headers, |
| 25 | + /// The method of the request |
| 26 | + pub method: method::Method, |
| 27 | + /// The URL of the request |
| 28 | + pub url: Url, |
| 29 | +} |
| 30 | + |
| 31 | +/// Describes a response. |
| 32 | +#[derive(Clone, Debug)] |
| 33 | +pub struct ResponseHead { |
| 34 | + /// The headers of the reponse |
| 35 | + pub headers: Headers, |
| 36 | + /// The raw status line of the response |
| 37 | + pub raw_status: RawStatus, |
| 38 | + /// The HTTP/2 version which generated the response |
| 39 | + pub version: version::HttpVersion, |
| 40 | +} |
| 41 | + |
| 42 | +/// The trait provides an API for sending an receiving HTTP messages. |
| 43 | +pub trait HttpMessage: Write + Read + Send + Any + Typeable + Debug { |
| 44 | + /// Initiates a new outgoing request. |
| 45 | + /// |
| 46 | + /// Only the request's head is provided (in terms of the `RequestHead` struct). |
| 47 | + /// |
| 48 | + /// After this, the `HttpMessage` instance can be used as an `io::Write` in order to write the |
| 49 | + /// body of the request. |
| 50 | + fn set_outgoing(&mut self, head: RequestHead) -> ::Result<RequestHead>; |
| 51 | + /// Obtains the incoming response and returns its head (i.e. the `ResponseHead` struct) |
| 52 | + /// |
| 53 | + /// After this, the `HttpMessage` instance can be used as an `io::Read` in order to read out |
| 54 | + /// the response body. |
| 55 | + fn get_incoming(&mut self) -> ::Result<ResponseHead>; |
| 56 | + |
| 57 | + /// Closes the underlying HTTP connection. |
| 58 | + fn close_connection(&mut self) -> ::Result<()>; |
| 59 | +} |
| 60 | + |
| 61 | +impl HttpMessage { |
| 62 | + unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T { |
| 63 | + mem::transmute(traitobject::data(self)) |
| 64 | + } |
| 65 | + |
| 66 | + unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T { |
| 67 | + mem::transmute(traitobject::data_mut(self)) |
| 68 | + } |
| 69 | + |
| 70 | + unsafe fn downcast_unchecked<T: 'static>(self: Box<HttpMessage>) -> Box<T> { |
| 71 | + let raw: *mut HttpMessage = mem::transmute(self); |
| 72 | + mem::transmute(traitobject::data_mut(raw)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl HttpMessage { |
| 77 | + /// Is the underlying type in this trait object a T? |
| 78 | + #[inline] |
| 79 | + pub fn is<T: Any>(&self) -> bool { |
| 80 | + (*self).get_type() == TypeId::of::<T>() |
| 81 | + } |
| 82 | + |
| 83 | + /// If the underlying type is T, get a reference to the contained data. |
| 84 | + #[inline] |
| 85 | + pub fn downcast_ref<T: Any>(&self) -> Option<&T> { |
| 86 | + if self.is::<T>() { |
| 87 | + Some(unsafe { self.downcast_ref_unchecked() }) |
| 88 | + } else { |
| 89 | + None |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// If the underlying type is T, get a mutable reference to the contained |
| 94 | + /// data. |
| 95 | + #[inline] |
| 96 | + pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { |
| 97 | + if self.is::<T>() { |
| 98 | + Some(unsafe { self.downcast_mut_unchecked() }) |
| 99 | + } else { |
| 100 | + None |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// If the underlying type is T, extract it. |
| 105 | + #[inline] |
| 106 | + pub fn downcast<T: Any>(self: Box<HttpMessage>) |
| 107 | + -> Result<Box<T>, Box<HttpMessage>> { |
| 108 | + if self.is::<T>() { |
| 109 | + Ok(unsafe { self.downcast_unchecked() }) |
| 110 | + } else { |
| 111 | + Err(self) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments