From cba327faff830fb1332dca81cb202aae71fd2358 Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Sat, 15 Mar 2025 11:45:00 +0800 Subject: [PATCH 1/4] fix messy code when send_file text --- src/main.rs | 9 +++++++++ tests/hello.txt | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/hello.txt diff --git a/src/main.rs b/src/main.rs index 038be55..6fad2bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -921,6 +921,15 @@ impl MainHandler { let mime = mime_types::from_path(path).first_or_octet_stream(); resp.headers .set_raw("content-type", vec![mime.to_string().into_bytes()]); + + // 修复中文乱码问题:对于文本类型的文件,明确指定 UTF-8 编码 + let content_type = if mime.type_() == "text" { + format!("{}; charset=utf-8", mime) + } else { + mime.to_string() + }; + resp.headers.set_raw("content-type", vec![content_type.into_bytes()]); + if self.coop { resp.headers.set_raw( "Cross-Origin-Opener-Policy", diff --git a/tests/hello.txt b/tests/hello.txt new file mode 100644 index 0000000..84bd9f0 --- /dev/null +++ b/tests/hello.txt @@ -0,0 +1,7 @@ +Language (English) "Hello, World!" Translation +English Hello, World! +Chinese (Simplified) 你好,世界! (Nǐ hǎo, shìjiè!) +Russian Здравствуй, мир! (Zdravstvuy, mir!) +Japanese こんにちは、世界! (Konnichiwa, sekai!) + + From a7eade3e2c2a3839a13d5da6208c54887d4f2d4c Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Mon, 17 Mar 2025 10:10:47 +0800 Subject: [PATCH 2/4] Refactor content type handling to explicitly specify UTF-8 encoding for text files, addressing non-English character issues. --- src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6fad2bd..2023b0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -919,10 +919,8 @@ impl MainHandler { } // Set mime type let mime = mime_types::from_path(path).first_or_octet_stream(); - resp.headers - .set_raw("content-type", vec![mime.to_string().into_bytes()]); - // 修复中文乱码问题:对于文本类型的文件,明确指定 UTF-8 编码 + // Fix messy code for non english issue: explicitly specify UTF-8 encoding for text files let content_type = if mime.type_() == "text" { format!("{}; charset=utf-8", mime) } else { From c04d6ead919850c0b8b962d8ae3258f9da314f8e Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Mon, 17 Mar 2025 11:10:30 +0800 Subject: [PATCH 3/4] Add support for YAML content type in UTF-8 encoding handling --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index 2023b0a..42efd25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -923,6 +923,8 @@ impl MainHandler { // Fix messy code for non english issue: explicitly specify UTF-8 encoding for text files let content_type = if mime.type_() == "text" { format!("{}; charset=utf-8", mime) + } else if mime.type_() == "application" && mime.subtype() == "x-yaml" { + format!("text/yaml; charset=utf-8") } else { mime.to_string() }; From e71e845b28566249b57727f40056a4ef5017952f Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Mon, 17 Mar 2025 11:27:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8F=AF=E6=89=A7=E8=A1=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=A7=E5=B0=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 840972c..bbe29fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,12 @@ path-dedot = "1" default = ["native-tls"] only-openssl = ["native-tls", "openssl"] native-tls = ["hyper-native-tls"] + + +[profile.release] +opt-level = 3 # 最高优化级别 +lto = true # 链接时优化 +codegen-units = 1 # 尽可能合并代码生成单元 +panic = 'abort' # 恐慌时立即终止,而不是展开(减小二进制大小) +strip = true # 从二进制文件中删除调试信息 +