forked from svrjs/svrjs
Added support for 307 and 308 redirects (both in config.json and in redirect() SVR.JS API method)
This commit is contained in:
parent
fef016dd1c
commit
9569c7b7fd
1 changed files with 11 additions and 5 deletions
16
svr.js
16
svr.js
|
@ -3127,15 +3127,21 @@ if (!cluster.isPrimary) {
|
||||||
|
|
||||||
|
|
||||||
// Function to perform HTTP redirection to a specified destination URL
|
// Function to perform HTTP redirection to a specified destination URL
|
||||||
function redirect(destination, isTemporary, customHeaders) {
|
function redirect(destination, isTemporary, keepMethod, customHeaders) {
|
||||||
|
// If keepMethod is a object, then save it to customHeaders
|
||||||
|
if (typeof keepMethod == "object") customHeaders = keepMethod;
|
||||||
|
|
||||||
|
// If isTemporary is a object, then save it to customHeaders
|
||||||
|
if (typeof isTemporary == "object") customHeaders = isTemporary;
|
||||||
|
|
||||||
// If customHeaders are not provided, get the default custom headers
|
// If customHeaders are not provided, get the default custom headers
|
||||||
if (customHeaders === undefined) customHeaders = getCustomHeaders();
|
if (customHeaders === undefined) customHeaders = getCustomHeaders();
|
||||||
|
|
||||||
// Set the "Location" header to the destination URL
|
// Set the "Location" header to the destination URL
|
||||||
customHeaders["Location"] = destination;
|
customHeaders["Location"] = destination;
|
||||||
|
|
||||||
// Determine the status code for redirection based on the isTemporary flag
|
// Determine the status code for redirection based on the isTemporary and keepMethod flags
|
||||||
var statusCode = isTemporary ? 302 : 301;
|
var statusCode = keepMethod ? (isTemporary ? 307 : 308) : (isTemporary ? 302 : 301);
|
||||||
|
|
||||||
// Write the response header with the appropriate status code and message
|
// Write the response header with the appropriate status code and message
|
||||||
res.writeHead(statusCode, http.STATUS_CODES[statusCode], customHeaders);
|
res.writeHead(statusCode, http.STATUS_CODES[statusCode], customHeaders);
|
||||||
|
@ -4355,7 +4361,7 @@ if (!cluster.isPrimary) {
|
||||||
// Handle non-standard codes
|
// Handle non-standard codes
|
||||||
if (nonscodeIndex > -1) {
|
if (nonscodeIndex > -1) {
|
||||||
var nonscode = nonStandardCodes[nonscodeIndex];
|
var nonscode = nonStandardCodes[nonscodeIndex];
|
||||||
if (nonscode.scode == 301 || nonscode.scode == 302) {
|
if (nonscode.scode == 301 || nonscode.scode == 302 || nonscode.scode == 307 || nonscode.scode == 308) {
|
||||||
var location = "";
|
var location = "";
|
||||||
if (regexI[nonscodeIndex]) {
|
if (regexI[nonscodeIndex]) {
|
||||||
location = req.url.replace(regexI[nonscodeIndex], nonscode.location);
|
location = req.url.replace(regexI[nonscodeIndex], nonscode.location);
|
||||||
|
@ -4364,7 +4370,7 @@ if (!cluster.isPrimary) {
|
||||||
} else {
|
} else {
|
||||||
location = nonscode.location + "?" + req.url.split("?")[1];
|
location = nonscode.location + "?" + req.url.split("?")[1];
|
||||||
}
|
}
|
||||||
redirect(location, nonscode.scode == 302);
|
redirect(location, nonscode.scode == 302 || nonscode.scode == 307, nonscode.scode == 307 || nonsceode.scode == 308);
|
||||||
return;
|
return;
|
||||||
} else if (nonscode.scode == 403) {
|
} else if (nonscode.scode == 403) {
|
||||||
callServerError(403);
|
callServerError(403);
|
||||||
|
|
Reference in a new issue