Skip to content

fix messy code when send_file text #131

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 # 从二进制文件中删除调试信息

13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,17 @@ 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()]);

// 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type is text, first read the content (at most 512 bytes), and use chardet to detect the encoding.

} else if mime.type_() == "application" && mime.subtype() == "x-yaml" {
format!("text/yaml; charset=utf-8")
} else {
mime.to_string()
};
resp.headers.set_raw("content-type", vec![content_type.into_bytes()]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content-type already set in line:923


if self.coop {
resp.headers.set_raw(
"Cross-Origin-Opener-Policy",
Expand Down
7 changes: 7 additions & 0 deletions tests/hello.txt
Original file line number Diff line number Diff line change
@@ -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!)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is unnecessary.