diff --git a/src/handlers/clientErrorHandler.js b/src/handlers/clientErrorHandler.js index 22c6da7..db2d79b 100644 --- a/src/handlers/clientErrorHandler.js +++ b/src/handlers/clientErrorHandler.js @@ -1,6 +1,7 @@ const fs = require("fs"); const http = require("http"); const defaultPageCSS = require("../res/defaultPageCSS.js"); +const statusCodes = require("../res/statusCodes.js"); const generateErrorStack = require("../utils/generateErrorStack.js"); const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js"); const generateServerString = require("../utils/generateServerString.js"); @@ -265,14 +266,14 @@ function clientErrorHandler(err, socket) { parseInt(process.version.split(".")[0].substring(1)) >= 16 ) { // Disable custom error page for HTTP SSL error - res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); + res.writeHead(errorCode, statusCodes[errorCode], cheaders); res.write( `
{errorDesc}
{server}
" .replace( /{errorMessage}/g, errorCode.toString() + " " + - http.STATUS_CODES[errorCode] + statusCodes[errorCode] .replace(/&/g, "&") .replace(//g, ">") @@ -316,7 +317,7 @@ function clientErrorHandler(err, socket) { fs.readFile(errorFile, (err, data) => { try { if (err) throw err; - res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); + res.writeHead(errorCode, statusCodes[errorCode], cheaders); responseEnd( data .toString() @@ -324,7 +325,7 @@ function clientErrorHandler(err, socket) { /{errorMessage}/g, errorCode.toString() + " " + - http.STATUS_CODES[errorCode] + statusCodes[errorCode] .replace(/&/g, "&") .replace(//g, ">") @@ -378,7 +379,7 @@ function clientErrorHandler(err, socket) { } else if (err.code == "ELOOP") { additionalError = 508; } - res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); + res.writeHead(errorCode, statusCodes[errorCode], cheaders); res.write( `{errorDesc}
${ additionalError == 404 @@ -389,7 +390,7 @@ function clientErrorHandler(err, socket) { /{errorMessage}/g, errorCode.toString() + " " + - http.STATUS_CODES[errorCode] + statusCodes[errorCode] .replace(/&/g, "&") .replace(//g, ">") diff --git a/src/handlers/requestHandler.js b/src/handlers/requestHandler.js index 2bce209..961ddee 100644 --- a/src/handlers/requestHandler.js +++ b/src/handlers/requestHandler.js @@ -1,4 +1,3 @@ -const http = require("http"); const fs = require("fs"); const net = require("net"); const defaultPageCSS = require("../res/defaultPageCSS.js"); @@ -10,6 +9,7 @@ const matchHostname = require("../utils/matchHostname.js"); const generateServerString = require("../utils/generateServerString.js"); const parseURL = require("../utils/urlParser.js"); const deepClone = require("../utils/deepClone.js"); +const statusCodes = require("../res/statusCodes.js"); let serverconsole = {}; let middleware = []; @@ -134,7 +134,7 @@ function requestHandler(req, res) { req.socket.remoteAddress == "localhost") ) { let headers = config.getCustomHeaders(); - res.writeHead(204, http.STATUS_CODES[204], headers); + res.writeHead(204, statusCodes[204], headers); res.end(); return; } @@ -173,9 +173,9 @@ function requestHandler(req, res) { "Server responded with " + code.toString() + " code." ); } - if (typeof codeDescription != "string" && http.STATUS_CODES[code]) { + if (typeof codeDescription != "string" && statusCodes[code]) { if (!headers) headers = codeDescription; - codeDescription = http.STATUS_CODES[code]; + codeDescription = statusCodes[code]; } lastStatusCode = code; } @@ -444,7 +444,7 @@ function requestHandler(req, res) { fs.readFile(errorFile, (err, data) => { try { if (err) throw err; - res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); + res.writeHead(errorCode, statusCodes[errorCode], cheaders); res.responseEnd( data .toString() @@ -452,7 +452,7 @@ function requestHandler(req, res) { /{errorMessage}/g, errorCode.toString() + " " + - http.STATUS_CODES[errorCode] + statusCodes[errorCode] .replace(/&/g, "&") .replace(//g, ">") @@ -523,7 +523,7 @@ function requestHandler(req, res) { additionalError = 508; } - res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); + res.writeHead(errorCode, statusCodes[errorCode], cheaders); res.write( `{errorDesc}
${ additionalError == 404 @@ -534,7 +534,7 @@ function requestHandler(req, res) { /{errorMessage}/g, errorCode.toString() + " " + - http.STATUS_CODES[errorCode] + statusCodes[errorCode] .replace(/&/g, "&") .replace(//g, ">") @@ -620,7 +620,7 @@ function requestHandler(req, res) { : 301; // Write the response header with the appropriate status code and message - res.writeHead(statusCode, http.STATUS_CODES[statusCode], customHeaders); + res.writeHead(statusCode, statusCodes[statusCode], customHeaders); // Log the redirection message logFacilities.resmessage("Client redirected to " + destination); @@ -656,7 +656,7 @@ function requestHandler(req, res) { // Respond with list of methods let hdss = config.getCustomHeaders(); hdss["Allow"] = "GET, POST, HEAD, OPTIONS"; - res.writeHead(204, http.STATUS_CODES[204], hdss); + res.writeHead(204, statusCodes[204], hdss); res.end(); return; } else { diff --git a/src/middleware/defaultHandlerChecks.js b/src/middleware/defaultHandlerChecks.js index 5b5ec56..bf0e697 100644 --- a/src/middleware/defaultHandlerChecks.js +++ b/src/middleware/defaultHandlerChecks.js @@ -1,5 +1,5 @@ -const http = require("http"); const defaultPageCSS = require("../res/defaultPageCSS.js"); +const statusCodes = require("../res/statusCodes.js"); const svrjsInfo = require("../../svrjs.json"); const { name } = svrjsInfo; @@ -7,7 +7,7 @@ module.exports = (req, res, logFacilities, config, next) => { if (req.isProxy) { let eheaders = config.getCustomHeaders(); eheaders["Content-Type"] = "text/html; charset=utf-8"; - res.writeHead(501, http.STATUS_CODES[501], eheaders); + res.writeHead(501, statusCodes[501], eheaders); res.write( `${name
.replace(/&/g, "&")
@@ -34,7 +34,7 @@ module.exports = (req, res, logFacilities, config, next) => {
if (req.method == "OPTIONS") {
let hdss = config.getCustomHeaders();
hdss["Allow"] = "GET, POST, HEAD, OPTIONS";
- res.writeHead(204, http.STATUS_CODES[204], hdss);
+ res.writeHead(204, statusCodes[204], hdss);
res.end();
return;
} else if (
diff --git a/src/middleware/staticFileServingAndDirectoryListings.js b/src/middleware/staticFileServingAndDirectoryListings.js
index 5dfe826..2a7f7d3 100644
--- a/src/middleware/staticFileServingAndDirectoryListings.js
+++ b/src/middleware/staticFileServingAndDirectoryListings.js
@@ -1,4 +1,3 @@
-const http = require("http");
const fs = require("fs");
const os = require("os");
const zlib = require("zlib");
@@ -9,6 +8,7 @@ const ipMatch = require("../utils/ipMatch.js");
const createRegex = require("../utils/createRegex.js");
const sha256 = require("../utils/sha256.js");
const sizify = require("../utils/sizify.js");
+const statusCodes = require("../res/statusCodes.js");
const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo;
@@ -140,7 +140,7 @@ module.exports = (req, res, logFacilities, config, next) => {
// Check if the client's request matches the ETag value (If-None-Match)
const clientETag = req.headers["if-none-match"];
if (clientETag === fileETag) {
- res.writeHead(304, http.STATUS_CODES[304], {
+ res.writeHead(304, statusCodes[304], {
ETag: clientETag
});
res.end();
@@ -206,14 +206,14 @@ module.exports = (req, res, logFacilities, config, next) => {
begin < res.head.length &&
end - begin < res.head.length
) {
- res.writeHead(206, http.STATUS_CODES[206], rhd);
+ res.writeHead(206, statusCodes[206], rhd);
res.end(res.head.substring(begin, end + 1));
return;
} else if (
ext == "html" &&
begin >= res.head.length + filelen
) {
- res.writeHead(206, http.STATUS_CODES[206], rhd);
+ res.writeHead(206, statusCodes[206], rhd);
res.end(
res.foot.substring(
begin - res.head.length - filelen,
@@ -278,7 +278,7 @@ module.exports = (req, res, logFacilities, config, next) => {
)
});
};
- res.writeHead(206, http.STATUS_CODES[206], rhd);
+ res.writeHead(206, statusCodes[206], rhd);
if (res.head.length == 0 || begin > res.head.length) {
afterWriteCallback();
} else if (
@@ -291,7 +291,7 @@ module.exports = (req, res, logFacilities, config, next) => {
process.nextTick(afterWriteCallback);
}
} else {
- res.writeHead(206, http.STATUS_CODES[206], rhd);
+ res.writeHead(206, statusCodes[206], rhd);
readStream.pipe(res);
}
logFacilities.resmessage(
@@ -302,7 +302,7 @@ module.exports = (req, res, logFacilities, config, next) => {
}
});
} else {
- res.writeHead(206, http.STATUS_CODES[206], rhd);
+ res.writeHead(206, statusCodes[206], rhd);
res.end();
}
}
@@ -461,7 +461,7 @@ module.exports = (req, res, logFacilities, config, next) => {
end: res.foot.length == 0
});
};
- res.writeHead(200, http.STATUS_CODES[200], hdhds);
+ res.writeHead(200, statusCodes[200], hdhds);
if (res.head.length == 0) {
afterWriteCallback();
} else if (!resStream.write(res.head)) {
@@ -470,7 +470,7 @@ module.exports = (req, res, logFacilities, config, next) => {
process.nextTick(afterWriteCallback);
}
} else {
- res.writeHead(200, http.STATUS_CODES[200], hdhds);
+ res.writeHead(200, statusCodes[200], hdhds);
readStream.pipe(resStream);
}
logFacilities.resmessage(
@@ -481,7 +481,7 @@ module.exports = (req, res, logFacilities, config, next) => {
}
});
} else {
- res.writeHead(200, http.STATUS_CODES[200], hdhds);
+ res.writeHead(200, statusCodes[200], hdhds);
res.end();
logFacilities.resmessage("Client successfully received content.");
}
@@ -890,7 +890,7 @@ module.exports = (req, res, logFacilities, config, next) => {
}
// Send the directory listing response
- res.writeHead(200, http.STATUS_CODES[200], {
+ res.writeHead(200, statusCodes[200], {
"Content-Type": "text/html; charset=utf-8"
});
res.end(
diff --git a/src/middleware/status.js b/src/middleware/status.js
index 06af4ec..3d9a494 100644
--- a/src/middleware/status.js
+++ b/src/middleware/status.js
@@ -1,7 +1,7 @@
-const http = require("http");
const os = require("os");
const defaultPageCSS = require("../res/defaultPageCSS.js");
const sizify = require("../utils/sizify.js");
+const statusCodes = require("../res/statusCodes.js");
const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo;
@@ -57,7 +57,7 @@ module.exports = (req, res, logFacilities, config, next) => {
}%`;
statusBody += `
Thread PID: ${process.pid}
`;
- res.writeHead(200, http.STATUS_CODES[200], {
+ res.writeHead(200, statusCodes[200], {
"Content-Type": "text/html; charset=utf-8"
});
res.end(
diff --git a/src/res/statusCodes.js b/src/res/statusCodes.js
new file mode 100644
index 0000000..ece40da
--- /dev/null
+++ b/src/res/statusCodes.js
@@ -0,0 +1,10 @@
+const http = require("http");
+
+const statusCodes = {
+ ...http.STATUS_CODES,
+ 497: "HTTP Request Sent to HTTPS Port",
+ 598: "Network Read Timeout Error",
+ 599: "Network Connect Timeout Error"
+};
+
+module.exports = statusCodes;