import { createLogger } from '@snowluma/common/logger'; import { createServer, type IncomingMessage, type Server, type ServerResponse, } from 'http'; import type { ApiHandler } from '../api-handler'; import type { DispatchPayload } from '../event-filter'; import type { ApiResponse, HttpServerNetwork, JsonObject, JsonValue } from '../types'; import { type StreamSink, wrapStreamFrame, wrapStreamTerminal } from '../streaming'; import { IOneBotNetworkAdapter, type AdapterStatus, type NetworkAdapterContext } from './adapter'; import { isAuthorized, normalizePath } from './utils'; const moduleLog = createLogger('OneBot.HTTP'); export class HttpServerAdapter extends IOneBotNetworkAdapter { private server: Server | null = null; private listening = false; private closePromise: Promise | null = null; private acceptingActions = false; private readonly inFlightActions = new Set>(); constructor(name: string, config: HttpServerNetwork, ctx: NetworkAdapterContext) { super(name, config, ctx, moduleLog); } async open(): Promise { if (this.isEnabled && this.listening) return; if (this.config.enabled === false) return; if (this.server) throw new Error(`HTTP adapter [${this.name}] still owns a previous server`); await this.startServer(); this.isEnabled = true; this.clearApplyFailure(); } async close(): Promise { if (this.closePromise) return this.closePromise; if (!this.isEnabled && !this.server && this.inFlightActions.size === 0) return; const wasEnabled = this.isEnabled; const wasListening = this.listening; const wasAcceptingActions = this.acceptingActions; this.acceptingActions = false; this.isEnabled = false; this.listening = false; const server = this.server; const release = server ? new Promise((resolve, reject) => { server.close((error) => { if (error && !isAlreadyClosedError(error)) reject(error); else resolve(); }); }) : Promise.resolve(); const attempt = Promise.all([release, Promise.all(this.inFlightActions)]).then(() => undefined); this.closePromise = attempt; try { await attempt; if (this.server === server) this.server = null; } catch (error) { // A failed close callback cannot prove the listener was released. Keep // ownership and live-state truth so shutdown/reconcile can retry it. this.isEnabled = wasEnabled; this.listening = wasListening; this.acceptingActions = wasAcceptingActions; throw error; } finally { this.closePromise = null; } } override describeStatus(): AdapterStatus { if (!this.isEnabled) return { name: this.name, kind: 'httpServer', status: 'disabled', detail: '未启用' }; if (!this.listening) return { name: this.name, kind: 'httpServer', status: 'down', detail: '未监听(端口被占用?)' }; return { name: this.name, kind: 'httpServer', status: 'ok', detail: '监听中' }; } protected override bindingSignature(config: HttpServerNetwork): string { return `${config.host ?? '0.0.0.0'}:${config.port}${normalizePath(config.path ?? '/')}`; } onEvent(_event: JsonObject, _payload: DispatchPayload): void { /* no-op */ } private startServer(): Promise { return new Promise((resolve, reject) => { const server = createServer((req, res) => { this.trackInboundAction(req, res); }); this.server = server; let opening = true; server.once('listening', () => { opening = false; if (this.server !== server || this.closePromise) { server.close(); reject(new Error(`HTTP adapter [${this.name}] was closed while binding`)); return; } this.listening = true; this.isEnabled = true; this.acceptingActions = true; this.log.success( '[%s] listening %s:%d%s', this.name, this.config.host ?? '0.0.0.0', this.config.port, normalizePath(this.config.path ?? '/'), ); resolve(); }); server.on('error', (error) => { if (this.server === server) { this.listening = false; this.isEnabled = false; this.acceptingActions = false; this.recordTransportFailure(error); if (opening) this.server = null; } this.log.error('[%s] server error: %s', this.name, error instanceof Error ? error.message : String(error)); if (opening) { opening = false; reject(error); } }); try { server.listen(this.config.port, this.config.host ?? '0.0.0.0'); } catch (error) { opening = false; if (this.server === server) this.server = null; this.recordTransportFailure(error); reject(error); } }); } private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise { const expectedPath = normalizePath(this.config.path ?? '/'); const accessToken = this.config.accessToken ?? ''; const parsedUrl = new URL(req.url ?? '/', 'http://127.0.0.1'); const incomingPath = parsedUrl.pathname; const ep = expectedPath.endsWith('/') ? expectedPath : expectedPath + '/'; let action = ''; if (incomingPath === expectedPath || incomingPath === expectedPath + '/') { action = ''; } else if (incomingPath.startsWith(ep)) { action = incomingPath.substring(ep.length); } else { writeJson(res, 404, { status: 'failed', retcode: 1404, data: null, wording: 'not found' }); return; } if (!isAuthorized(req, accessToken)) { writeJson(res, 401, { status: 'failed', retcode: 1401, data: null, wording: 'unauthorized' }); return; } if (req.method === 'GET' && !action) { writeJson(res, 200, { status: 'ok', retcode: 0, data: { online: true } }); return; } try { let params: Record = {}; let echo: unknown; if (req.method === 'GET') { parsedUrl.searchParams.forEach((value, key) => { try { params[key] = JSON.parse(value); } catch { params[key] = value; } }); } else if (req.method === 'POST') { const bodyContent = await readRequestBody(req); if (bodyContent.trim()) { const contentType = req.headers['content-type'] ?? ''; if (contentType.includes('application/x-www-form-urlencoded')) { const parsed = new URLSearchParams(bodyContent); parsed.forEach((value, key) => { try { params[key] = JSON.parse(value); } catch { params[key] = value; } }); if (params.action && !action) { action = String(params.action); delete params.action; } if (params.echo !== undefined) { echo = params.echo; delete params.echo; } } else if (contentType.includes('application/json') || !contentType) { try { const parsedBody = JSON.parse(bodyContent); if (typeof parsedBody === 'object' && parsedBody !== null && !Array.isArray(parsedBody)) { if (parsedBody.action && !action) action = String(parsedBody.action); if (parsedBody.params && typeof parsedBody.params === 'object' && !Array.isArray(parsedBody.params)) { params = parsedBody.params as Record; } else { params = parsedBody as Record; } echo = parsedBody.echo; } } catch { if (contentType.includes('application/json')) { writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: invalid json' }); return; } // 无 content-type,JSON 失败则 fallback 到 urlencoded const parsed = new URLSearchParams(bodyContent); parsed.forEach((value, key) => { try { params[key] = JSON.parse(value); } catch { params[key] = value; } }); if (params.action && !action) { action = String(params.action); delete params.action; } if (params.echo !== undefined) { echo = params.echo; delete params.echo; } } } else { writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: `bad request: unsupported content-type: ${contentType}` }); return; } } } else { writeJson(res, 405, { status: 'failed', retcode: 1400, data: null, wording: 'method not allowed' }); return; } if (!action) { writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: missing action' }); return; } // Stream API (#163): answer with chunked multi-frame output — each frame // a JSON envelope delimited by `\r\n\r\n`, terminated by the final frame. if (this.ctx.api.isStreamAction(action)) { await streamHttpResponse(res, this.ctx.api, action, params as JsonObject, echo as JsonValue | undefined); return; } const response = await this.ctx.api.handle(action, params as JsonObject); if (echo !== undefined) { response.echo = echo as JsonValue; } writeJson(res, 200, response); } catch (error) { const wording = error instanceof Error ? error.message : 'internal error'; writeJson(res, 500, { status: 'failed', retcode: 1200, data: null, wording }); } } private trackInboundAction(req: IncomingMessage, res: ServerResponse): void { if (!this.acceptingActions || this.ctx.api.isAcceptingActions === false) { writeJson(res, 503, { status: 'failed', retcode: 1200, data: null, wording: 'server closing' }); return; } let action: Promise; try { action = this.handleRequest(req, res); } catch (error) { this.log.error('[%s] inbound action start failed: %s', this.name, error instanceof Error ? (error.stack ?? error.message) : String(error)); writeJson(res, 500, { status: 'failed', retcode: 1200, data: null, wording: 'internal error' }); return; } const tracked = action.then( () => undefined, (error) => { this.log.error('[%s] inbound action failed: %s', this.name, error instanceof Error ? (error.stack ?? error.message) : String(error)); }, ); this.inFlightActions.add(tracked); void tracked.then(() => { this.inFlightActions.delete(tracked); }); } } function isAlreadyClosedError(error: Error): boolean { return (error as NodeJS.ErrnoException).code === 'ERR_SERVER_NOT_RUNNING'; } function readRequestBody(req: IncomingMessage, maxBytes = 2 * 1024 * 1024): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; let total = 0; req.on('data', (chunk: Buffer) => { total += chunk.length; if (total > maxBytes) { reject(new Error('request body too large')); req.destroy(); return; } chunks.push(chunk); }); req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); req.on('error', reject); }); } function writeJson(res: ServerResponse, statusCode: number, data: unknown): void { // A streaming response may already have flushed headers (and even ended); a // late error must not double-send and trip ERR_HTTP_HEADERS_SENT. if (res.headersSent || res.writableEnded || res.destroyed) return; res.statusCode = statusCode; res.setHeader('Content-Type', 'application/json; charset=utf-8'); res.end(JSON.stringify(data)); } /** Stream a multi-frame Stream API response (#163): chunked transfer (no * Content-Length → Node frames it), each frame a JSON envelope delimited by * `\r\n\r\n`, the action's terminal response written last. Matches NapCat: * the body is `\r\n\r\n`-joined JSON objects, NOT a single JSON document, so * no `application/json` Content-Type is claimed. Each `res.write` is awaited * (flush callback) for backpressure, and the sink aborts the action once the * client disconnects so a big download stops pumping into a dead socket. */ async function streamHttpResponse( res: ServerResponse, api: ApiHandler, action: string, params: JsonObject, echo: JsonValue | undefined, ): Promise { res.statusCode = 200; const writeFrame = (frame: ApiResponse): Promise => new Promise((resolve) => { if (res.writableEnded || res.destroyed) { resolve(); return; } res.write(JSON.stringify(frame) + '\r\n\r\n', () => resolve()); }); const sink: StreamSink = { send: async (frame) => { if (res.writableEnded || res.destroyed) throw new Error('stream client disconnected'); await writeFrame(wrapStreamFrame(frame, echo)); }, }; const response = await api.handle(action, params, sink); await writeFrame(wrapStreamTerminal(response, echo)); if (!res.writableEnded && !res.destroyed) res.end(); }