修管理页 Content-Type(浏览器显示成一屏源码)

用户截图:打开 relay.xxmxxs.top/zhongzhuanzhan 看到的**不是页面,
而是整页 HTML 源码**。

根因:_send() 的 content_type 参数默认是 application/json,
我重做管理页时调它没传 text/html —— 浏览器按 JSON/纯文本处理,
就把 HTML 原样显示了。

⚠ 更该反省的是:**我当时没发现。**
   验证脚本只看了 HTTP 200 和字节数,两个都对,就以为没问题。
   200 不等于页面能看 —— 响应头也得看。

顺手修的第二个问题:
   HEAD 请求一律返回 501 Unsupported method(没实现 do_HEAD)。
   浏览器用 GET 所以页面照开,但 Cloudflare 健康检查、
   很多监控/代理探测都用 HEAD,curl -I 也是。
   补了 do_HEAD:按路径算出类型和长度,只回头不回体。

新增测试 _build/test_response_headers.py(9 项):
  * HEAD 各路径不能是 501
  * 官网/管理页 Content-Type 必须是 text/html,不能是 json
  * favicon、产品截图必须是 image/*
并加进全量套件(阶段 3b6)。

验证:该测试 9/9 通过;全量套件 33 个阶段全部通过。
This commit is contained in:
xmz7891
2026-10-07 09:53:43 +08:00
parent 160dc3765e
commit fe3b9ada12
11 changed files with 400 additions and 25 deletions
+43
View File
@@ -0,0 +1,43 @@
#!/bin/bash
# 验证响应头 —— 上次改完只看了状态码和字节数,没看 Content-Type,
# 结果页面在浏览器里是"一屏源码"。
set -u
BASE=http://127.0.0.1:8080
PASS=$(cat /root/lanremote-relay/data/admin_password.txt 2>/dev/null)
curl -s -c /tmp/ck.txt -o /dev/null --max-time 15 \
-d "username=xmz7891&password=$PASS" "$BASE/admin/login"
echo " 管理页响应头:"
curl -sI -b /tmp/ck.txt --max-time 15 "$BASE/zhongzhuanzhan" \
| head -4 | sed 's/^/ /'
echo
echo " 官网响应头:"
curl -sI --max-time 15 "$BASE/" | head -3 | sed 's/^/ /'
echo
echo " 产品截图响应头:"
curl -sI --max-time 15 "$BASE/shot/pc-main.png" | head -3 | sed 's/^/ /'
echo
echo " 登录页响应头:"
curl -sI --max-time 15 "$BASE/zhongzhuanzhan" | head -3 | sed 's/^/ /'
echo
echo " 判断:"
admin_ct=$(curl -sI -b /tmp/ck.txt --max-time 15 "$BASE/zhongzhuanzhan" \
| grep -i '^content-type' | tr -d '\r')
if echo "$admin_ct" | grep -qi 'text/html'; then
echo " ✓ 管理页是 text/html"
else
echo " ✗ 管理页 Content-Type 不对:$admin_ct"
fi
site_ct=$(curl -sI --max-time 15 "$BASE/" | grep -i '^content-type' | tr -d '\r')
if echo "$site_ct" | grep -qi 'text/html'; then
echo " ✓ 官网是 text/html"
else
echo " ✗ 官网 Content-Type 不对:$site_ct"
fi