Improved CGI response parser

This commit is contained in:
Dorian Niemiec 2023-08-30 18:13:30 +02:00
parent 9381b3dec0
commit a80d91de48

View file

@ -193,26 +193,33 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
if (dh) bheaderso = dh;
var code = 200;
var msg = "OK";
if (bheaders[0].indexOf("HTTP/") == 0) {
var heada = bheaders.shift();
var hso = heada.split(" ");
code = hso[1];
if (hso[2] !== undefined) msg = heada.split(" ").splice(2).join(" ");
var httpMatch = bheaders[0].match(/^HTTP\/[^ ]+ ([0-9]{3})(?: (.*))?$/);
if (httpMatch) {
bheaders.shift();
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
} else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift();
var hso = heada.split(" ");
if (hso[0].match(/^[0-9]{3}$/)) {
code = hso[0];
if (hso[1] !== undefined) msg = heada.split(" ").splice(1).join(" ");
var hso = heada.match(/^([0-9]{3})(?: (.*))?$/);
if (hso) {
code = parseInt(hso[1]);
if (hso[2]) msg = hso[2];
else msg = http.STATUS_CODES[code];
}
}
for (var i = 0; i < bheaders.length; i++) {
var headerp = bheaders[i].split(": ");
var headern = headerp.shift();
var headerv = headerp.join(": ");
var headerp = bheaders[i].match(/^([^:]*)(?:: (.*))?/);
if(!headerp) headerp = [];
var headern = headerp[1];
var headerv = headerp[2];
if (headern.toLowerCase() == "status") {
code = headerv.split(" ")[0];
if (headerv.split(" ")[1] !== undefined) msg = headerv.split(" ").splice(1).join(" ");
var httpMatch = headerv.match(/^([0-9]{3})(?: (.*))?$/);
if(httpMatch) {
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
}
} else if (headern.toLowerCase() == "set-cookie") {
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
bheaderso["Set-Cookie"].push(headerv);