From a80d91de48fe5662e660b44483a308096d6baa43 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Wed, 30 Aug 2023 18:13:30 +0200 Subject: [PATCH] Improved CGI response parser --- index.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 9e021c2..3651240 100644 --- a/index.js +++ b/index.js @@ -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);