1
0
Fork 0
forked from svrjs/svrjs

fix: fix a bug with 497, 598, and 599 status code HTTP responses

This commit is contained in:
Dorian Niemiec 2024-10-15 18:23:09 +02:00
parent d7b5d0af44
commit 2f475b4066
6 changed files with 43 additions and 32 deletions

View file

@ -1,6 +1,7 @@
const fs = require("fs"); const fs = require("fs");
const http = require("http"); const http = require("http");
const defaultPageCSS = require("../res/defaultPageCSS.js"); const defaultPageCSS = require("../res/defaultPageCSS.js");
const statusCodes = require("../res/statusCodes.js");
const generateErrorStack = require("../utils/generateErrorStack.js"); const generateErrorStack = require("../utils/generateErrorStack.js");
const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js"); const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js");
const generateServerString = require("../utils/generateServerString.js"); const generateServerString = require("../utils/generateServerString.js");
@ -265,14 +266,14 @@ function clientErrorHandler(err, socket) {
parseInt(process.version.split(".")[0].substring(1)) >= 16 parseInt(process.version.split(".")[0].substring(1)) >= 16
) { ) {
// Disable custom error page for HTTP SSL error // Disable custom error page for HTTP SSL error
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, statusCodes[errorCode], cheaders);
res.write( res.write(
`<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}${"</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p><p><i>{server}</i></p></body></html>" `<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}${"</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p><p><i>{server}</i></p></body></html>"
.replace( .replace(
/{errorMessage}/g, /{errorMessage}/g,
errorCode.toString() + errorCode.toString() +
" " + " " +
http.STATUS_CODES[errorCode] statusCodes[errorCode]
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")
@ -316,7 +317,7 @@ function clientErrorHandler(err, socket) {
fs.readFile(errorFile, (err, data) => { fs.readFile(errorFile, (err, data) => {
try { try {
if (err) throw err; if (err) throw err;
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, statusCodes[errorCode], cheaders);
responseEnd( responseEnd(
data data
.toString() .toString()
@ -324,7 +325,7 @@ function clientErrorHandler(err, socket) {
/{errorMessage}/g, /{errorMessage}/g,
errorCode.toString() + errorCode.toString() +
" " + " " +
http.STATUS_CODES[errorCode] statusCodes[errorCode]
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")
@ -378,7 +379,7 @@ function clientErrorHandler(err, socket) {
} else if (err.code == "ELOOP") { } else if (err.code == "ELOOP") {
additionalError = 508; additionalError = 508;
} }
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, statusCodes[errorCode], cheaders);
res.write( res.write(
`<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>${ `<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>${
additionalError == 404 additionalError == 404
@ -389,7 +390,7 @@ function clientErrorHandler(err, socket) {
/{errorMessage}/g, /{errorMessage}/g,
errorCode.toString() + errorCode.toString() +
" " + " " +
http.STATUS_CODES[errorCode] statusCodes[errorCode]
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")

View file

@ -1,4 +1,3 @@
const http = require("http");
const fs = require("fs"); const fs = require("fs");
const net = require("net"); const net = require("net");
const defaultPageCSS = require("../res/defaultPageCSS.js"); const defaultPageCSS = require("../res/defaultPageCSS.js");
@ -10,6 +9,7 @@ const matchHostname = require("../utils/matchHostname.js");
const generateServerString = require("../utils/generateServerString.js"); const generateServerString = require("../utils/generateServerString.js");
const parseURL = require("../utils/urlParser.js"); const parseURL = require("../utils/urlParser.js");
const deepClone = require("../utils/deepClone.js"); const deepClone = require("../utils/deepClone.js");
const statusCodes = require("../res/statusCodes.js");
let serverconsole = {}; let serverconsole = {};
let middleware = []; let middleware = [];
@ -134,7 +134,7 @@ function requestHandler(req, res) {
req.socket.remoteAddress == "localhost") req.socket.remoteAddress == "localhost")
) { ) {
let headers = config.getCustomHeaders(); let headers = config.getCustomHeaders();
res.writeHead(204, http.STATUS_CODES[204], headers); res.writeHead(204, statusCodes[204], headers);
res.end(); res.end();
return; return;
} }
@ -173,9 +173,9 @@ function requestHandler(req, res) {
"Server responded with " + code.toString() + " code." "Server responded with " + code.toString() + " code."
); );
} }
if (typeof codeDescription != "string" && http.STATUS_CODES[code]) { if (typeof codeDescription != "string" && statusCodes[code]) {
if (!headers) headers = codeDescription; if (!headers) headers = codeDescription;
codeDescription = http.STATUS_CODES[code]; codeDescription = statusCodes[code];
} }
lastStatusCode = code; lastStatusCode = code;
} }
@ -444,7 +444,7 @@ function requestHandler(req, res) {
fs.readFile(errorFile, (err, data) => { fs.readFile(errorFile, (err, data) => {
try { try {
if (err) throw err; if (err) throw err;
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, statusCodes[errorCode], cheaders);
res.responseEnd( res.responseEnd(
data data
.toString() .toString()
@ -452,7 +452,7 @@ function requestHandler(req, res) {
/{errorMessage}/g, /{errorMessage}/g,
errorCode.toString() + errorCode.toString() +
" " + " " +
http.STATUS_CODES[errorCode] statusCodes[errorCode]
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")
@ -523,7 +523,7 @@ function requestHandler(req, res) {
additionalError = 508; additionalError = 508;
} }
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, statusCodes[errorCode], cheaders);
res.write( res.write(
`<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>${ `<!DOCTYPE html><html><head><title>{errorMessage}</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>${
additionalError == 404 additionalError == 404
@ -534,7 +534,7 @@ function requestHandler(req, res) {
/{errorMessage}/g, /{errorMessage}/g,
errorCode.toString() + errorCode.toString() +
" " + " " +
http.STATUS_CODES[errorCode] statusCodes[errorCode]
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")
@ -620,7 +620,7 @@ function requestHandler(req, res) {
: 301; : 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, statusCodes[statusCode], customHeaders);
// Log the redirection message // Log the redirection message
logFacilities.resmessage("Client redirected to " + destination); logFacilities.resmessage("Client redirected to " + destination);
@ -656,7 +656,7 @@ function requestHandler(req, res) {
// Respond with list of methods // Respond with list of methods
let hdss = config.getCustomHeaders(); let hdss = config.getCustomHeaders();
hdss["Allow"] = "GET, POST, HEAD, OPTIONS"; hdss["Allow"] = "GET, POST, HEAD, OPTIONS";
res.writeHead(204, http.STATUS_CODES[204], hdss); res.writeHead(204, statusCodes[204], hdss);
res.end(); res.end();
return; return;
} else { } else {

View file

@ -1,5 +1,5 @@
const http = require("http");
const defaultPageCSS = require("../res/defaultPageCSS.js"); const defaultPageCSS = require("../res/defaultPageCSS.js");
const statusCodes = require("../res/statusCodes.js");
const svrjsInfo = require("../../svrjs.json"); const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo; const { name } = svrjsInfo;
@ -7,7 +7,7 @@ module.exports = (req, res, logFacilities, config, next) => {
if (req.isProxy) { if (req.isProxy) {
let eheaders = config.getCustomHeaders(); let eheaders = config.getCustomHeaders();
eheaders["Content-Type"] = "text/html; charset=utf-8"; eheaders["Content-Type"] = "text/html; charset=utf-8";
res.writeHead(501, http.STATUS_CODES[501], eheaders); res.writeHead(501, statusCodes[501], eheaders);
res.write( res.write(
`<!DOCTYPE html><html><head><title>Proxy not implemented</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>Proxy not implemented</h1><p>${name `<!DOCTYPE html><html><head><title>Proxy not implemented</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>${defaultPageCSS}</style></head><body><h1>Proxy not implemented</h1><p>${name
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
@ -34,7 +34,7 @@ module.exports = (req, res, logFacilities, config, next) => {
if (req.method == "OPTIONS") { if (req.method == "OPTIONS") {
let hdss = config.getCustomHeaders(); let hdss = config.getCustomHeaders();
hdss["Allow"] = "GET, POST, HEAD, OPTIONS"; hdss["Allow"] = "GET, POST, HEAD, OPTIONS";
res.writeHead(204, http.STATUS_CODES[204], hdss); res.writeHead(204, statusCodes[204], hdss);
res.end(); res.end();
return; return;
} else if ( } else if (

View file

@ -1,4 +1,3 @@
const http = require("http");
const fs = require("fs"); const fs = require("fs");
const os = require("os"); const os = require("os");
const zlib = require("zlib"); const zlib = require("zlib");
@ -9,6 +8,7 @@ const ipMatch = require("../utils/ipMatch.js");
const createRegex = require("../utils/createRegex.js"); const createRegex = require("../utils/createRegex.js");
const sha256 = require("../utils/sha256.js"); const sha256 = require("../utils/sha256.js");
const sizify = require("../utils/sizify.js"); const sizify = require("../utils/sizify.js");
const statusCodes = require("../res/statusCodes.js");
const svrjsInfo = require("../../svrjs.json"); const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo; 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) // Check if the client's request matches the ETag value (If-None-Match)
const clientETag = req.headers["if-none-match"]; const clientETag = req.headers["if-none-match"];
if (clientETag === fileETag) { if (clientETag === fileETag) {
res.writeHead(304, http.STATUS_CODES[304], { res.writeHead(304, statusCodes[304], {
ETag: clientETag ETag: clientETag
}); });
res.end(); res.end();
@ -206,14 +206,14 @@ module.exports = (req, res, logFacilities, config, next) => {
begin < res.head.length && begin < res.head.length &&
end - 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)); res.end(res.head.substring(begin, end + 1));
return; return;
} else if ( } else if (
ext == "html" && ext == "html" &&
begin >= res.head.length + filelen begin >= res.head.length + filelen
) { ) {
res.writeHead(206, http.STATUS_CODES[206], rhd); res.writeHead(206, statusCodes[206], rhd);
res.end( res.end(
res.foot.substring( res.foot.substring(
begin - res.head.length - filelen, 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) { if (res.head.length == 0 || begin > res.head.length) {
afterWriteCallback(); afterWriteCallback();
} else if ( } else if (
@ -291,7 +291,7 @@ module.exports = (req, res, logFacilities, config, next) => {
process.nextTick(afterWriteCallback); process.nextTick(afterWriteCallback);
} }
} else { } else {
res.writeHead(206, http.STATUS_CODES[206], rhd); res.writeHead(206, statusCodes[206], rhd);
readStream.pipe(res); readStream.pipe(res);
} }
logFacilities.resmessage( logFacilities.resmessage(
@ -302,7 +302,7 @@ module.exports = (req, res, logFacilities, config, next) => {
} }
}); });
} else { } else {
res.writeHead(206, http.STATUS_CODES[206], rhd); res.writeHead(206, statusCodes[206], rhd);
res.end(); res.end();
} }
} }
@ -461,7 +461,7 @@ module.exports = (req, res, logFacilities, config, next) => {
end: res.foot.length == 0 end: res.foot.length == 0
}); });
}; };
res.writeHead(200, http.STATUS_CODES[200], hdhds); res.writeHead(200, statusCodes[200], hdhds);
if (res.head.length == 0) { if (res.head.length == 0) {
afterWriteCallback(); afterWriteCallback();
} else if (!resStream.write(res.head)) { } else if (!resStream.write(res.head)) {
@ -470,7 +470,7 @@ module.exports = (req, res, logFacilities, config, next) => {
process.nextTick(afterWriteCallback); process.nextTick(afterWriteCallback);
} }
} else { } else {
res.writeHead(200, http.STATUS_CODES[200], hdhds); res.writeHead(200, statusCodes[200], hdhds);
readStream.pipe(resStream); readStream.pipe(resStream);
} }
logFacilities.resmessage( logFacilities.resmessage(
@ -481,7 +481,7 @@ module.exports = (req, res, logFacilities, config, next) => {
} }
}); });
} else { } else {
res.writeHead(200, http.STATUS_CODES[200], hdhds); res.writeHead(200, statusCodes[200], hdhds);
res.end(); res.end();
logFacilities.resmessage("Client successfully received content."); logFacilities.resmessage("Client successfully received content.");
} }
@ -890,7 +890,7 @@ module.exports = (req, res, logFacilities, config, next) => {
} }
// Send the directory listing response // Send the directory listing response
res.writeHead(200, http.STATUS_CODES[200], { res.writeHead(200, statusCodes[200], {
"Content-Type": "text/html; charset=utf-8" "Content-Type": "text/html; charset=utf-8"
}); });
res.end( res.end(

View file

@ -1,7 +1,7 @@
const http = require("http");
const os = require("os"); const os = require("os");
const defaultPageCSS = require("../res/defaultPageCSS.js"); const defaultPageCSS = require("../res/defaultPageCSS.js");
const sizify = require("../utils/sizify.js"); const sizify = require("../utils/sizify.js");
const statusCodes = require("../res/statusCodes.js");
const svrjsInfo = require("../../svrjs.json"); const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo; const { name } = svrjsInfo;
@ -57,7 +57,7 @@ module.exports = (req, res, logFacilities, config, next) => {
}%`; }%`;
statusBody += `<br/>Thread PID: ${process.pid}<br/>`; statusBody += `<br/>Thread PID: ${process.pid}<br/>`;
res.writeHead(200, http.STATUS_CODES[200], { res.writeHead(200, statusCodes[200], {
"Content-Type": "text/html; charset=utf-8" "Content-Type": "text/html; charset=utf-8"
}); });
res.end( res.end(

10
src/res/statusCodes.js Normal file
View file

@ -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;