Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Html responder #3399

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions actix-multipart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -58,6 +56,8 @@ async fn main() -> std::io::Result<()> {

<!-- cargo-rdme end -->

[More available in the examples repo &rarr;](https://github.com/actix/examples/tree/master/forms/multipart)

Curl request :

```bash
Expand Down
1 change: 1 addition & 0 deletions actix-web/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Add `web::Html` responder.
- Add `HttpRequest::full_url()` method to get the complete URL of the request.

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion actix-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
66 changes: 66 additions & 0 deletions actix-web/src/types/html.rs
Original file line number Diff line number Diff line change
@@ -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("<p>Hello, World!</p>")
/// # ;
/// ```
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Html(String);

impl Html {
/// Constructs a new `Html` responder.
pub fn new(html: impl Into<String>) -> Self {
Self(html.into())
}
}

impl Responder for Html {
type Body = String;

fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
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("<p>Hello, World!</p>");
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("<p>"));
}
}
2 changes: 2 additions & 0 deletions actix-web/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod either;
mod form;
mod header;
mod html;
mod json;
mod path;
mod payload;
Expand All @@ -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},
Expand Down
Loading