1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/client/response.rs

130 lines
3.3 KiB
Rust
Raw Normal View History

2018-04-14 01:02:01 +02:00
use std::{fmt, str};
2018-01-28 07:03:03 +01:00
use bytes::Bytes;
use cookie::Cookie;
use futures::{Async, Poll, Stream};
use http::header::{self, HeaderValue};
2018-04-14 01:02:01 +02:00
use http::{HeaderMap, StatusCode, Version};
2018-01-28 07:03:03 +01:00
use error::{CookieParseError, PayloadError};
2018-04-14 01:02:01 +02:00
use httpmessage::HttpMessage;
2018-02-19 12:11:11 +01:00
use super::pipeline::Pipeline;
2018-01-28 07:03:03 +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
impl Default for ClientMessage {
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
}
/// An HTTP Client response
pub struct ClientResponse(ClientMessage, Option<Box<Pipeline>>);
impl HttpMessage for ClientResponse {
/// Get the headers from the response.
#[inline]
fn headers(&self) -> &HeaderMap {
&self.0.headers
}
}
2018-01-28 07:03:03 +01:00
impl ClientResponse {
pub(crate) fn new(msg: ClientMessage) -> ClientResponse {
ClientResponse(msg, None)
2018-02-19 12:11:11 +01:00
}
pub(crate) fn set_pipeline(&mut self, pl: Box<Pipeline>) {
self.1 = Some(pl);
}
/// Get the HTTP version of this response.
2018-01-28 07:03:03 +01:00
#[inline]
pub fn version(&self) -> Version {
self.0.version
2018-01-28 07:03:03 +01:00
}
/// Get the status from the server.
#[inline]
pub fn status(&self) -> StatusCode {
self.0.status
2018-01-28 07:03:03 +01:00
}
2018-03-07 07:36:34 +01:00
/// Load response cookies.
pub fn cookies(&self) -> Result<Vec<Cookie<'static>>, CookieParseError> {
let mut cookies = Vec::new();
for val in self.0.headers.get_all(header::SET_COOKIE).iter() {
let s = str::from_utf8(val.as_bytes()).map_err(CookieParseError::from)?;
cookies.push(Cookie::parse_encoded(s)?.into_owned());
}
Ok(cookies)
}
/// 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 {
2018-04-14 01:02:01 +02:00
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 {
2018-05-17 21:20:20 +02:00
let res = writeln!(f, "\nClientResponse {:?} {}", self.version(), self.status());
2018-04-09 23:25:30 +02:00
let _ = writeln!(f, " headers:");
2018-03-08 00:41:46 +01:00
for (key, val) in self.headers().iter() {
2018-04-09 23:25:30 +02:00
let _ = writeln!(f, " {:?}: {:?}", key, val);
2018-01-28 07:03:03 +01:00
}
res
}
}
/// Future that resolves to a complete request body.
2018-02-19 12:11:11 +01:00
impl Stream for ClientResponse {
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-03-08 07:40:46 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_debug() {
let mut resp = ClientResponse::new(ClientMessage::default());
resp.0
2018-05-17 21:20:20 +02:00
.headers
.insert(header::COOKIE, HeaderValue::from_static("cookie1=value1"));
resp.0
2018-05-17 21:20:20 +02:00
.headers
.insert(header::COOKIE, HeaderValue::from_static("cookie2=value2"));
2018-03-08 07:40:46 +01:00
let dbg = format!("{:?}", resp);
assert!(dbg.contains("ClientResponse"));
}
}