1
0
Fork 0
forked from svrjs/svrjs

Added res.head, res.foot, res.responseWrite, and rejection of expectations.

This commit is contained in:
Dorian Niemiec 2024-08-23 22:10:49 +02:00
parent 881502d897
commit 0c9013feac

View file

@ -295,6 +295,16 @@ module.exports = (req, res, logFacilities, config, next) => {
"Host name rewritten: " + oldHostHeader + " => " + req.headers.host, "Host name rewritten: " + oldHostHeader + " => " + req.headers.host,
); );
// Header and footer placeholders
res.head = "";
res.foot = "";
res.responseEnd = (body) => {
// If body is Buffer, then it is converted to String anyway.
res.write(head + body + foot);
res.end();
}
// Server error calling method // Server error calling method
res.error = (errorCode, extName, stack, ch) => { res.error = (errorCode, extName, stack, ch) => {
if (typeof errorCode !== "number") { if (typeof errorCode !== "number") {
@ -444,8 +454,7 @@ module.exports = (req, res, logFacilities, config, next) => {
try { try {
if (err) throw err; if (err) throw err;
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
//TODO: res.end -> responseEnd res.responseEnd(
res.end(
data data
.toString() .toString()
.replace( .replace(
@ -660,6 +669,13 @@ module.exports = (req, res, logFacilities, config, next) => {
return; return;
}; };
try {
res.head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header
res.foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer
} catch (err) {
callServerError(500, err);
}
// Authenticated user variable // Authenticated user variable
req.authUser = null; req.authUser = null;
@ -679,6 +695,12 @@ module.exports = (req, res, logFacilities, config, next) => {
} }
} }
if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
// Expectations not met.
callServerError(417);
return;
}
if (req.method == "CONNECT") { if (req.method == "CONNECT") {
// CONNECT requests should be handled in "connect" event. // CONNECT requests should be handled in "connect" event.
callServerError(501); callServerError(501);