From c778d3b6993b6c57faa36001eb48050f8492bdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Thu, 15 Aug 2024 00:33:20 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=8F=90=E9=AB=98=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/network/passive-http.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/onebot/network/passive-http.ts b/src/onebot/network/passive-http.ts index bba5b091..0547a7cf 100644 --- a/src/onebot/network/passive-http.ts +++ b/src/onebot/network/passive-http.ts @@ -43,13 +43,34 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { this.server?.close(); this.app = undefined; } - + cors(): any { + return (req: Request, res: Response, next: any) => { + if (req.method === 'OPTIONS') { + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + } + next(); + }; + } private initializeServer() { this.app = express(); this.server = http.createServer(this.app); - this.app.use(express.json()); - this.app.use(express.urlencoded({ extended: false })); + this.app.use(this.cors()); + this.app.use(express.urlencoded({ extended: true, limit: '5000mb' })); + this.app.use((req, res, next) => { + // 兼容处理没有带content-type的请求 + req.headers['content-type'] = 'application/json'; + const originalJson = express.json({ limit: '5000mb' }); + originalJson(req, res, (err) => { + if (err) { + return res.status(400).send('Invalid JSON'); + } + next(); + }); + }); + this.app.use((req, res, next) => this.authorize(this.token, req, res, next)); this.app.use((req, res) => this.handleRequest(req, res));