1
0
Fork 0
forked from svrjs/svrjs

Fixed error handling for invalid URL rewrite regexes. Also fixed bug with HTTP proxy not working.

This commit is contained in:
Dorian Niemiec 2024-01-18 01:12:09 +01:00
parent 60a84d879d
commit 1a2019664a

17
svr.js
View file

@ -4168,9 +4168,9 @@ if (!cluster.isPrimary) {
// Handle URL rewriting // Handle URL rewriting
function rewriteURL(address, map, callback, _fileState, _mapBegIndex) { function rewriteURL(address, map, callback, _fileState, _mapBegIndex) {
var rewrittenURL = address; var rewrittenURL = address;
if (!isProxy) {
var doCallback = true; var doCallback = true;
for(var i=(_mapBegIndex ? _mapBegIndex : 0);i<map.length;i++) { if (!isProxy) {
for (var i = (_mapBegIndex ? _mapBegIndex : 0); i < map.length; i++) {
var mapEntry = map[i]; var mapEntry = map[i];
if(href != "/" && (mapEntry.isNotDirectory || mapEntry.isNotFile) && !_fileState) { if(href != "/" && (mapEntry.isNotDirectory || mapEntry.isNotFile) && !_fileState) {
fs.stat("." + decodeURIComponent(href), function(err, stats) { fs.stat("." + decodeURIComponent(href), function(err, stats) {
@ -4190,15 +4190,20 @@ if (!cluster.isPrimary) {
break; break;
} }
if (matchHostname(mapEntry.host) && createRegex(mapEntry.definingRegex).test(address) && !(mapEntry.isNotDirectory && _fileState == 2) && !(mapEntry.isNotFile && _fileState == 1)) { if (matchHostname(mapEntry.host) && createRegex(mapEntry.definingRegex).test(address) && !(mapEntry.isNotDirectory && _fileState == 2) && !(mapEntry.isNotFile && _fileState == 1)) {
try {
mapEntry.replacements.forEach(function (replacement) { mapEntry.replacements.forEach(function (replacement) {
rewrittenURL = rewrittenURL.replace(createRegex(replacement.regex), replacement.replacement); rewrittenURL = rewrittenURL.replace(createRegex(replacement.regex), replacement.replacement);
}); });
if (mapEntry.append) rewrittenURL += mapEntry.append; if (mapEntry.append) rewrittenURL += mapEntry.append;
} catch (err) {
doCallback = false;
callback(err, address);
}
break; break;
} }
} }
} }
if(doCallback) callback(rewrittenURL); if(doCallback) callback(null, rewrittenURL);
} }
// Trailing slash redirection // Trailing slash redirection
@ -4233,7 +4238,11 @@ if (!cluster.isPrimary) {
var origHref = href; var origHref = href;
// Rewrite URLs // Rewrite URLs
rewriteURL(req.url, rewriteMap, function(rewrittenURL) { rewriteURL(req.url, rewriteMap, function(err, rewrittenURL) {
if (err) {
callServerError(500, undefined, err);
return;
}
if (rewrittenURL != req.url) { if (rewrittenURL != req.url) {
serverconsole.resmessage("URL rewritten: " + req.url + " => " + rewrittenURL); serverconsole.resmessage("URL rewritten: " + req.url + " => " + rewrittenURL);
req.url = rewrittenURL; req.url = rewrittenURL;