From 188206a90382111e2d8337febf4f6965c0730b6e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 11 Jun 2024 00:36:46 +0100 Subject: [PATCH] feat: Html responder (#3399) --- actix-multipart/README.md | 4 +-- actix-web/CHANGES.md | 1 + actix-web/README.md | 2 +- actix-web/src/types/html.rs | 66 +++++++++++++++++++++++++++++++++++++ actix-web/src/types/mod.rs | 2 ++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 actix-web/src/types/html.rs diff --git a/actix-multipart/README.md b/actix-multipart/README.md index ef7630637..d61347f32 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -19,8 +19,6 @@ Multipart form support for Actix Web. ## Examples -[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) - ```rust use actix_web::{post, App, HttpServer, Responder}; @@ -58,6 +56,8 @@ async fn main() -> std::io::Result<()> { +[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) + Curl request : ```bash diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index bb0844e05..54c7045ae 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `web::Html` responder. - Add `HttpRequest::full_url()` method to get the complete URL of the request. ### Fixed diff --git a/actix-web/README.md b/actix-web/README.md index 8b4375bdd..3f9d3e0d5 100644 --- a/actix-web/README.md +++ b/actix-web/README.md @@ -109,4 +109,4 @@ This project is licensed under either of the following licenses, at your option: ## Code of Conduct -Contribution to the actix-web repo is organized under the terms of the Contributor Covenant. The Actix team promises to intervene to uphold that code of conduct. +Contribution to the `actix/actix-web` repo is organized under the terms of the Contributor Covenant. The Actix team promises to intervene to uphold that code of conduct. diff --git a/actix-web/src/types/html.rs b/actix-web/src/types/html.rs new file mode 100644 index 000000000..c370ee07b --- /dev/null +++ b/actix-web/src/types/html.rs @@ -0,0 +1,66 @@ +//! Semantic HTML responder. See [`Html`]. + +use crate::{ + http::{ + header::{self, ContentType, TryIntoHeaderValue}, + StatusCode, + }, + HttpRequest, HttpResponse, Responder, +}; + +/// Semantic HTML responder. +/// +/// When used as a responder, creates a 200 OK response, sets the correct HTML content type, and +/// uses the string passed to [`Html::new()`] as the body. +/// +/// ``` +/// # use actix_web::web::Html; +/// Html::new("

Hello, World!

") +/// # ; +/// ``` +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct Html(String); + +impl Html { + /// Constructs a new `Html` responder. + pub fn new(html: impl Into) -> Self { + Self(html.into()) + } +} + +impl Responder for Html { + type Body = String; + + fn respond_to(self, _req: &HttpRequest) -> HttpResponse { + let mut res = HttpResponse::with_body(StatusCode::OK, self.0); + res.headers_mut().insert( + header::CONTENT_TYPE, + ContentType::html().try_into_value().unwrap(), + ); + res + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test::TestRequest; + + #[test] + fn responder() { + let req = TestRequest::default().to_http_request(); + + let res = Html::new("

Hello, World!

"); + let res = res.respond_to(&req); + + assert!(res.status().is_success()); + assert!(res + .headers() + .get(header::CONTENT_TYPE) + .unwrap() + .to_str() + .unwrap() + .starts_with("text/html")); + assert!(res.body().starts_with("

")); + } +} diff --git a/actix-web/src/types/mod.rs b/actix-web/src/types/mod.rs index 792edd650..cabe53d6a 100644 --- a/actix-web/src/types/mod.rs +++ b/actix-web/src/types/mod.rs @@ -3,6 +3,7 @@ mod either; mod form; mod header; +mod html; mod json; mod path; mod payload; @@ -13,6 +14,7 @@ pub use self::{ either::Either, form::{Form, FormConfig, UrlEncoded}, header::Header, + html::Html, json::{Json, JsonBody, JsonConfig}, path::{Path, PathConfig}, payload::{Payload, PayloadConfig},