cogo_http/http/
message.rs

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