From 9f8b0f4fe3753eea81d292e71412d84df0f24dd7 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 9 Mar 2024 15:48:23 +0100 Subject: [PATCH] Remove try/catch block in HTTP/1.x compatibility shim; errors there are non-existent --- svr.js | 90 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 50 deletions(-) diff --git a/svr.js b/svr.js index 4410e82..ab1ad0d 100644 --- a/svr.js +++ b/svr.js @@ -2918,58 +2918,48 @@ if (!cluster.isPrimary) { // Make HTTP/1.x API-based scripts compatible with HTTP/2.0 API if (configJSON.enableHTTP2 == true && req.httpVersion == "2.0") { - try { - // Set HTTP/1.x methods (to prevent process warnings) - res.writeHeadNodeApi = res.writeHead; - res.setHeaderNodeApi = res.setHeader; + // Set HTTP/1.x methods (to prevent process warnings) + res.writeHeadNodeApi = res.writeHead; + res.setHeaderNodeApi = res.setHeader; - res.writeHead = function (a, b, c) { - var table = c; - if (typeof (b) == "object") table = b; - if (table == undefined) table = this.tHeaders; - table = JSON.parse(JSON.stringify(table)); - if (table["content-type"] != undefined && table["Content-Type"] != undefined) { - delete table["content-type"]; - } - delete table["transfer-encoding"]; - delete table["connection"]; - delete table["keep-alive"]; - delete table["upgrade"]; - if (res.stream && res.stream.destroyed) { - return false; - } else { - return res.writeHeadNodeApi(a, table); - } - }; - res.setHeader = function (headerName, headerValue) { - var al = headerName.toLowerCase(); - if (al != "transfer-encoding" && al != "connection" && al != "keep-alive" && al != "upgrade") return res.setHeaderNodeApi(headerName, headerValue); - return false; - }; - - // Set HTTP/1.x headers - if (!req.headers.host) req.headers.host = req.headers[":authority"]; - (req.headers[":path"] == undefined ? (function () {})() : req.url = req.headers[":path"]); - req.protocol = req.headers[":scheme"]; - var headers = [":path", ":method"]; - for (var i = 0; i < headers.length; i++) { - if (req.headers[headers[i]] == undefined) { - var cheaders = getCustomHeaders(); - cheaders["Content-Type"] = "text/html; charset=utf-8"; - res.writeHead(400, "Bad Request", cheaders); - res.write("400 Bad Request

400 Bad Request

The request you sent is invalid.

" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS").replace(/&/g, "&").replace(//g, ">") + (req.headers[":authority"] == undefined ? "" : " on " + req.headers[":authority"]) + "

"); - res.end(); - return; - } + res.writeHead = function (a, b, c) { + var table = c; + if (typeof (b) == "object") table = b; + if (table == undefined) table = this.tHeaders; + table = JSON.parse(JSON.stringify(table)); + if (table["content-type"] != undefined && table["Content-Type"] != undefined) { + delete table["content-type"]; + } + delete table["transfer-encoding"]; + delete table["connection"]; + delete table["keep-alive"]; + delete table["upgrade"]; + if (res.stream && res.stream.destroyed) { + return false; + } else { + return res.writeHeadNodeApi(a, table); + } + }; + res.setHeader = function (headerName, headerValue) { + var al = headerName.toLowerCase(); + if (al != "transfer-encoding" && al != "connection" && al != "keep-alive" && al != "upgrade") return res.setHeaderNodeApi(headerName, headerValue); + return false; + }; + + // Set HTTP/1.x headers + if (!req.headers.host) req.headers.host = req.headers[":authority"]; + (req.headers[":path"] == undefined ? (function () {})() : req.url = req.headers[":path"]); + req.protocol = req.headers[":scheme"]; + var headers = [":path", ":method"]; + for (var i = 0; i < headers.length; i++) { + if (req.headers[headers[i]] == undefined) { + var cheaders = getCustomHeaders(); + cheaders["Content-Type"] = "text/html; charset=utf-8"; + res.writeHead(400, "Bad Request", cheaders); + res.write("400 Bad Request

400 Bad Request

The request you sent is invalid.

" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS").replace(/&/g, "&").replace(//g, ">") + (req.headers[":authority"] == undefined ? "" : " on " + req.headers[":authority"]) + "

"); + res.end(); + return; } - } catch (err) { - var cheaders = getCustomHeaders(); - cheaders["Content-Type"] = "text/html; charset=utf-8"; - cheaders[":status"] = "500"; - res.stream.respond(cheaders); - res.stream.write("500 Internal Server Error

500 Internal Server Error

The server had an unexpected error. Below, error stack is shown:

" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, "  ") + "

Please contact with developer/administrator of the website.

" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS").replace(/&/g, "&").replace(//g, ">") + (req.headers[":authority"] == undefined ? "" : " on " + req.headers[":authority"]) + "

"); - res.stream.end(); - return; } }