use std::{fmt, net::SocketAddr, time::Duration};
use http::{HeaderMap, StatusCode, Version};
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
use tokio::time::Instant;
use url::Url;
use crate::{Body, Error, ErrorKind, Result, redact::SanitizedUrl};
pub(crate) const DEFAULT_BODY_LIMIT: usize = 16 * 1024 * 1024;
pub struct Response {
status: StatusCode,
version: Version,
headers: HeaderMap,
effective_url: Url,
redirect_count: u32,
remote_addr: Option<SocketAddr>,
body: Body,
body_limit: usize,
}
#[derive(Default)]
pub(crate) struct ResponseBuilder {
status: Option<StatusCode>,
version: Option<Version>,
headers: HeaderMap,
effective_url: Option<Url>,
remote_addr: Option<SocketAddr>,
body: Body,
}
impl Response {
pub(crate) fn builder() -> ResponseBuilder {
ResponseBuilder::default()
}
#[must_use]
pub fn status(&self) -> StatusCode {
self.status
}
#[must_use]
pub fn version(&self) -> Version {
self.version
}
#[must_use]
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
#[must_use]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.headers
}
#[must_use]
pub fn effective_url(&self) -> &Url {
&self.effective_url
}
#[must_use]
pub fn redirect_count(&self) -> u32 {
self.redirect_count
}
#[must_use]
pub fn remote_addr(&self) -> Option<SocketAddr> {
self.remote_addr
}
#[must_use]
pub fn body(&self) -> &Body {
&self.body
}
#[must_use]
pub fn body_mut(&mut self) -> &mut Body {
&mut self.body
}
#[must_use]
pub fn into_body(self) -> Body {
self.body
}
#[must_use]
pub fn trailers(&self) -> Option<&HeaderMap> {
self.body.trailers()
}
pub async fn collect(mut self) -> Result<Self> {
self.body = self.body.collect_limited(self.body_limit).await?;
Ok(self)
}
pub async fn bytes(self) -> Result<Vec<u8>> {
let response = self.collect().await?;
response
.body
.into_buffered_bytes()
.ok_or_else(|| Error::new(ErrorKind::BodyIo, "body did not become buffered"))
}
pub async fn text(self) -> Result<String> {
String::from_utf8(self.bytes().await?)
.map_err(|_| Error::new(ErrorKind::Decoding, "response body is not valid UTF-8"))
}
#[cfg(feature = "json")]
pub async fn json<T>(self) -> Result<T>
where
T: DeserializeOwned,
{
serde_json::from_slice(&self.bytes().await?)
.map_err(|_| Error::new(ErrorKind::Decoding, "response JSON could not be decoded"))
}
pub async fn discard(self) -> Result<()> {
self.body.discard_limited(self.body_limit).await
}
pub(crate) fn set_redirect_count(&mut self, count: u32) {
self.redirect_count = count;
}
pub(crate) fn enforce_body_timeouts(&mut self, deadline: Instant, read_idle: Duration) {
self.body = std::mem::take(&mut self.body).enforce_timeouts(deadline, read_idle);
}
pub(crate) fn set_body_limit(&mut self, limit: usize) {
self.body_limit = limit;
}
pub(crate) async fn discard_for_redirect(self, limit: usize) -> Result<()> {
self.body.discard_limited(limit).await
}
}
impl ResponseBuilder {
#[must_use]
pub(crate) fn status(mut self, status: StatusCode) -> Self {
self.status = Some(status);
self
}
#[must_use]
pub(crate) fn version(mut self, version: Version) -> Self {
self.version = Some(version);
self
}
#[must_use]
pub(crate) fn headers(mut self, headers: HeaderMap) -> Self {
self.headers.extend(headers);
self
}
#[must_use]
pub(crate) fn effective_url(mut self, url: Url) -> Self {
self.effective_url = Some(url);
self
}
#[must_use]
pub(crate) fn remote_addr(mut self, address: SocketAddr) -> Self {
self.remote_addr = Some(address);
self
}
#[must_use]
pub(crate) fn body(mut self, body: impl Into<Body>) -> Self {
self.body = body.into();
self
}
pub(crate) fn build(self) -> Result<Response> {
let status = self
.status
.ok_or(Error::invalid_response("response status is required"))?;
let version = self
.version
.ok_or(Error::invalid_response("response HTTP version is required"))?;
let effective_url = self
.effective_url
.ok_or(Error::invalid_response("response URL is required"))?;
if !matches!(effective_url.scheme(), "http" | "https")
|| effective_url.host_str().is_none()
|| !effective_url.username().is_empty()
|| effective_url.password().is_some()
|| effective_url.fragment().is_some()
{
return Err(Error::invalid_response(
"response URL must be an absolute HTTP URL without userinfo or a fragment",
));
}
Ok(Response {
status,
version,
headers: self.headers,
effective_url,
redirect_count: 0,
remote_addr: self.remote_addr,
body: self.body,
body_limit: DEFAULT_BODY_LIMIT,
})
}
}
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("effective_url", &SanitizedUrl(&self.effective_url))
.field("redirect_count", &self.redirect_count)
.field("remote_addr", &self.remote_addr)
.field("body", &self.body)
.finish()
}
}
impl fmt::Debug for ResponseBuilder {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("ResponseBuilder")
.field("status", &self.status)
.field("version", &self.version)
.field("header_count", &self.headers.len())
.field("has_effective_url", &self.effective_url.is_some())
.field("body", &self.body)
.finish()
}
}
#[cfg(test)]
mod describe_response {
use http::HeaderValue;
#[cfg(feature = "json")]
use http::header::CONTENT_TYPE;
use super::*;
fn response(body: impl Into<Body>) -> Response {
Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse("https://example.com/path?token=secret").unwrap())
.body(body)
.build()
.unwrap()
}
#[test]
fn it_custom_response_requires_status_version_and_url() {
assert_eq!(
Response::builder().build().unwrap_err().to_string(),
"invalid response: response status is required"
);
assert_eq!(
Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.build()
.unwrap_err()
.to_string(),
"invalid response: response URL is required"
);
assert_eq!(
Response::builder()
.status(StatusCode::OK)
.effective_url(Url::parse("https://example.com").unwrap())
.build()
.unwrap_err()
.to_string(),
"invalid response: response HTTP version is required"
);
for url in [
"file:///tmp/value",
"https://user:pass@example.com/",
"https://example.com/#fragment",
] {
assert!(
Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse(url).unwrap())
.build()
.is_err()
);
}
}
#[test]
fn it_custom_response_uses_neutral_policy_defaults() {
let response = response(Body::empty());
assert_eq!(response.redirect_count, 0);
assert_eq!(response.body_limit, DEFAULT_BODY_LIMIT);
}
#[test]
fn it_response_builder_headers_replace_matching_names_and_preserve_repeated_values() {
let mut initial = HeaderMap::new();
initial.append("x-mode", HeaderValue::from_static("old-first"));
initial.append("x-mode", HeaderValue::from_static("old-second"));
initial.insert("x-stable", HeaderValue::from_static("kept"));
let mut replacement = HeaderMap::new();
replacement.append("x-mode", HeaderValue::from_static("new-first"));
replacement.append("x-mode", HeaderValue::from_static("new-second"));
let response = Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse("https://example.com").unwrap())
.headers(initial)
.headers(replacement)
.build()
.unwrap();
let values = response
.headers()
.get_all("x-mode")
.iter()
.map(|value| value.to_str().unwrap())
.collect::<Vec<_>>();
assert_eq!(values, ["new-first", "new-second"]);
assert_eq!(response.headers()["x-stable"], "kept");
}
#[tokio::test]
async fn it_collection_is_bounded_and_exact_at_limit() {
let exact = response("four");
assert_eq!(
exact.collect().await.unwrap().body().as_bytes().unwrap(),
b"four"
);
let mut over = Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse("https://example.com").unwrap())
.body("fives")
.build()
.unwrap();
over.set_body_limit(4);
assert_eq!(over.bytes().await.unwrap_err().kind(), ErrorKind::BodyLimit);
}
#[tokio::test]
async fn it_collect_preserves_response_metadata_while_buffering_body() {
let effective_url = Url::parse("https://example.com/final?secret=hidden").unwrap();
let remote_addr = "127.0.0.1:443".parse().unwrap();
let mut headers = HeaderMap::new();
headers.insert("x-response", HeaderValue::from_static("preserved"));
let mut response = Response::builder()
.status(StatusCode::CREATED)
.version(Version::HTTP_2)
.headers(headers)
.effective_url(effective_url.clone())
.remote_addr(remote_addr)
.body("four")
.build()
.unwrap();
response.set_redirect_count(2);
response.set_body_limit(4);
let response = response.collect().await.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
assert_eq!(response.version(), Version::HTTP_2);
assert_eq!(response.headers()["x-response"], "preserved");
assert_eq!(response.effective_url(), &effective_url);
assert_eq!(response.redirect_count(), 2);
assert_eq!(response.remote_addr(), Some(remote_addr));
assert_eq!(response.body().as_bytes(), Some(&b"four"[..]));
}
#[cfg(feature = "json")]
#[tokio::test]
async fn it_json_collects_before_decoding() {
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
let value: serde_json::Value = Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse("https://example.com").unwrap())
.headers(headers)
.body(r#"{"ok":true}"#)
.build()
.unwrap()
.json()
.await
.unwrap();
assert_eq!(value, serde_json::json!({ "ok": true }));
}
#[test]
fn it_response_debug_redacts_url_query_headers_and_body() {
let mut headers = HeaderMap::new();
headers.insert("authorization", HeaderValue::from_static("secret-token"));
let response = Response::builder()
.status(StatusCode::OK)
.version(Version::HTTP_11)
.effective_url(Url::parse("https://example.com/path?token=query-secret").unwrap())
.headers(headers)
.body("body-secret")
.build()
.unwrap();
let debug = format!("{response:?}");
for secret in ["query-secret", "secret-token", "body-secret"] {
assert!(!debug.contains(secret), "debug leaked {secret}: {debug}");
}
}
}