From 91d9acc68b5a4ec19158ba9c10c4d31e28aac7ec Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 12 Nov 2024 20:13:35 +0800 Subject: [PATCH] fix: redirection in router handler --- lib/middleware/template.test.ts | 6 ++++++ lib/middleware/template.tsx | 4 +++- lib/routes/141jav/index.ts | 2 +- lib/routes/141ppv/index.ts | 2 +- lib/routes/aqara/region.ts | 2 +- lib/routes/cs/zzkx.ts | 2 +- lib/routes/dongqiudi/daily.ts | 2 +- lib/routes/hostmonit/cloudflareyesv6.ts | 2 +- lib/routes/jiemian/list.ts | 2 +- lib/routes/liulinblog/itnews.ts | 2 +- lib/routes/nbd/daily.ts | 2 +- lib/routes/scmp/coronavirus.ts | 2 +- lib/routes/test/index.ts | 4 ++++ lib/routes/weibo/timeline.ts | 5 +++-- 14 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/middleware/template.test.ts b/lib/middleware/template.test.ts index 2a03c5ddd77a12..da61f0ffec20e4 100644 --- a/lib/middleware/template.test.ts +++ b/lib/middleware/template.test.ts @@ -108,4 +108,10 @@ describe('template', () => { expect(parsed.items[0].enclosure?.length).toBe('3661'); expect(parsed.items[0].itunes.duration).toBe('10:10:10'); }); + + it(`redirect`, async () => { + const response = await app.request('/test/redirect'); + expect(response.status).toBe(301); + expect(response.headers.get('location')).toBe('/test/1'); + }); }); diff --git a/lib/middleware/template.tsx b/lib/middleware/template.tsx index 36dae2e72d93d5..6bcf3fd639502b 100644 --- a/lib/middleware/template.tsx +++ b/lib/middleware/template.tsx @@ -102,7 +102,9 @@ const middleware: MiddlewareHandler = async (ctx, next) => { return ctx.json(result); } - if (ctx.get('no-content')) { + if (ctx.get('redirect')) { + return ctx.redirect(ctx.get('redirect'), 301); + } else if (ctx.get('no-content')) { return ctx.body(null); } else { // retain .ums for backward compatibility diff --git a/lib/routes/141jav/index.ts b/lib/routes/141jav/index.ts index 5eb1d07579eda5..b7f58edad2498b 100644 --- a/lib/routes/141jav/index.ts +++ b/lib/routes/141jav/index.ts @@ -66,7 +66,7 @@ async function handler(ctx) { const $ = load(response.data); if (getSubPath(ctx) === '/') { - ctx.redirect(`/141jav${$('.overview').first().attr('href')}`); + ctx.set('redirect', `/141jav${$('.overview').first().attr('href')}`); return; } diff --git a/lib/routes/141ppv/index.ts b/lib/routes/141ppv/index.ts index 2e084470440f2e..fbf1239b4c7146 100644 --- a/lib/routes/141ppv/index.ts +++ b/lib/routes/141ppv/index.ts @@ -66,7 +66,7 @@ async function handler(ctx) { const $ = load(response.data); if (getSubPath(ctx) === '/') { - ctx.redirect(`/141ppv${$('.overview').first().attr('href')}`); + ctx.set('redirect', `/141ppv${$('.overview').first().attr('href')}`); return; } diff --git a/lib/routes/aqara/region.ts b/lib/routes/aqara/region.ts index e3c20484d217d8..48174a0834bec3 100644 --- a/lib/routes/aqara/region.ts +++ b/lib/routes/aqara/region.ts @@ -14,5 +14,5 @@ function handler(ctx) { const { region = 'en', type = 'news' } = ctx.req.param(); const redirectTo = `/aqara/${region}/category/${types[type]}`; - ctx.redirect(redirectTo); + ctx.set('redirect', redirectTo); } diff --git a/lib/routes/cs/zzkx.ts b/lib/routes/cs/zzkx.ts index 0786a25591907a..783ce264b52589 100644 --- a/lib/routes/cs/zzkx.ts +++ b/lib/routes/cs/zzkx.ts @@ -10,5 +10,5 @@ function handler(ctx) { // https://www.cs.com.cn/sylm/jsbd/ const redirectTo = '/cs/sylm/jsbd'; - ctx.redirect(redirectTo); + ctx.set('redirect', redirectTo); } diff --git a/lib/routes/dongqiudi/daily.ts b/lib/routes/dongqiudi/daily.ts index d4e32f93b43551..c5ec38e677026e 100644 --- a/lib/routes/dongqiudi/daily.ts +++ b/lib/routes/dongqiudi/daily.ts @@ -18,5 +18,5 @@ export const route: Route = { }; function handler(ctx) { - ctx.redirect('/dongqiudi/special/48'); + ctx.set('redirect', '/dongqiudi/special/48'); } diff --git a/lib/routes/hostmonit/cloudflareyesv6.ts b/lib/routes/hostmonit/cloudflareyesv6.ts index a52287cfb06485..c7d38cbae38796 100644 --- a/lib/routes/hostmonit/cloudflareyesv6.ts +++ b/lib/routes/hostmonit/cloudflareyesv6.ts @@ -7,5 +7,5 @@ export const route: Route = { }; function handler(ctx) { - ctx.redirect('/hostmonit/cloudflareyes/v6'); + ctx.set('redirect', '/hostmonit/cloudflareyes/v6'); } diff --git a/lib/routes/jiemian/list.ts b/lib/routes/jiemian/list.ts index ea316e70148732..95637ee14fd901 100644 --- a/lib/routes/jiemian/list.ts +++ b/lib/routes/jiemian/list.ts @@ -10,5 +10,5 @@ function handler(ctx) { const id = ctx.req.param('id'); const redirectTo = `/jiemian${id ? `/lists/${id}` : ''}`; - ctx.redirect(redirectTo); + ctx.set('redirect', redirectTo); } diff --git a/lib/routes/liulinblog/itnews.ts b/lib/routes/liulinblog/itnews.ts index bc627efc54c1b9..155ad9060fb773 100644 --- a/lib/routes/liulinblog/itnews.ts +++ b/lib/routes/liulinblog/itnews.ts @@ -9,5 +9,5 @@ export const route: Route = { function handler(ctx) { const { channel } = ctx.req.param(); const redirectTo = `/liulinblog/${channel}`; - ctx.redirect(redirectTo); + ctx.set('redirect', redirectTo); } diff --git a/lib/routes/nbd/daily.ts b/lib/routes/nbd/daily.ts index a05b1060734c58..49c554967a6caa 100644 --- a/lib/routes/nbd/daily.ts +++ b/lib/routes/nbd/daily.ts @@ -24,5 +24,5 @@ export const route: Route = { }; function handler(ctx) { - ctx.redirect('/nbd/332'); + ctx.set('redirect', '/nbd/332'); } diff --git a/lib/routes/scmp/coronavirus.ts b/lib/routes/scmp/coronavirus.ts index 7745656b475b16..193267e80f2133 100644 --- a/lib/routes/scmp/coronavirus.ts +++ b/lib/routes/scmp/coronavirus.ts @@ -18,5 +18,5 @@ export const route: Route = { }; function handler(ctx) { - ctx.redirect('/scmp/topics/coronavirus-pandemic-all-stories'); + ctx.set('redirect', '/scmp/topics/coronavirus-pandemic-all-stories'); } diff --git a/lib/routes/test/index.ts b/lib/routes/test/index.ts index 70c88815b7e1e9..a4a744fe06eefe 100644 --- a/lib/routes/test/index.ts +++ b/lib/routes/test/index.ts @@ -32,6 +32,10 @@ async function handler(ctx) { if (ctx.req.param('id') === 'invalid-parameter-error') { throw new InvalidParameterError('Test invalid parameter error'); } + if (ctx.req.param('id') === 'redirect') { + ctx.set('redirect', '/test/1'); + return; + } let item: DataItem[] = []; let image: string | null = null; switch (ctx.req.param('id')) { diff --git a/lib/routes/weibo/timeline.ts b/lib/routes/weibo/timeline.ts index 1acdacc594cdef..921d5cc0f7963d 100644 --- a/lib/routes/weibo/timeline.ts +++ b/lib/routes/weibo/timeline.ts @@ -95,7 +95,8 @@ async function handler(ctx) { ctx.set({ 'Cache-Control': 'no-cache', }); - ctx.redirect(`https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${routeParams}` : ''}`); + ctx.set('redirect', `https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${routeParams}` : ''}`); + return; } const resultItem = await Promise.all( response.statuses.map(async (item) => { @@ -183,6 +184,6 @@ async function handler(ctx) { ctx.set({ 'Cache-Control': 'no-cache', }); - ctx.redirect(`https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${feature}/${routeParams.replaceAll('&', '%26')}` : ''}`); + ctx.set('redirect', `https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}${routeParams ? `&state=${feature}/${routeParams.replaceAll('&', '%26')}` : ''}`); } }