Add Lzpf compression

This commit is contained in:
Eric MacDonald
2021-08-06 01:42:27 -04:00
parent 743989d4f0
commit f12717bfef
3 changed files with 433 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ const mime = require('mime-types');
const { crc16 } = require('easy-crc');
const process = require('process');
var WTVSec = require('./WTVSec.js');
var WTVLzpf = require('./WTVLzpf.js');
var WTVClientCapabilities = require('./WTVClientCapabilities.js');
var WTVClientSessionData = require('./WTVClientSessionData.js');
@@ -435,7 +436,7 @@ function headerStringToObj(headers, response = false) {
return headers_obj;
}
async function sendToClient(socket, headers_obj, data) {
async function sendToClient(socket, headers_obj, data, compress_data = false) {
var headers = "";
if (typeof (data) === 'undefined') data = '';
if (typeof (headers_obj) === 'string') {
@@ -455,6 +456,19 @@ async function sendToClient(socket, headers_obj, data) {
headers_obj = moveObjectElement('Connection', 'http_response', headers_obj);
}
// If wtv-lzpf is in the header then force compression
if (headers_obj["wtv-lzpf"]) {
compress_data = true;
}
// compress if needed
if (compress_data && clen > 0) {
headers_obj["wtv-lzpf"] = "0";
var lzpf = new WTVLzpf();
data = lzpf.Compress(data);
}
// encrypt if needed
if (socket_sessions[socket.id].secure == true) {
var clen = null;
@@ -483,6 +497,8 @@ async function sendToClient(socket, headers_obj, data) {
}
// calculate content length
// On the WNI server this is the length before compression but we're using the length after compression.
// It matches the HTTP spec anyway so leaving.
if (typeof data.length !== 'undefined') {
headers_obj["Content-length"] = data.length;
} else if (typeof data.byteLength !== 'undefined') {