From ef0b4578821e2ae9ce3a4b8351f3311496149ee7 Mon Sep 17 00:00:00 2001 From: Mario Dian Date: Tue, 4 Oct 2022 16:03:27 +0800 Subject: [PATCH] Add query string support fixes #45 --- server/handlers/links.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/server/handlers/links.ts b/server/handlers/links.ts index a497cdd71..1c7320add 100644 --- a/server/handlers/links.ts +++ b/server/handlers/links.ts @@ -289,18 +289,32 @@ export const redirect = (app: ReturnType): Handler => async ( return res.redirect("/banned"); } - // 5. If wants to see link info, then redirect + // 5. Append query string when provided + if (req.query) { + const linkTargetParams = new URLSearchParams(link.target); + + Object.entries(req.query).forEach(([key, value]) => { + if (typeof value === "string") { + linkTargetParams.append(key, value); + } + }); + + // toString() encodes original url, decode it back + link.target = decodeURIComponent(linkTargetParams.toString()); + } + + // 6. If wants to see link info, then redirect const doesRequestInfo = /.*\+$/gi.test(req.params.id); if (doesRequestInfo && !link.password) { return app.render(req, res, "/url-info", { target: link.target }); } - // 6. If link is protected, redirect to password page + // 7. If link is protected, redirect to password page if (link.password) { return res.redirect(`/protected/${link.uuid}`); } - // 7. Create link visit + // 8. Create link visit if (link.user_id && !isBot) { queue.visit.add({ headers: req.headers, @@ -310,7 +324,7 @@ export const redirect = (app: ReturnType): Handler => async ( }); } - // 8. Create Google Analytics visit + // 9. Create Google Analytics visit if (env.GOOGLE_ANALYTICS_UNIVERSAL && !isBot) { ua(env.GOOGLE_ANALYTICS_UNIVERSAL) .pageview({