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:
parent
60a84d879d
commit
1a2019664a
1 changed files with 18 additions and 9 deletions
27
svr.js
27
svr.js
|
@ -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;
|
||||||
|
var doCallback = true;
|
||||||
if (!isProxy) {
|
if (!isProxy) {
|
||||||
var doCallback = true;
|
for (var i = (_mapBegIndex ? _mapBegIndex : 0); i < map.length; i++) {
|
||||||
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)) {
|
||||||
mapEntry.replacements.forEach(function (replacement) {
|
try {
|
||||||
rewrittenURL = rewrittenURL.replace(createRegex(replacement.regex), replacement.replacement);
|
mapEntry.replacements.forEach(function (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
|
||||||
|
@ -4207,7 +4212,7 @@ if (!cluster.isPrimary) {
|
||||||
fs.stat("." + decodeURIComponent(href), function (err, stats) {
|
fs.stat("." + decodeURIComponent(href), function (err, stats) {
|
||||||
if (err || !stats.isDirectory()) {
|
if (err || !stats.isDirectory()) {
|
||||||
try {
|
try {
|
||||||
callback();
|
callback();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
callServerError(500, undefined, err);
|
callServerError(500, undefined, err);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
|
Reference in a new issue