Ver Fonte

【修复,服务端】设置 404 响应的正确字符编码

将 404 错误页面的 content-type 头部从 "text/plain" 更新为 "text/plain; charset=utf-8",
以确保浏览器能够正确解析中文错误信息的字符编码。
PJ568 há 1 ano atrás
pai
commit
b0d9008163
3 ficheiros alterados com 7 adições e 4 exclusões
  1. 1 1
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 5 2
      src/server.rs

+ 1 - 1
Cargo.lock

@@ -942,7 +942,7 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
 
 
 [[package]]
 [[package]]
 name = "swb-sys-monitor"
 name = "swb-sys-monitor"
-version = "0.1.2"
+version = "0.1.3"
 dependencies = [
 dependencies = [
  "anyhow",
  "anyhow",
  "clap",
  "clap",

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 [package]
 name = "swb-sys-monitor"
 name = "swb-sys-monitor"
-version = "0.1.2"
+version = "0.1.3"
 authors = ["PJ568 <[email protected]>"]
 authors = ["PJ568 <[email protected]>"]
 edition = "2024"
 edition = "2024"
 description = "为嵌入式设备高并发监控场景设计的极简网页资源占用显示系统"
 description = "为嵌入式设备高并发监控场景设计的极简网页资源占用显示系统"

+ 5 - 2
src/server.rs

@@ -107,7 +107,7 @@ impl StatusServer {
         warn!("请求了不存在的页面");
         warn!("请求了不存在的页面");
         Response::builder()
         Response::builder()
             .status(StatusCode::NOT_FOUND)
             .status(StatusCode::NOT_FOUND)
-            .header("content-type", "text/plain")
+            .header("content-type", "text/plain; charset=utf-8")
             .body(Body::from("页面未找到"))
             .body(Body::from("页面未找到"))
             .unwrap()
             .unwrap()
     }
     }
@@ -235,7 +235,10 @@ mod tests {
         assert_eq!(response.status(), StatusCode::NOT_FOUND);
         assert_eq!(response.status(), StatusCode::NOT_FOUND);
 
 
         let headers = response.headers();
         let headers = response.headers();
-        assert_eq!(headers.get("content-type").unwrap(), "text/plain");
+        assert_eq!(
+            headers.get("content-type").unwrap(),
+            "text/plain; charset=utf-8"
+        );
 
 
         let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
         let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
         assert_eq!(std::str::from_utf8(&body).unwrap(), "页面未找到");
         assert_eq!(std::str::from_utf8(&body).unwrap(), "页面未找到");