feat(web): visit 204 response, up page header/sort, trim system page

This commit is contained in:
kairee committed 2026-11-01 17:06:26 +08:00
1 parent e3bd2e601b
commit 84ce1e41e8
7 files changed
+41 -54

No files matched your search

+16 -11
View File
@@ -315,11 +315,15 @@ border-color:#3a2c22;color:#f3e9dc}button{background:#e9b97c;color:#2a1a10}}
raise ApiError(HTTPStatus.METHOD_NOT_ALLOWED, "method_not_allowed", "不支持该操作")
def _visit_post(self, query: Mapping[str, list[str]]) -> None:
"""记录一次页面浏览;前端 sendBeacon 上报,写内存后定期批量落库。"""
"""记录一次页面浏览;前端 sendBeacon 上报,写内存后定期批量落库。
埋点不需要响应内容,统一返回 ``204 No Content``,避免为加密体积和
前端解析负担;异常(如请求体过大)仍返回 JSON 错误。
"""
track = self.context.config_store.get().web.track
collector = self.context.visits
if not track.enabled or collector is None:
self._send_json(HTTPStatus.OK, {"recorded": False, "reason": "disabled"})
self._send_no_content()
return
path_value = _param(query, "path", "")
@@ -329,7 +333,7 @@ border-color:#3a2c22;color:#f3e9dc}button{background:#e9b97c;color:#2a1a10}}
user_agent = self.headers.get("User-Agent")
if not track.count_bots and is_bot(user_agent):
self._send_json(HTTPStatus.OK, {"recorded": False, "reason": "bot"})
self._send_no_content()
return
peer = self.client_address[0] if self.client_address else None
@@ -340,16 +344,11 @@ border-color:#3a2c22;color:#f3e9dc}button{background:#e9b97c;color:#2a1a10}}
header_order=track.header_order,
)
if not ip:
self._send_json(HTTPStatus.OK, {"recorded": False, "reason": "no_ip"})
self._send_no_content()
return
route = normalize_route(path_value)
recorded = collector.record(ip, route)
self._send_json(HTTPStatus.OK, {
"recorded": bool(recorded),
"route": route,
"reason": None if recorded else "capped",
})
collector.record(ip, normalize_route(path_value))
self._send_no_content()
def _read_small_body(self) -> bytes:
try:
@@ -570,6 +569,12 @@ border-color:#3a2c22;color:#f3e9dc}button{background:#e9b97c;color:#2a1a10}}
if self.command != "HEAD":
self.wfile.write(payload)
def _send_no_content(self, status: int = HTTPStatus.NO_CONTENT) -> None:
self.send_response(int(status))
self.send_header("Content-Length", "0")
self._security_headers()
self.end_headers()
def _send_html(self, status: int, html: str) -> None:
payload = html.encode("utf-8")
self.send_response(int(status))