From 485fc611a0121e1f174a38de323871ebc6c22fe3 Mon Sep 17 00:00:00 2001 From: macabeus Date: Sat, 21 Dec 2024 18:23:27 +0000 Subject: [PATCH] fix: handle paths with spaces (#944) --- src/adapter/web-standard/index.ts | 2 +- src/dynamic-handle.ts | 2 +- test/core/elysia.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/adapter/web-standard/index.ts b/src/adapter/web-standard/index.ts index 3052f53c..472836db 100644 --- a/src/adapter/web-standard/index.ts +++ b/src/adapter/web-standard/index.ts @@ -79,7 +79,7 @@ export const WebStandardAdapter: ElysiaAdapter = { const hasTrace = app.event.trace.length > 0 fnLiteral += - `const u=r.url,` + + `const u=decodeURI(r.url),` + `s=u.indexOf('/',${standardHostname ? 11 : 7}),` + `qi=u.indexOf('?', s + 1)\n` + `let p\n` + diff --git a/src/dynamic-handle.ts b/src/dynamic-handle.ts index 98c5ecc8..7ee9bb42 100644 --- a/src/dynamic-handle.ts +++ b/src/dynamic-handle.ts @@ -38,7 +38,7 @@ export const createDynamicHandler = (app: AnyElysia) => { const { mapResponse, mapEarlyResponse } = app['~adapter'].handler return async (request: Request): Promise => { - const url = request.url, + const url = decodeURI(request.url), s = url.indexOf('/', 11), qi = url.indexOf('?', s + 1), path = qi === -1 ? url.substring(s) : url.substring(s, qi) diff --git a/test/core/elysia.test.ts b/test/core/elysia.test.ts index 2a3e9ab6..8f0e79e7 100644 --- a/test/core/elysia.test.ts +++ b/test/core/elysia.test.ts @@ -206,4 +206,26 @@ describe('Edge Case', () => { expect(response).toBe('params-transform'); }) }) + + describe('handle path with spaces', () => { + it('when AOT is on', async () => { + const PATH = "/y a y"; + + const app = new Elysia().get(PATH, () => "result from a path wirh spaces"); + + const response = await app.handle(new Request(`http://localhost${PATH}`)); + + expect(response.status).toBe(200); + }) + + it('when AOT is off', async () => { + const PATH = "/y a y"; + + const app = new Elysia({ aot: false }).get(PATH, () => "result from a path wirh spaces"); + + const response = await app.handle(new Request(`http://localhost${PATH}`)); + + expect(response.status).toBe(200); + }) + }) })