diff --git a/src/server/context.ts b/src/server/context.ts index 630af24b..2bf02ebd 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -266,9 +266,8 @@ export class ServerContext { const { localUrl, path, size, contentType, etag } of this.#extractResult .staticFiles ) { - const route = sanitizePathToRegex(path); - staticRoutes[route] = { - baseRoute: toBaseRoute(route), + staticRoutes[path] = { + baseRoute: toBaseRoute(path), methods: { "HEAD": this.#staticFileHandler( localUrl, @@ -589,18 +588,6 @@ export function normalizeURLPath(path: string): string | null { } } -function sanitizePathToRegex(path: string): string { - return path - .replaceAll("\*", "\\*") - .replaceAll("\+", "\\+") - .replaceAll("\?", "\\?") - .replaceAll("\{", "\\{") - .replaceAll("\}", "\\}") - .replaceAll("\(", "\\(") - .replaceAll("\)", "\\)") - .replaceAll("\:", "\\:"); -} - function serializeCSPDirectives(csp: ContentSecurityPolicyDirectives): string { return Object.entries(csp) .filter(([_key, value]) => value !== undefined) diff --git a/tests/fixture/static/foo (bar).txt b/tests/fixture/static/foo (bar).txt new file mode 100644 index 00000000..85c88293 --- /dev/null +++ b/tests/fixture/static/foo (bar).txt @@ -0,0 +1 @@ +it works \ No newline at end of file diff --git a/tests/fixture/static/foo bar.txt b/tests/fixture/static/foo bar.txt new file mode 100644 index 00000000..85c88293 --- /dev/null +++ b/tests/fixture/static/foo bar.txt @@ -0,0 +1 @@ +it works \ No newline at end of file diff --git a/tests/main_test.ts b/tests/main_test.ts index aef4b412..fcc63c75 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -359,6 +359,16 @@ Deno.test("static file - by file path", async () => { assertEquals(resp3.headers.get("content-type"), "text/plain"); }); +Deno.test("static file - spaces or other characters in name", async () => { + const res = await handler(new Request("https://fresh.deno.dev/foo bar.txt")); + assertEquals(await res.text(), "it works"); + + const res2 = await handler( + new Request("https://fresh.deno.dev/foo (bar).txt"), + ); + assertEquals(await res2.text(), "it works"); +}); + Deno.test("HEAD request", async () => { // Static file const resp = await handler(