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-11-16 07:34:29 +01:00
|
|
|
use http::{HeaderMap, StatusCode, Version};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
use body::PayloadStream;
|
|
|
|
use error::PayloadError;
|
2018-10-23 03:18:05 +02:00
|
|
|
use extensions::Extensions;
|
2018-11-14 18:38:16 +01:00
|
|
|
use httpmessage::HttpMessage;
|
2018-11-16 07:34:29 +01:00
|
|
|
use request::{Message, MessageFlags, MessagePool, RequestHead};
|
2018-10-23 03:18:05 +02:00
|
|
|
use uri::Url;
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
use super::pipeline::Payload;
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
/// Client Response
|
|
|
|
pub struct ClientResponse {
|
|
|
|
pub(crate) inner: Rc<Message>,
|
2018-11-14 18:38:16 +01:00
|
|
|
pub(crate) payload: RefCell<Option<PayloadStream>>,
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
impl HttpMessage for ClientResponse {
|
|
|
|
type Stream = PayloadStream;
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
fn headers(&self) -> &HeaderMap {
|
2018-11-16 07:34:29 +01:00
|
|
|
&self.inner.head.headers
|
2018-11-14 18:38:16 +01:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
#[inline]
|
|
|
|
fn payload(&self) -> Self::Stream {
|
|
|
|
if let Some(payload) = self.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,
|
2018-11-16 07:34:29 +01:00
|
|
|
head: RequestHead::default(),
|
2018-10-23 03:18:05 +02:00
|
|
|
status: StatusCode::OK,
|
|
|
|
url: Url::default(),
|
|
|
|
flags: Cell::new(MessageFlags::empty()),
|
|
|
|
payload: RefCell::new(None),
|
|
|
|
extensions: RefCell::new(Extensions::new()),
|
|
|
|
}),
|
2018-11-14 18:38:16 +01:00
|
|
|
payload: RefCell::new(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 {
|
2018-11-16 07:34:29 +01:00
|
|
|
self.inner().head.version
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2018-11-16 07:34:29 +01:00
|
|
|
&self.inner().head.headers
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns mutable Request's headers.
|
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2018-11-16 07:34:29 +01:00
|
|
|
&mut self.inner_mut().head.headers
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
2018-11-14 18:38:16 +01:00
|
|
|
type Error = PayloadError;
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
if let Some(ref mut payload) = &mut *self.payload.borrow_mut() {
|
2018-11-14 07:53:30 +01:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|