Skip to content

Commit

Permalink
only dedouble slashes in pathnames
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Feb 17, 2024
1 parent b05ecc9 commit faac9d4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
4 changes: 1 addition & 3 deletions packages/primate/src/hooks/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import o from "rcompat/object";
import { tryreturn } from "rcompat/async";
import errors from "../errors.js";

const deslash = url => url.replaceAll(/(?<!http:)\/{2,}/gu, _ => "/");

const get_body = (request, url) =>
tryreturn(async _ => await Body.parse(request) ?? {})
.orelse(error => errors.MismatchedBody.throw(url.pathname, error.message));

export default app => async original => {
const { headers } = original;

const url = new URL(deslash(globalThis.decodeURIComponent(original.url)));
const url = new URL(globalThis.decodeURIComponent(original.url));
const cookies = headers.get("cookie");

return { original, url,
Expand Down
5 changes: 3 additions & 2 deletions packages/primate/src/hooks/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ export default test => {
await matches([
"..",
"../",
"/",
"//",
"../..",
"../../",
"../../.",
Expand Down Expand Up @@ -93,6 +91,9 @@ export default test => {
assert(response2.query.get("foo")).equals("bar");
assert(response2.query.get("bar")).equals("baz");
assert(response2.query.get("baz")).undefined();
const response3 = await r.get("/?url=https://primatejs.com");
assert(response3.query.json()).equals({ url: "https://primatejs.com" });
assert(response3.query.get("url")).equals("https://primatejs.com");
});

test.case("cookies", async assert => {
Expand Down
4 changes: 3 additions & 1 deletion packages/primate/src/hooks/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ export default app => {
is_method({ route, method, pathname }));

const index = path => `${location.routes}${path === "/" ? "/index" : path}`;
// remove excess slashes
const deslash = url => url.replaceAll(/\/{2,}/gu, _ => "/");

return ({ original: { method }, url }) => {
const pathname = deroot(url.pathname);
const pathname = deroot(deslash(url.pathname));
const route = find(method, pathname) ?? errors.NoRouteToPath
.throw(method.toLowerCase(), pathname, index(pathname));
return { ...route, path: to_path(route, pathname) };
Expand Down
12 changes: 10 additions & 2 deletions packages/primate/src/hooks/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export default test => {

test.reassert(assert => ({
match: (url, expected) => {
assert(r(url).pathname.toString()).equals(expected.toString());
try {
assert(r(url).pathname.toString()).equals(expected.toString());
} catch {
assert.fail();
}
},
fail: (url, result) => {
const reason = mark("no {0} route to {1}", "get", result ?? url);
Expand All @@ -133,10 +137,14 @@ export default test => {
match("/users/1a", re);
match("/users/aa", re);
match("/users/ba?key=value", re);
match("/users/1a/", re);
// double deslashes are dedoubled in pathnames
match("/users//1a", re);
match("//users//1a", re);
match("//users//1a//", re);
fail("/user/1a");
fail("/users/a");
fail("/users/aA");
fail("/users//a");
fail("/users/?a", "/users");
});
test.case("no params", ({ path }) => {
Expand Down

0 comments on commit faac9d4

Please sign in to comment.