From c2941b6c1de1777ef6d03aa00c2e4414d76808e8 Mon Sep 17 00:00:00 2001 From: Gabi Villalonga Simon Date: Wed, 20 Nov 2024 14:50:00 -0600 Subject: [PATCH] fix: do not add query params on top of the URL with query params --- src/router.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/router.ts b/src/router.ts index 0a92340..d6c5a53 100644 --- a/src/router.ts +++ b/src/router.ts @@ -84,11 +84,13 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => { } } + const urlNoQueryParams = new URL(req.url); + deleteAllURLParams(urlNoQueryParams); if (tags.truncated) { return new Response(JSON.stringify(ManifestTagsListTooBigError), { status: 400, headers: { - "Link": `${req.url}/?last=${tags.truncated ? tags.cursor : ""}; rel=next`, + "Link": `${urlNoQueryParams.toString()}/?last=${tags.truncated ? tags.cursor : ""}; rel=next`, "Content-Type": "application/json", }, }); @@ -514,6 +516,12 @@ export type TagsList = { tags: string[]; }; +function deleteAllURLParams(url: URL) { + for (const key of url.searchParams.keys()) { + url.searchParams.delete(key); + } +} + v2Router.get("/:name+/tags/list", async (req, env: Env) => { const { name } = req.params; const { n: nStr = 50, last } = req.query; @@ -529,6 +537,8 @@ v2Router.get("/:name+/tags/list", async (req, env: Env) => { }); const keys = tags.objects.map((object) => object.key.split("/").pop()!); + const urlNoQueryParams = new URL(req.url); + deleteAllURLParams(urlNoQueryParams); return new Response( JSON.stringify({ name, @@ -538,7 +548,7 @@ v2Router.get("/:name+/tags/list", async (req, env: Env) => { status: 200, headers: { "Content-Type": "application/json", - "Link": `${req.url}?n=${n}&last=${keys.length ? keys[keys.length - 1] : ""}; rel=next`, + "Link": `${urlNoQueryParams.toString()}?n=${n}&last=${keys.length ? keys[keys.length - 1] : ""}; rel=next`, }, }, );