Skip to content

Commit

Permalink
chore(NamedFile): Skip write content-type if it exists (#939)
Browse files Browse the repository at this point in the history
* chore(NamedFile): Skip write content-type if it exists

* wip
  • Loading branch information
chrislearn authored Oct 1, 2024
1 parent 7e88621 commit 7132018
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions crates/core/src/fs/named_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use headers::*;
use tokio::fs::File;

use super::{ChunkedFile, ChunkedState};
use crate::http::header::{CONTENT_DISPOSITION, CONTENT_ENCODING, IF_NONE_MATCH, RANGE};
use crate::http::header::{
CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_TYPE, IF_NONE_MATCH, RANGE,
};
use crate::http::{HttpRange, Mime, Request, Response, StatusCode, StatusError};
use crate::{async_trait, Depot, Error, Result, Writer};

Expand Down Expand Up @@ -459,9 +461,10 @@ impl NamedFile {
}
}
}

res.headers_mut()
.typed_insert(ContentType::from(self.content_type.clone()));
if !res.headers().contains_key(CONTENT_TYPE) {
res.headers_mut()
.typed_insert(ContentType::from(self.content_type.clone()));
}
if let Some(lm) = last_modified {
res.headers_mut().typed_insert(LastModified::from(lm));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/craft-macros/src/craft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub(crate) fn generate(input: Item) -> syn::Result<TokenStream> {
}
}

const REGEX_STR: &'static str = r#"(?s)#\s*\[\s*(::)?\s*([A-Za-z_][A-Za-z0-9_]*\s*::\s*)*\s*craft\s*\(\s*(?P<name>handler|endpoint)\s*(?P<content>\(.*\))?\s*\)\s*\]"#;
const REGEX_STR: &str = r#"(?s)#\s*\[\s*(::)?\s*([A-Za-z_][A-Za-z0-9_]*\s*::\s*)*\s*craft\s*\(\s*(?P<name>handler|endpoint)\s*(?P<content>\(.*\))?\s*\)\s*\]"#;

fn take_method_macro(item_fn: &mut ImplItemFn) -> syn::Result<Option<Attribute>> {
let mut index: Option<usize> = None;
let mut new_attr: Option<Attribute> = None;
let re = Regex::new(REGEX_STR).unwrap();
let re = Regex::new(REGEX_STR).expect("regex compile should not fail");
for (idx, attr) in &mut item_fn.attrs.iter().enumerate() {
if !(match attr.path().segments.last() {
Some(segment) => segment.ident == "craft",
Expand Down

0 comments on commit 7132018

Please sign in to comment.