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; if (dh) bheaderso = dh;
var code = 200; var code = 200;
var msg = "OK"; var msg = "OK";
if (bheaders[0].indexOf("HTTP/") == 0) { var httpMatch = bheaders[0].match(/^HTTP\/[^ ]+ ([0-9]{3})(?: (.*))?$/);
var heada = bheaders.shift(); if (httpMatch) {
var hso = heada.split(" "); bheaders.shift();
code = hso[1]; code = parseInt(httpMatch[1]);
if (hso[2] !== undefined) msg = heada.split(" ").splice(2).join(" "); if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
} else if (bheaders[0].indexOf(":") == -1) { } else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift(); var heada = bheaders.shift();
var hso = heada.split(" "); var hso = heada.match(/^([0-9]{3})(?: (.*))?$/);
if (hso[0].match(/^[0-9]{3}$/)) { if (hso) {
code = hso[0]; code = parseInt(hso[1]);
if (hso[1] !== undefined) msg = heada.split(" ").splice(1).join(" "); if (hso[2]) msg = hso[2];
else msg = http.STATUS_CODES[code];
} }
} }
for (var i = 0; i < bheaders.length; i++) { for (var i = 0; i < bheaders.length; i++) {
var headerp = bheaders[i].split(": "); var headerp = bheaders[i].match(/^([^:]*)(?:: (.*))?/);
var headern = headerp.shift(); if(!headerp) headerp = [];
var headerv = headerp.join(": "); var headern = headerp[1];
var headerv = headerp[2];
if (headern.toLowerCase() == "status") { if (headern.toLowerCase() == "status") {
code = headerv.split(" ")[0]; var httpMatch = headerv.match(/^([0-9]{3})(?: (.*))?$/);
if (headerv.split(" ")[1] !== undefined) msg = headerv.split(" ").splice(1).join(" "); if(httpMatch) {
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
}
} else if (headern.toLowerCase() == "set-cookie") { } else if (headern.toLowerCase() == "set-cookie") {
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = []; if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
bheaderso["Set-Cookie"].push(headerv); bheaderso["Set-Cookie"].push(headerv);