From 8c7c413938c6ae36c403c044af6faae3b1a3e9d3 Mon Sep 17 00:00:00 2001 From: Tim Wilkinson Date: Mon, 16 Sep 2024 18:11:31 -0700 Subject: [PATCH] Improve when compression is used on http requests (#1515) * Improve when compression is used on http requests * Improve --- files/app/root.ut | 50 ++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/files/app/root.ut b/files/app/root.ut index f5122e74..3838341c 100644 --- a/files/app/root.ut +++ b/files/app/root.ut @@ -545,47 +545,35 @@ global.handle_request = function(env) res = `
ERROR: ${e.message}
${e.stacktrace[0].context}
`; } if (!response.override) { - if (index(env.HTTP_ACCEPT_ENCODING || "", "gzip") === -1 || !config.compress) { - response.headers["Content-Length"] = `${length(res)}`; - uhttpd.send( - `Status: ${response.statusCode} OK\r\n`, - join("", map(keys(response.headers), k => k + ": " + response.headers[k] + "\r\n")), - "\r\n", - res - ); - } - else { + let datafile = null; + if (env.HTTP_ACCEPT_ENCODING && index(env.HTTP_ACCEPT_ENCODING, "gzip") !== -1 && config.compress) { const r = fs.open("/dev/urandom"); - let datafile; if (r) { const rid = r.read(8); r.close(); datafile = `/tmp/uhttpd.${hexenc(rid)}`; - } - else { - datafile = `/tmp/uhttpd.${time()}${math.rand()}`; - } - try { - fs.writefile(datafile, res); - const z = fs.popen("exec /bin/gzip -c " + datafile); - try { + const x = fs.open(datafile, "wx"); + if (x) { + x.write(res); + x.close(); + const z = fs.popen("exec /bin/gzip -c " + datafile); res = z.read("all"); - response.headers["Content-Length"] = `${length(res)}`; - uhttpd.send( - `Status: ${response.statusCode} OK\r\nContent-Encoding: gzip\r\n`, - join("", map(keys(response.headers), k => k + ": " + response.headers[k] + "\r\n")), - "\r\n", - res - ); + z.close(); + fs.unlink(datafile); } - catch (_) { + else { + datafile = null; } - z.close(); } - catch (_) { - } - fs.unlink(datafile); } + response.headers["Content-Length"] = `${length(res)}`; + uhttpd.send( + `Status: ${response.statusCode} OK\r\n`, + (datafile ? `Content-Encoding: gzip\r\n` : ``), + join("", map(keys(response.headers), k => k + ": " + response.headers[k] + "\r\n")), + "\r\n", + res + ); } if (response.reboot) { system("(sleep 2; exec /sbin/reboot)&");