mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-17 20:28:21 +08:00
fix: middleware errors
This commit is contained in:
@@ -29,31 +29,25 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
|
||||
await next();
|
||||
|
||||
if (!ctx.res.body || ctx.res.headers.get('ETag')) {
|
||||
const data = ctx.get('data');
|
||||
if (!data || ctx.res.headers.get('ETag')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const status = Math.trunc(ctx.res.status / 100);
|
||||
if (2 !== status) {
|
||||
return;
|
||||
}
|
||||
const lastBuildDate = data.lastBuildDate;
|
||||
delete data.lastBuildDate;
|
||||
const etag = etagCalculate(JSON.stringify(data));
|
||||
|
||||
const res = ctx.res as Response;
|
||||
const body = await res.clone().text();
|
||||
const etag = etagCalculate(body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '').replace(/<atom:link(.*)\/>/, ''));
|
||||
|
||||
ctx.set('ETag', etag);
|
||||
ctx.header('ETag', etag);
|
||||
|
||||
const ifNoneMatch = ctx.req.header('If-None-Match') ?? null;
|
||||
if (etagMatches(etag, ifNoneMatch)) {
|
||||
console.log('in')
|
||||
ctx.status(304);
|
||||
ctx.body(null);
|
||||
return ctx.body(null);
|
||||
} else {
|
||||
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
|
||||
if (match) {
|
||||
ctx.header('Last-Modified', match[1]);
|
||||
}
|
||||
ctx.header('Last-Modified', lastBuildDate);
|
||||
}
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
export default middleware;
|
||||
|
||||
Reference in New Issue
Block a user