Skip to content

Commit

Permalink
Avoid 500 error on invalid referrer URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Nov 11, 2024
1 parent c1e6788 commit a4f8bb9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/shortUrlRedir.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ export function shortUrlRedir(ctx) {
}
const ref = ctx.get('referer');
if (!utmSource && ref?.length) {
const refUrl = new URL(ref);
const hostname = refUrl.hostname;
if (hostname !== 'hebcal.com' && !hostname.endsWith('.hebcal.com')) {
utmSource = hostname;
try {
const refUrl = new URL(ref);
const hostname = refUrl.hostname;
if (hostname !== 'hebcal.com' && !hostname.endsWith('.hebcal.com')) {
utmSource = hostname;
}
} catch (err) {
// ignore errors in invalid referrer URL
ctx.logger.warn(err, `invalid referrer ${ref}`);
}
}
utmSource = utmSource || 'redir';
Expand Down Expand Up @@ -71,7 +76,12 @@ function isValidDouble(id) {
return false;
}

const sedrotBaseUrl = 'https://www.hebcal.com/sedrot/';

function shortParshaRedir(ctx, str, qs) {
if (!str) {
return sedrotBaseUrl;
}
const code = str.charCodeAt(0);
if (code < 48 || code > 57) {
return false; // not a number, let old redirect logic happen
Expand Down Expand Up @@ -103,5 +113,5 @@ function shortParshaRedir(ctx, str, qs) {
if (il) {
qs.set('i', 'on');
}
return `https://www.hebcal.com/sedrot/${name}-${fmtDt}?` + qs.toString();
return `${sedrotBaseUrl}${name}-${fmtDt}?` + qs.toString();
}

0 comments on commit a4f8bb9

Please sign in to comment.