This commit is contained in:
zefie
2021-08-11 22:55:18 -04:00
parent 3d693e176f
commit e32913b10d

View File

@@ -711,6 +711,11 @@ async function sendToClient(socket, headers_obj, data) {
} }
}); });
if (headers_obj["Connection"]) {
if (headers_obj["Connection"].toLowerCase() == "close" && wtv_connection_close == "true") {
socket_sessions[socket.id].destroy_me = true;
}
}
// send to client // send to client
var toClient = null; var toClient = null;
@@ -729,7 +734,29 @@ async function sendToClient(socket, headers_obj, data) {
} }
if (minisrv_config.config.debug_flags.quiet) console.log(" * Sent" + verbosity_mod + " " + headers_obj.http_response + " to client (Content-Type:", headers_obj['Content-Type'], "~", headers_obj['Content-length'], "bytes)"); if (minisrv_config.config.debug_flags.quiet) console.log(" * Sent" + verbosity_mod + " " + headers_obj.http_response + " to client (Content-Type:", headers_obj['Content-Type'], "~", headers_obj['Content-length'], "bytes)");
} }
}
async function sendToSocket(socket, data, chunk_offset = 0) {
// buffer size = lesser of minisrv_config.config.chunk_size or size remaining
var chunk_size = 16384;
var can_write = true;
var data_left = 0;
while (data_left != data.byteLength && can_write) {
var data_left = (data.byteLength - chunk_offset);
if (data_left === 0) break;
var buffer_size = (data_left >= chunk_size) ? chunk_size : data_left;
var chunk = new Buffer.alloc(buffer_size);
chunk_offset += data.copy(chunk, 0, chunk_offset, (chunk_offset + buffer_size));
can_write = socket.write(chunk);
if (!can_write) {
socket.once('drain', function () {
sendToSocket(socket, data, socket.bytesWritten);
});
break;
}
}
if (data_left == data.byteLength) {
if (socket_sessions[socket.id].expecting_post_data) delete socket_sessions[socket.id].expecting_post_data; if (socket_sessions[socket.id].expecting_post_data) delete socket_sessions[socket.id].expecting_post_data;
if (socket_sessions[socket.id].header_buffer) delete socket_sessions[socket.id].header_buffer; if (socket_sessions[socket.id].header_buffer) delete socket_sessions[socket.id].header_buffer;
if (socket_sessions[socket.id].secure_buffer) delete socket_sessions[socket.id].secure_buffer; if (socket_sessions[socket.id].secure_buffer) delete socket_sessions[socket.id].secure_buffer;
@@ -740,32 +767,9 @@ async function sendToClient(socket, headers_obj, data) {
if (socket_sessions[socket.id].post_data_percents_shown) delete socket_sessions[socket.id].post_data_percents_shown; if (socket_sessions[socket.id].post_data_percents_shown) delete socket_sessions[socket.id].post_data_percents_shown;
socket.setTimeout(minisrv_config.config.socket_timeout * 1000); socket.setTimeout(minisrv_config.config.socket_timeout * 1000);
if (socket_sessions[socket.id].close_me) socket.end(); if (socket_sessions[socket.id].close_me) socket.end();
if (headers_obj["Connection"]) { if (socket_sessions[socket.id].destroy_me) socket.destroy();
if (headers_obj["Connection"].toLowerCase() == "close" && wtv_connection_close == "true") {
socket.destroy();
} }
} }
}
async function sendToSocket(socket, data, chunk_offset = 0) {
socket.setNoDelay(true);
// buffer size = lesser of minisrv_config.config.chunk_size or size remaining
var chunk_size = 16384;
var can_write = true;
while (socket.bytesWritten != data.byteLength && can_write) {
var buffer_size = ((data.byteLength - chunk_offset) >= chunk_size) ? chunk_size : (data.byteLength - chunk_offset);
var chunk = new Buffer.alloc(buffer_size);
data.copy(chunk, 0, chunk_offset, (chunk_offset + buffer_size));
can_write = socket.write(chunk);
if (!can_write) {
socket.once('drain', function () {
sendToSocket(socket, data, socket.bytesWritten);
});
break;
}
}
if (socket.bytesWritten == data.byteLength) socket.end();
}
function concatArrayBuffer(buffer1, buffer2) { function concatArrayBuffer(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength); var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);