From 13e76c0c9bafea73b5067deb25e20c02afa01aa7 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Wed, 27 Mar 2024 23:58:00 +0800 Subject: [PATCH] Small fixes Improve matching of content type and charset in content type header. --- src/backend/http/server.ts | 23 ++++++++++++++++++++--- src/common/data-source.ts | 5 ----- 2 files changed, 20 insertions(+), 8 deletions(-) delete mode 100644 src/common/data-source.ts diff --git a/src/backend/http/server.ts b/src/backend/http/server.ts index a7b5ed0..6f43397 100644 --- a/src/backend/http/server.ts +++ b/src/backend/http/server.ts @@ -174,6 +174,14 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr decorateRequestWithBackend(backendState), ]; + const isTextMediaType = (mediaType: string) => ( + mediaType.startsWith('text/') + || [ + 'application/json', + 'application/xml' + ].includes(mediaType) + ); + const processRequest = (middlewares: [string, Middleware, v.BaseSchema?][]) => async (req: RequestContext) => { if (req.url === '/' || req.url === '') { return handleGetRoot(req); @@ -220,10 +228,19 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr if (schema) { const contentTypeHeader = req.headers['content-type'] ?? 'application/octet-stream'; - const fragments = contentTypeHeader.split(';'); + const fragments = contentTypeHeader.replace(/\s+/g, ' ').split(';'); const mediaType = fragments[0]; - const charsetParam = fragments.map((s) => s.trim()) - .find((f) => f.startsWith('charset=')) ?? (mediaType.startsWith('text/') ? 'charset=utf-8' : 'charset=binary'); + const charsetParam = ( + fragments + .map((s) => s.trim()) + .find((f) => f.startsWith('charset=')) + + ?? ( + isTextMediaType(mediaType) + ? 'charset=utf-8' + : 'charset=binary' + ) + ); const [_charsetKey, charsetRaw] = charsetParam.split('=').map((s) => s.trim()); const charset = ( ( diff --git a/src/common/data-source.ts b/src/common/data-source.ts deleted file mode 100644 index 58a5c46..0000000 --- a/src/common/data-source.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface BaseDataSource {} - -export interface GenerationStrategy { - (dataSource: D, ...args: unknown[]): Promise; -}