use std::{fmt, net::SocketAddr, pin::Pin, time::Duration};
use bytes::Bytes;
use http::{Extensions, HeaderMap, StatusCode, Uri, Version, header};
use http_body::Body as _;
use serde::de::DeserializeOwned;
use crate::{Body, Error, Limits, Result, stream::ResponseStream};
pub struct Response {
inner: http::Response<Body>,
url: Uri,
content_length: Option<u64>,
remote_addr: Option<SocketAddr>,
limits: Limits,
info: ResponseInfo,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ResponseInfo {
attempts: usize,
redirects: usize,
elapsed: Duration,
}
impl ResponseInfo {
#[must_use]
pub const fn attempts(self) -> usize {
self.attempts
}
#[must_use]
pub const fn redirects(self) -> usize {
self.redirects
}
#[must_use]
pub const fn elapsed(self) -> Duration {
self.elapsed
}
}
impl Response {
pub(crate) fn new(
parts: http::response::Parts,
body: Body,
url: Uri,
remote_addr: Option<SocketAddr>,
limits: Limits,
) -> Self {
let content_length = body.size_hint().exact();
Self {
inner: http::Response::from_parts(parts, body),
url,
content_length,
remote_addr,
limits,
info: ResponseInfo {
attempts: 1,
redirects: 0,
elapsed: Duration::ZERO,
},
}
}
pub(crate) fn into_parts(
self,
) -> (
http::Response<Body>,
Uri,
Option<u64>,
Option<SocketAddr>,
Limits,
ResponseInfo,
) {
(
self.inner,
self.url,
self.content_length,
self.remote_addr,
self.limits,
self.info,
)
}
pub(crate) fn record_dispatch(&mut self, attempts: usize, redirects: usize, elapsed: Duration) {
self.info = ResponseInfo {
attempts,
redirects,
elapsed,
};
}
#[must_use]
pub fn status(&self) -> StatusCode {
self.inner.status()
}
#[must_use]
pub fn version(&self) -> Version {
self.inner.version()
}
#[must_use]
pub fn headers(&self) -> &HeaderMap {
self.inner.headers()
}
#[must_use]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
self.inner.headers_mut()
}
#[must_use]
pub fn extensions(&self) -> &Extensions {
self.inner.extensions()
}
#[must_use]
pub fn extensions_mut(&mut self) -> &mut Extensions {
self.inner.extensions_mut()
}
#[must_use]
pub fn url(&self) -> &Uri {
&self.url
}
#[must_use]
pub const fn content_length(&self) -> Option<u64> {
self.content_length
}
#[must_use]
pub const fn remote_addr(&self) -> Option<SocketAddr> {
self.remote_addr
}
#[must_use]
pub const fn info(&self) -> ResponseInfo {
self.info
}
pub fn error_for_status(self) -> Result<Self> {
self.error_for_status_ref()?;
Ok(self)
}
pub fn error_for_status_ref(&self) -> Result<&Self> {
if self.status().is_client_error() || self.status().is_server_error() {
return Err(Error::status_error(self.status()));
}
Ok(self)
}
#[must_use]
pub fn body(&self) -> &Body {
self.inner.body()
}
#[must_use]
pub fn body_mut(&mut self) -> &mut Body {
self.inner.body_mut()
}
#[must_use]
pub fn into_body(self) -> Body {
self.inner.into_body()
}
#[must_use]
pub fn stream(self) -> ResponseStream {
let item_limit = self.limits.stream_item_bytes();
ResponseStream::new(self.into_body(), item_limit)
}
pub async fn chunk(&mut self) -> Result<Option<Bytes>> {
loop {
let frame =
std::future::poll_fn(|context| Pin::new(self.inner.body_mut()).poll_frame(context))
.await;
match frame {
Some(Ok(frame)) => {
if let Ok(data) = frame.into_data()
&& !data.is_empty()
{
return Ok(Some(data));
}
}
Some(Err(error)) => return Err(error),
None => return Ok(None),
}
}
}
#[must_use]
pub fn upgrade(mut self) -> crate::Upgrade {
self.extensions_mut()
.remove::<crate::Upgrade>()
.unwrap_or_else(crate::Upgrade::unavailable)
}
pub async fn bytes(self) -> Result<Bytes> {
let limit = self.limits.response_body_bytes();
collect(self.into_body(), limit).await
}
pub async fn bytes_with_limit(self, limit: usize) -> Result<Bytes> {
if limit == 0 {
return Err(Error::request(
"response body limit must be greater than zero",
));
}
collect(self.into_body(), limit).await
}
pub async fn text(self) -> Result<String> {
let content_type = content_type(self.headers()).map(ToOwned::to_owned);
decode_text(&self.bytes().await?, content_type.as_deref())
}
pub async fn text_with_limit(self, limit: usize) -> Result<String> {
let content_type = content_type(self.headers()).map(ToOwned::to_owned);
decode_text(
&self.bytes_with_limit(limit).await?,
content_type.as_deref(),
)
}
pub async fn json<T>(self) -> Result<T>
where
T: DeserializeOwned,
{
crate::content::decode_json(&self.bytes().await?, "response JSON could not be decoded")
}
pub async fn json_with_limit<T>(self, limit: usize) -> Result<T>
where
T: DeserializeOwned,
{
crate::content::decode_json(
&self.bytes_with_limit(limit).await?,
"response JSON could not be decoded",
)
}
pub async fn form<T>(self) -> Result<T>
where
T: DeserializeOwned,
{
crate::content::decode_form(&self.bytes().await?, "response form could not be decoded")
}
pub async fn form_with_limit<T>(self, limit: usize) -> Result<T>
where
T: DeserializeOwned,
{
crate::content::decode_form(
&self.bytes_with_limit(limit).await?,
"response form could not be decoded",
)
}
pub async fn copy_to<W>(mut self, writer: &mut W) -> Result<u64>
where
W: futures_io::AsyncWrite + Unpin + ?Sized,
{
use futures_util::AsyncWriteExt as _;
let mut written = 0_u64;
while let Some(chunk) = self.chunk().await? {
writer.write_all(&chunk).await.map_err(|_| Error::body())?;
written = written
.checked_add(chunk.len() as u64)
.ok_or_else(Error::body)?;
}
Ok(written)
}
pub async fn discard(self) -> Result<()> {
let mut body = self.into_body();
while let Some(frame) =
std::future::poll_fn(|context| Pin::new(&mut body).poll_frame(context)).await
{
frame?;
}
Ok(())
}
}
pub(crate) fn decode_text(bytes: &[u8], content_type: Option<&str>) -> Result<String> {
let charset = content_type
.and_then(|value| value.parse::<mime::Mime>().ok())
.and_then(|media_type| {
media_type
.get_param(mime::CHARSET)
.map(|charset| charset.as_str().to_owned())
});
let encoding = charset
.as_deref()
.and_then(|charset| encoding_rs::Encoding::for_label(charset.as_bytes()))
.unwrap_or(encoding_rs::UTF_8);
let (text, _, malformed) = encoding.decode(bytes);
if malformed {
return Err(Error::decode(
"response text is invalid for its declared character encoding",
));
}
Ok(text.into_owned())
}
fn content_type(headers: &HeaderMap) -> Option<&str> {
headers.get(header::CONTENT_TYPE)?.to_str().ok()
}
impl fmt::Debug for Response {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Response")
.field("status", &self.status())
.field("version", &self.version())
.field("header_count", &self.headers().len())
.field("content_length", &self.content_length())
.field("remote_addr", &self.remote_addr())
.field("info", &self.info)
.finish_non_exhaustive()
}
}
async fn collect(mut body: Body, limit: usize) -> Result<Bytes> {
if body.size_hint().lower() > limit as u64 {
return Err(Error::body_too_large());
}
let capacity = body
.size_hint()
.exact()
.and_then(|length| usize::try_from(length).ok())
.filter(|length| *length <= limit);
let mut first = None;
let mut bytes = None;
while let Some(frame) =
std::future::poll_fn(|context| Pin::new(&mut body).poll_frame(context)).await
{
let frame = frame?;
if let Ok(data) = frame.into_data() {
let length = bytes
.as_ref()
.map_or_else(|| first.as_ref().map_or(0, Bytes::len), Vec::len)
.checked_add(data.len())
.ok_or_else(Error::body_too_large)?;
if length > limit {
return Err(Error::body_too_large());
}
if let Some(bytes) = &mut bytes {
bytes.extend_from_slice(&data);
} else if let Some(previous) = first.take() {
let mut collected = Vec::with_capacity(capacity.unwrap_or(length).max(length));
collected.extend_from_slice(&previous);
collected.extend_from_slice(&data);
bytes = Some(collected);
} else {
first = Some(data);
}
}
}
Ok(match bytes {
Some(bytes) => Bytes::from(bytes),
None => first.unwrap_or_default(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use http_body::{Body as HttpBody, Frame, SizeHint};
use std::{
convert::Infallible,
pin::Pin,
task::{Context, Poll},
};
const COLLECTION_LIMIT: usize = 16 * 1024 * 1024;
fn response(body: impl Into<Body>) -> Response {
let inner = http::Response::new(body.into());
let (parts, body) = inner.into_parts();
Response::new(
parts,
body,
"https://example.com/resource".parse().unwrap(),
None,
Limits::default().response_body(COLLECTION_LIMIT),
)
}
#[tokio::test]
async fn decoders_consume_the_body_and_classify_invalid_data() {
assert_eq!(response("tez").text().await.unwrap(), "tez");
assert_eq!(
response(br#"{"name":"Ada"}"#.as_slice())
.json::<serde_json::Value>()
.await
.unwrap(),
serde_json::json!({ "name": "Ada" })
);
let error = response(vec![0xff]).text().await.unwrap_err();
assert!(error.is_decode());
}
#[test]
fn text_decoding_honors_declared_charsets_and_rejects_malformed_data() {
assert_eq!(
decode_text(
&[0x63, 0x61, 0x66, 0xe9],
Some("text/plain; charset=windows-1252")
)
.unwrap(),
"café"
);
assert!(decode_text(&[0xff], Some("text/plain; charset=utf-8")).is_err());
}
#[tokio::test]
async fn collecting_one_data_frame_reuses_its_bytes() {
let original = Bytes::from_static(b"single allocation");
let collected = response(original.clone()).bytes().await.unwrap();
assert_eq!(collected, original);
assert_eq!(collected.as_ptr(), original.as_ptr());
}
#[tokio::test]
async fn collection_limit_accepts_the_boundary_and_rejects_the_next_byte() {
let exact = vec![b'x'; COLLECTION_LIMIT];
assert_eq!(
collect(Body::from(exact), COLLECTION_LIMIT)
.await
.unwrap()
.len(),
COLLECTION_LIMIT
);
let too_large = vec![b'y'; COLLECTION_LIMIT + 1];
let error = collect(Body::from(too_large), COLLECTION_LIMIT)
.await
.unwrap_err();
assert!(error.is_body_too_large());
assert!(error.is_body());
}
#[tokio::test]
async fn collection_does_not_treat_an_upper_hint_as_actual_length() {
struct LooseUpperBound(Option<Bytes>);
impl HttpBody for LooseUpperBound {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(
self: Pin<&mut Self>,
_context: &mut Context<'_>,
) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
Poll::Ready(self.get_mut().0.take().map(Frame::data).map(Ok))
}
fn size_hint(&self) -> SizeHint {
let mut hint = SizeHint::new();
hint.set_upper(COLLECTION_LIMIT as u64 + 1);
hint
}
}
let bytes = collect(
Body::wrap(LooseUpperBound(Some(Bytes::from_static(b"small")))),
COLLECTION_LIMIT,
)
.await
.unwrap();
assert_eq!(bytes, "small");
}
#[test]
fn debug_never_includes_header_values_or_body_data() {
let mut headers = HeaderMap::new();
headers.insert("authorization", "Bearer secret".parse().unwrap());
headers.insert(header::CONTENT_LENGTH, "1024".parse().unwrap());
let mut inner = http::Response::new(Body::from("body secret"));
*inner.headers_mut() = headers;
let (parts, body) = inner.into_parts();
let response = Response::new(
parts,
body,
"https://example.com/resource?token=secret".parse().unwrap(),
Some("127.0.0.1:8080".parse().unwrap()),
Limits::default().response_body(COLLECTION_LIMIT),
);
let debug = format!("{response:?}");
assert!(debug.contains("content_length: Some(11)"));
assert!(debug.contains("remote_addr: Some(127.0.0.1:8080)"));
assert!(!debug.contains("secret"));
assert!(!debug.contains("authorization"));
}
}