1
0
Fork 0
forked from svrjs/svrjs

Changed rewriteURL method to use callbacks.

This commit is contained in:
Dorian Niemiec 2024-01-13 07:34:28 +01:00
parent 506df5ada7
commit d942342106

82
svr.js
View file

@ -4177,24 +4177,59 @@ if (!cluster.isPrimary) {
} }
// Handle URL rewriting // Handle URL rewriting
function rewriteURL(address, map) { function rewriteURL(address, map, callback) {
var rewrittenAddress = address;
var rewrittenURL = address;
if (!isProxy) {
map.every(function (mapEntry) { map.every(function (mapEntry) {
if (matchHostname(mapEntry.host) && createRegex(mapEntry.definingRegex).test(address)) { if (matchHostname(mapEntry.host) && createRegex(mapEntry.definingRegex).test(address)) {
mapEntry.replacements.forEach(function (replacement) { mapEntry.replacements.forEach(function (replacement) {
rewrittenAddress = rewrittenAddress.replace(createRegex(replacement.regex), replacement.replacement); rewrittenURL = rewrittenURL.replace(createRegex(replacement.regex), replacement.replacement);
}); });
if (mapEntry.append) rewrittenAddress += mapEntry.append; if (mapEntry.append) rewrittenURL += mapEntry.append;
return false; return false;
} else { } else {
return true; return true;
} }
}); });
return rewrittenAddress;
} }
callback(rewrittenURL);
}
// Trailing slash redirection
function redirectTrailingSlashes(callback) {
if (!disableTrailingSlashRedirects && href[href.length - 1] != "/" && origHref[origHref.length - 1] != "/") {
fs.stat("." + decodeURIComponent(href), function (err, stats) {
if (err || !stats.isDirectory()) {
try {
callback();
} catch (err) {
callServerError(500, undefined, err);
}
} else {
var destinationURL = uobject;
destinationURL.path = null;
destinationURL.href = null;
destinationURL.pathname = origHref + "/";
destinationURL.hostname = null;
destinationURL.host = null;
destinationURL.port = null;
destinationURL.protocol = null;
destinationURL.slashes = null;
destinationURL = url.format(destinationURL);
redirect(destinationURL);
}
});
} else {
callback();
}
}
var origHref = href; var origHref = href;
if (!isProxy) {
var rewrittenURL = rewriteURL(req.url, rewriteMap); // Rewrite URLs
rewriteURL(req.url, rewriteMap, function(rewrittenURL) {
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;
@ -4248,8 +4283,6 @@ if (!cluster.isPrimary) {
} }
} }
} }
}
// Set response headers // Set response headers
if (!isProxy) { if (!isProxy) {
var hkh = getCustomHeaders(); var hkh = getCustomHeaders();
@ -4355,35 +4388,6 @@ if (!cluster.isPrimary) {
} }
} }
// Trailing slash redirection
function redirectTrailingSlashes(callback) {
if (!disableTrailingSlashRedirects && href[href.length - 1] != "/" && origHref[origHref.length - 1] != "/") {
fs.stat("." + decodeURIComponent(href), function (err, stats) {
if (err || !stats.isDirectory()) {
try {
callback();
} catch (err) {
callServerError(500, undefined, err);
}
} else {
var destinationURL = uobject;
destinationURL.path = null;
destinationURL.href = null;
destinationURL.pathname = origHref + "/";
destinationURL.hostname = null;
destinationURL.host = null;
destinationURL.port = null;
destinationURL.protocol = null;
destinationURL.slashes = null;
destinationURL = url.format(destinationURL);
redirect(destinationURL);
}
});
} else {
callback();
}
}
// Handle HTTP authentication // Handle HTTP authentication
if (authIndex > -1) { if (authIndex > -1) {
var authcode = nonStandardCodes[authIndex]; var authcode = nonStandardCodes[authIndex];
@ -4582,6 +4586,8 @@ if (!cluster.isPrimary) {
}); });
} }
} }
});
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, undefined, generateErrorStack(err));
} }