2018-01-29 23:44:25 +01:00
|
|
|
use std::{fmt, str};
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::UnsafeCell;
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
use bytes::Bytes;
|
2018-01-29 23:44:25 +01:00
|
|
|
use cookie::Cookie;
|
2018-02-28 00:03:28 +01:00
|
|
|
use futures::{Async, Poll, Stream};
|
2018-01-29 23:44:25 +01:00
|
|
|
use http::{HeaderMap, StatusCode, Version};
|
|
|
|
use http::header::{self, HeaderValue};
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
use httpmessage::HttpMessage;
|
|
|
|
use error::{CookieParseError, PayloadError};
|
2018-02-19 12:11:11 +01:00
|
|
|
|
|
|
|
use super::pipeline::Pipeline;
|
2018-01-28 07:03:03 +01:00
|
|
|
|
|
|
|
|
2018-01-29 23:44:25 +01:00
|
|
|
pub(crate) struct ClientMessage {
|
|
|
|
pub status: StatusCode,
|
|
|
|
pub version: Version,
|
|
|
|
pub headers: HeaderMap<HeaderValue>,
|
|
|
|
pub cookies: Option<Vec<Cookie<'static>>>,
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-01-29 23:44:25 +01:00
|
|
|
impl Default for ClientMessage {
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-01-29 23:44:25 +01:00
|
|
|
fn default() -> ClientMessage {
|
|
|
|
ClientMessage {
|
|
|
|
status: StatusCode::OK,
|
|
|
|
version: Version::HTTP_11,
|
|
|
|
headers: HeaderMap::with_capacity(16),
|
|
|
|
cookies: None,
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 21:44:14 +01:00
|
|
|
/// An HTTP Client response
|
2018-02-19 12:11:11 +01:00
|
|
|
pub struct ClientResponse(Rc<UnsafeCell<ClientMessage>>, Option<Box<Pipeline>>);
|
2018-01-29 23:44:25 +01:00
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
impl HttpMessage for ClientResponse {
|
|
|
|
/// Get the headers from the response.
|
|
|
|
#[inline]
|
|
|
|
fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.as_ref().headers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:03:03 +01:00
|
|
|
impl ClientResponse {
|
2018-01-29 23:44:25 +01:00
|
|
|
|
|
|
|
pub(crate) fn new(msg: ClientMessage) -> ClientResponse {
|
2018-02-19 12:11:11 +01:00
|
|
|
ClientResponse(Rc::new(UnsafeCell::new(msg)), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn set_pipeline(&mut self, pl: Box<Pipeline>) {
|
|
|
|
self.1 = Some(pl);
|
2018-01-29 23:44:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &ClientMessage {
|
|
|
|
unsafe{ &*self.0.get() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))]
|
|
|
|
fn as_mut(&self) -> &mut ClientMessage {
|
|
|
|
unsafe{ &mut *self.0.get() }
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:44:25 +01:00
|
|
|
/// Get the HTTP version of this response.
|
2018-01-28 07:03:03 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn version(&self) -> Version {
|
2018-01-29 23:44:25 +01:00
|
|
|
self.as_ref().version
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the status from the server.
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
2018-01-29 23:44:25 +01:00
|
|
|
self.as_ref().status
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 07:36:34 +01:00
|
|
|
/// Load response cookies.
|
2018-01-29 23:44:25 +01:00
|
|
|
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
|
|
|
if self.as_ref().cookies.is_none() {
|
|
|
|
let msg = self.as_mut();
|
|
|
|
let mut cookies = Vec::new();
|
2018-03-07 10:48:34 +01:00
|
|
|
for val in msg.headers.get_all(header::SET_COOKIE).iter() {
|
2018-03-08 00:41:46 +01:00
|
|
|
let s = str::from_utf8(val.as_bytes()).map_err(CookieParseError::from)?;
|
2018-03-07 10:48:34 +01:00
|
|
|
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
2018-01-29 23:44:25 +01:00
|
|
|
}
|
|
|
|
msg.cookies = Some(cookies)
|
|
|
|
}
|
|
|
|
Ok(self.as_ref().cookies.as_ref().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return request cookie.
|
|
|
|
pub fn cookie(&self, name: &str) -> Option<&Cookie> {
|
|
|
|
if let Ok(cookies) = self.cookies() {
|
|
|
|
for cookie in cookies {
|
|
|
|
if cookie.name() == name {
|
|
|
|
return Some(cookie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for ClientResponse {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let res = write!(
|
2018-01-29 23:44:25 +01:00
|
|
|
f, "\nClientResponse {:?} {}\n", self.version(), self.status());
|
2018-01-28 07:03:03 +01:00
|
|
|
let _ = write!(f, " headers:\n");
|
2018-03-08 00:41:46 +01:00
|
|
|
for (key, val) in self.headers().iter() {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, val);
|
2018-01-28 07:03:03 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 23:44:25 +01:00
|
|
|
|
|
|
|
/// Future that resolves to a complete request body.
|
2018-02-19 12:11:11 +01:00
|
|
|
impl Stream for ClientResponse {
|
2018-01-29 23:44:25 +01:00
|
|
|
type Item = Bytes;
|
|
|
|
type Error = PayloadError;
|
|
|
|
|
2018-02-19 12:11:11 +01:00
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
if let Some(ref mut pl) = self.1 {
|
|
|
|
pl.poll()
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(None))
|
2018-01-29 23:44:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-08 07:40:46 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
|
|
|
let resp = ClientResponse::new(ClientMessage::default());
|
|
|
|
resp.as_mut().headers.insert(
|
|
|
|
header::COOKIE, HeaderValue::from_static("cookie1=value1"));
|
|
|
|
resp.as_mut().headers.insert(
|
|
|
|
header::COOKIE, HeaderValue::from_static("cookie2=value2"));
|
|
|
|
|
|
|
|
let dbg = format!("{:?}", resp);
|
|
|
|
assert!(dbg.contains("ClientResponse"));
|
|
|
|
}
|
|
|
|
}
|