Skip to content

Commit

Permalink
chore(core::writing): Do not forcibly overwrite the content-type and …
Browse files Browse the repository at this point in the history
…optimize the code. (#950)

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

* chore(core::writing): Text::set_header -> Text::append_header

* chore(core::writing): Check if it exists before setting the header.
  • Loading branch information
andeya authored Oct 10, 2024
1 parent 2095f3c commit 75be65b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
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

0 comments on commit 75be65b

Please sign in to comment.