From 4cabb9696ec45c10d286f057713439a4c7e307f0 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Mon, 11 Mar 2024 10:46:58 +0800 Subject: [PATCH] refactor: custom json parser --- manifest.json | 4 ++-- src/common/server/http.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/manifest.json b/manifest.json index 952c1eb..d80d003 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "manifest_version": 4, "type": "extension", - "name": "LLOneBot v3.13.11", + "name": "LLOneBot v3.14.0", "slug": "LLOneBot", "description": "LiteLoaderQQNT的OneBotApi", - "version": "3.13.11", + "version": "3.14.0", "icon": "./icon.png", "authors": [ { diff --git a/src/common/server/http.ts b/src/common/server/http.ts index 6876465..d971b2e 100644 --- a/src/common/server/http.ts +++ b/src/common/server/http.ts @@ -12,7 +12,36 @@ export abstract class HttpServerBase { constructor() { this.expressAPP = express(); this.expressAPP.use(express.urlencoded({extended: true, limit: "500mb"})); - this.expressAPP.use(json({limit: "500mb"})); + // 自定义JSON解析中间件,避免有些人不填写Content-Type导致无法解析JSON + const customJsonParser = (req, res, next) => { + // 确保我们只处理`POST`请求 + if (req.method === 'POST') { + // 临时存储请求体数据 + let data = ''; + + // 监听data事件以收集数据片段 + req.on('data', chunk => { + data += chunk; + }); + + // 数据接收完成 + req.on('end', () => { + if (data) { + try { + // 尝试解析JSON + req.body = JSON.parse(data); + } catch (error) { + // 解析失败,返回错误响应 + return res.status(400).send('Invalid JSON'); + } + } + next(); + }); + } else { + next(); + } + }; + this.expressAPP.use(customJsonParser); } authorize(req: Request, res: Response, next: () => void) {