Add Response::content_length() method
This commit is contained in:
@@ -30,7 +30,7 @@ use bytes::{Buf, BufMut, BytesMut};
|
||||
use libflate::non_blocking::gzip;
|
||||
use futures::{Async, Future, Poll, Stream};
|
||||
use hyper::{HeaderMap};
|
||||
use hyper::header::{CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING, HeaderValue};
|
||||
use hyper::header::{CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
||||
|
||||
use super::{Body, Chunk};
|
||||
use error;
|
||||
@@ -118,17 +118,17 @@ impl Decoder {
|
||||
content_encoding_gzip = headers
|
||||
.get_all(CONTENT_ENCODING)
|
||||
.iter()
|
||||
.fold(false, |acc, enc| acc || enc == HeaderValue::from_static("gzip"));
|
||||
.fold(false, |acc, enc| acc || enc == "gzip");
|
||||
content_encoding_gzip ||
|
||||
headers
|
||||
.get_all(TRANSFER_ENCODING)
|
||||
.iter()
|
||||
.fold(false, |acc, enc| acc || enc == HeaderValue::from_static("gzip"))
|
||||
.fold(false, |acc, enc| acc || enc == "gzip")
|
||||
};
|
||||
if is_gzip {
|
||||
if let Some(content_length) = headers.get(CONTENT_LENGTH) {
|
||||
if content_length == "0" {
|
||||
warn!("GZipped response with content-length of 0");
|
||||
warn!("gzip response with content-length of 0");
|
||||
is_gzip = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use futures::{Async, Future, Poll, Stream};
|
||||
use futures::stream::Concat2;
|
||||
use hyper::{HeaderMap, StatusCode, Version};
|
||||
use hyper::client::connect::HttpInfo;
|
||||
use hyper::header::{CONTENT_LENGTH};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json;
|
||||
use url::Url;
|
||||
@@ -49,19 +50,6 @@ impl Response {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the final `Url` of this `Response`.
|
||||
#[inline]
|
||||
pub fn url(&self) -> &Url {
|
||||
&self.url
|
||||
}
|
||||
|
||||
/// Get the remote address used to get this `Response`.
|
||||
pub fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
self
|
||||
.extensions
|
||||
.get::<HttpInfo>()
|
||||
.map(|info| info.remote_addr())
|
||||
}
|
||||
|
||||
/// Get the `StatusCode` of this `Response`.
|
||||
#[inline]
|
||||
@@ -81,6 +69,35 @@ impl Response {
|
||||
&mut self.headers
|
||||
}
|
||||
|
||||
/// Get the final `Url` of this `Response`.
|
||||
#[inline]
|
||||
pub fn url(&self) -> &Url {
|
||||
&self.url
|
||||
}
|
||||
|
||||
/// Get the remote address used to get this `Response`.
|
||||
pub fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
self
|
||||
.extensions
|
||||
.get::<HttpInfo>()
|
||||
.map(|info| info.remote_addr())
|
||||
}
|
||||
|
||||
/// Get the content-length of this response, if known.
|
||||
///
|
||||
/// Reasons it may not be known:
|
||||
///
|
||||
/// - The server didn't send a `content-length` header.
|
||||
/// - The response is gzipped and automatically decoded (thus changing
|
||||
/// the actual decoded length).
|
||||
pub fn content_length(&self) -> Option<u64> {
|
||||
self
|
||||
.headers()
|
||||
.get(CONTENT_LENGTH)
|
||||
.and_then(|ct_len| ct_len.to_str().ok())
|
||||
.and_then(|ct_len| ct_len.parse().ok())
|
||||
}
|
||||
|
||||
/// Consumes the response, returning the body
|
||||
pub fn into_body(self) -> Decoder {
|
||||
self.body
|
||||
|
||||
Reference in New Issue
Block a user