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

chore(core::writing): Do not forcibly overwrite the content-type and optimize the code. #950

Merged
merged 3 commits into from
Oct 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
7 changes: 4 additions & 3 deletions crates/core/src/writing/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Display, Formatter};
use async_trait::async_trait;
use serde::Serialize;

use super::Scribe;
use super::{try_set_header, Scribe};
use crate::http::header::{HeaderValue, CONTENT_TYPE};
use crate::http::{Response, StatusError};

Expand Down Expand Up @@ -36,11 +36,12 @@ where
fn render(self, res: &mut Response) {
match serde_json::to_vec(&self.0) {
Ok(bytes) => {
res.headers_mut().insert(
try_set_header(
&mut res.headers,
CONTENT_TYPE,
HeaderValue::from_static("application/json; charset=utf-8"),
);
res.write_body(bytes).ok();
let _ = res.write_body(bytes);
}
Err(e) => {
tracing::error!(error = ?e, "JsonContent write error");
Expand Down
28 changes: 21 additions & 7 deletions crates/core/src/writing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod redirect;
mod seek;
mod text;

use http::StatusCode;
use http::header::{AsHeaderName, IntoHeaderName};
use http::{HeaderMap, StatusCode};
pub use json::Json;
pub use redirect::Redirect;
pub use seek::ReadSeeker;
Expand Down Expand Up @@ -97,31 +98,34 @@ impl Scribe for StatusCode {
impl Scribe for &'static str {
#[inline]
fn render(self, res: &mut Response) {
res.headers_mut().insert(
try_set_header(
&mut res.headers,
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
res.write_body(self).ok();
let _ = res.write_body(self);
}
}
impl Scribe for &String {
#[inline]
fn render(self, res: &mut Response) {
res.headers_mut().insert(
try_set_header(
&mut res.headers,
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
res.write_body(self.as_bytes().to_vec()).ok();
let _ = res.write_body(self.as_bytes().to_vec());
}
}
impl Scribe for String {
#[inline]
fn render(self, res: &mut Response) {
res.headers_mut().insert(
try_set_header(
&mut res.headers,
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
res.write_body(self).ok();
let _ = res.write_body(self);
}
}
impl Scribe for std::convert::Infallible {
Expand Down Expand Up @@ -149,6 +153,16 @@ macro_rules! writer_tuple_impls {

crate::for_each_tuple!(writer_tuple_impls);

fn try_set_header<K, V>(headers: &mut HeaderMap<V>, key: K, val: V)
where
K: IntoHeaderName,
for<'a> &'a K: AsHeaderName,
{
if !headers.contains_key(&key) {
let _ = headers.insert(key, val);
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down
18 changes: 9 additions & 9 deletions crates/core/src/writing/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{self, Debug, Display, Formatter};

use super::Scribe;
use super::{try_set_header, Scribe};
use crate::http::header::{HeaderValue, CONTENT_TYPE};
use crate::http::Response;

Expand Down Expand Up @@ -44,7 +44,7 @@ impl<C> Text<C>
where
C: AsRef<str>,
{
fn set_header(self, res: &mut Response) -> C {
fn try_set_header(self, res: &mut Response) -> C {
let (ctype, content) = match self {
Self::Plain(content) => (
HeaderValue::from_static("text/plain; charset=utf-8"),
Expand Down Expand Up @@ -81,29 +81,29 @@ where
content,
),
};
res.headers_mut().insert(CONTENT_TYPE, ctype);
try_set_header(&mut res.headers, CONTENT_TYPE, ctype);
content
}
}
impl Scribe for Text<&'static str> {
#[inline]
fn render(self, res: &mut Response) {
let content = self.set_header(res);
res.write_body(content).ok();
let content = self.try_set_header(res);
let _ = res.write_body(content);
}
}
impl Scribe for Text<String> {
#[inline]
fn render(self, res: &mut Response) {
let content = self.set_header(res);
res.write_body(content).ok();
let content = self.try_set_header(res);
let _ = res.write_body(content);
}
}
impl Scribe for Text<&String> {
#[inline]
fn render(self, res: &mut Response) {
let content = self.set_header(res);
res.write_body(content.as_bytes().to_vec()).ok();
let content = self.try_set_header(res);
let _ = res.write_body(content.as_bytes().to_vec());
}
}
impl<C: Debug> Debug for Text<C> {
Expand Down
Loading