use http::header::{CONTENT_TYPE, LOCATION};
use http::{HeaderValue, StatusCode};
use serde::Serialize;
use crate::body::Body;
use crate::{AppError, Json};
mod into_response;
mod parts;
pub mod sse;
pub use into_response::IntoResponse;
pub use parts::{AppendHeaders, IntoResponseParts, ResponseParts, TryIntoHeaderError};
pub type Response<B = Body> = http::Response<B>;
#[derive(Clone, Copy, Debug)]
#[must_use]
pub struct Html<T>(pub T);
impl<T: IntoResponse> IntoResponse for Html<T> {
fn into_response(self) -> Response {
let content_type = HeaderValue::from_static("text/html; charset=utf-8");
([(CONTENT_TYPE, content_type)], self.0).into_response()
}
}
impl<T> From<T> for Html<T> {
fn from(inner: T) -> Self {
Self(inner)
}
}
#[must_use = "a redirect does nothing until it is returned from a handler"]
#[derive(Clone, Debug)]
pub struct Redirect {
status: StatusCode,
location: String,
}
impl Redirect {
pub fn to(location: &str) -> Self {
Self::with_status(StatusCode::SEE_OTHER, location)
}
pub fn temporary(location: &str) -> Self {
Self::with_status(StatusCode::TEMPORARY_REDIRECT, location)
}
pub fn permanent(location: &str) -> Self {
Self::with_status(StatusCode::PERMANENT_REDIRECT, location)
}
#[must_use]
pub fn status_code(&self) -> StatusCode {
self.status
}
#[must_use]
pub fn location(&self) -> &str {
&self.location
}
fn with_status(status: StatusCode, location: &str) -> Self {
Self {
status,
location: location.to_owned(),
}
}
}
impl IntoResponse for Redirect {
fn into_response(self) -> Response {
match HeaderValue::try_from(self.location) {
Ok(location) => (self.status, [(LOCATION, location)]).into_response(),
Err(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response(),
}
}
}
#[derive(Clone, Debug)]
pub struct Created<T>(pub String, pub T);
impl<T: Serialize> IntoResponse for Created<T> {
fn into_response(self) -> Response {
let Created(location, body) = self;
let location = match HeaderValue::from_str(&location) {
Ok(value) => value,
Err(error) => {
return AppError::internal(format!("invalid Location header: {error}"))
.into_response();
}
};
let mut response = (StatusCode::CREATED, Json(body)).into_response();
response.headers_mut().insert(LOCATION, location);
response
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoContent;
impl IntoResponse for NoContent {
fn into_response(self) -> Response {
StatusCode::NO_CONTENT.into_response()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn created_sets_location_and_status() {
let response =
Created("/api/deals/1".to_owned(), serde_json::json!({"id": 1})).into_response();
assert_eq!(response.status(), StatusCode::CREATED);
assert_eq!(response.headers()[LOCATION], "/api/deals/1");
assert_eq!(
response.headers()[http::header::CONTENT_TYPE],
"application/json"
);
assert_eq!(NoContent.into_response().status(), StatusCode::NO_CONTENT);
let bad = Created("/a\nb".to_owned(), ()).into_response();
assert_eq!(bad.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn redirects_have_their_status_and_location() {
let response = Redirect::to("/next").into_response();
assert_eq!(response.status(), StatusCode::SEE_OTHER);
assert_eq!(response.headers()[LOCATION], "/next");
assert_eq!(
Redirect::temporary("/t").status_code(),
StatusCode::TEMPORARY_REDIRECT
);
assert_eq!(
Redirect::permanent("/p").status_code(),
StatusCode::PERMANENT_REDIRECT
);
assert_eq!(Redirect::permanent("/p").location(), "/p");
}
#[test]
fn a_location_that_cannot_be_a_header_is_a_500() {
let response = Redirect::to("/a\nb").into_response();
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert!(!response.headers().contains_key(LOCATION));
}
}