2019-02-08 04:26:12 +01:00
|
|
|
use std::cell::RefCell;
|
2019-02-12 20:07:42 +01:00
|
|
|
use std::{fmt, mem};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
use bytes::Bytes;
|
2019-02-12 20:07:42 +01:00
|
|
|
use futures::{Poll, Stream};
|
2018-11-16 07:34:29 +01:00
|
|
|
use http::{HeaderMap, StatusCode, Version};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::error::PayloadError;
|
|
|
|
use crate::httpmessage::HttpMessage;
|
|
|
|
use crate::message::{Head, ResponseHead};
|
2019-02-12 20:07:42 +01:00
|
|
|
use crate::payload::{Payload, PayloadStream};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
/// Client Response
|
|
|
|
pub struct ClientResponse {
|
2018-11-17 04:28:07 +01:00
|
|
|
pub(crate) head: ResponseHead,
|
2019-02-12 20:07:42 +01:00
|
|
|
pub(crate) payload: RefCell<Payload>,
|
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-17 04:28:07 +01:00
|
|
|
&self.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]
|
2019-02-12 20:07:42 +01:00
|
|
|
fn payload(&self) -> Payload<Self::Stream> {
|
|
|
|
mem::replace(&mut *self.payload.borrow_mut(), Payload::None)
|
2018-11-14 18:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
impl ClientResponse {
|
|
|
|
/// Create new Request instance
|
|
|
|
pub fn new() -> ClientResponse {
|
|
|
|
ClientResponse {
|
2018-11-17 04:28:07 +01:00
|
|
|
head: ResponseHead::default(),
|
2019-02-12 20:07:42 +01:00
|
|
|
payload: RefCell::new(Payload::None),
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-11-17 04:28:07 +01:00
|
|
|
pub(crate) fn head(&self) -> &ResponseHead {
|
|
|
|
&self.head
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-11-17 04:28:07 +01:00
|
|
|
pub(crate) fn head_mut(&mut self) -> &mut ResponseHead {
|
|
|
|
&mut self.head
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the Request Version.
|
|
|
|
#[inline]
|
|
|
|
pub fn version(&self) -> Version {
|
2018-11-17 18:03:35 +01:00
|
|
|
self.head().version
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the status from the server.
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
2018-11-17 04:28:07 +01:00
|
|
|
self.head().status
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns Request's headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2018-11-17 04:28:07 +01:00
|
|
|
&self.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-17 04:28:07 +01:00
|
|
|
&mut self.head_mut().headers
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a connection should be kept alive.
|
|
|
|
#[inline]
|
|
|
|
pub fn keep_alive(&self) -> bool {
|
2018-11-19 02:52:56 +01:00
|
|
|
self.head().keep_alive()
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
2019-02-07 20:06:05 +01:00
|
|
|
|
|
|
|
/// Set response payload
|
2019-02-12 20:07:42 +01:00
|
|
|
pub fn set_payload(&mut self, payload: Payload) {
|
|
|
|
*self.payload.get_mut() = payload;
|
2019-02-07 20:06:05 +01:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
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> {
|
2019-02-12 20:07:42 +01:00
|
|
|
self.payload.get_mut().poll()
|
2018-11-14 07:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|