2018-10-23 03:18:05 +02:00
|
|
|
use std::cell::{Cell, Ref, RefCell, RefMut};
|
|
|
|
use std::fmt;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::{Async, Poll, Stream};
|
2018-10-23 03:18:05 +02:00
|
|
|
use http::{HeaderMap, Method, StatusCode, Version};
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
use body::BodyStream;
|
|
|
|
use error::Error;
|
2018-10-23 03:18:05 +02:00
|
|
|
use extensions::Extensions;
|
|
|
|
use request::{Message, MessageFlags, MessagePool};
|
|
|
|
use uri::Url;
|
|
|
|
|
|
|
|
/// Client Response
|
|
|
|
pub struct ClientResponse {
|
|
|
|
pub(crate) inner: Rc<Message>,
|
2018-11-14 07:53:30 +01:00
|
|
|
pub(crate) payload: Option<BodyStream>,
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
// impl HttpMessage for ClientResponse {
|
|
|
|
// type Stream = Payload;
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
// fn headers(&self) -> &HeaderMap {
|
|
|
|
// &self.inner.headers
|
|
|
|
// }
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
// #[inline]
|
|
|
|
// fn payload(&self) -> Payload {
|
|
|
|
// if let Some(payload) = self.inner.payload.borrow_mut().take() {
|
|
|
|
// payload
|
|
|
|
// } else {
|
|
|
|
// Payload::empty()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
impl ClientResponse {
|
|
|
|
/// Create new Request instance
|
|
|
|
pub fn new() -> ClientResponse {
|
|
|
|
ClientResponse::with_pool(MessagePool::pool())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new Request instance with pool
|
|
|
|
pub(crate) fn with_pool(pool: &'static MessagePool) -> ClientResponse {
|
|
|
|
ClientResponse {
|
|
|
|
inner: Rc::new(Message {
|
|
|
|
pool,
|
|
|
|
method: Method::GET,
|
|
|
|
status: StatusCode::OK,
|
|
|
|
url: Url::default(),
|
|
|
|
version: Version::HTTP_11,
|
|
|
|
headers: HeaderMap::with_capacity(16),
|
|
|
|
flags: Cell::new(MessageFlags::empty()),
|
|
|
|
payload: RefCell::new(None),
|
|
|
|
extensions: RefCell::new(Extensions::new()),
|
|
|
|
}),
|
2018-11-14 07:53:30 +01:00
|
|
|
payload: None,
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn inner(&self) -> &Message {
|
|
|
|
self.inner.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn inner_mut(&mut self) -> &mut Message {
|
|
|
|
Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the Request Version.
|
|
|
|
#[inline]
|
|
|
|
pub fn version(&self) -> Version {
|
|
|
|
self.inner().version
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the status from the server.
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
|
|
|
self.inner().status
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns Request's headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.inner().headers
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns mutable Request's headers.
|
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
|
|
|
&mut self.inner_mut().headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a connection should be kept alive.
|
|
|
|
#[inline]
|
|
|
|
pub fn keep_alive(&self) -> bool {
|
|
|
|
self.inner().flags.get().contains(MessageFlags::KEEPALIVE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Request extensions
|
|
|
|
#[inline]
|
|
|
|
pub fn extensions(&self) -> Ref<Extensions> {
|
|
|
|
self.inner().extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the request's extensions
|
|
|
|
#[inline]
|
|
|
|
pub fn extensions_mut(&self) -> RefMut<Extensions> {
|
|
|
|
self.inner().extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
impl Stream for ClientResponse {
|
|
|
|
type Item = Bytes;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Error> {
|
|
|
|
if let Some(ref mut payload) = self.payload {
|
|
|
|
payload.poll()
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
impl Drop for ClientResponse {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if Rc::strong_count(&self.inner) == 1 {
|
|
|
|
self.inner.pool.release(self.inner.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for ClientResponse {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "\nClientResponse {:?} {}", self.version(), self.status(),)?;
|
|
|
|
writeln!(f, " headers:")?;
|
|
|
|
for (key, val) in self.headers().iter() {
|
|
|
|
writeln!(f, " {:?}: {:?}", key, val)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|