From 737ed0f66ea2f5351b440c4a387091c5374758b3 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 3 Apr 2021 14:51:54 +0200 Subject: [PATCH] [HTML5] Disable body_size in fetch. We were using `Content-Length` from the server when `Content-Encoding` was not set (i.e. response was not compressed). Sadly, in CORS requests accessing headers is restricted, and while `Content-Length` is enabled by default, `Content-Encoding` is not. This results in the impossibility of knowing if the content was compressed, unless the server explicitly enabled the encoding header via `Access-Control-Expose-Headers`. To keep maximum compatibility we must disable `body_size` completely. --- platform/javascript/js/engine/preloader.js | 17 ----------------- .../javascript/js/libs/library_godot_fetch.js | 13 +------------ 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/platform/javascript/js/engine/preloader.js b/platform/javascript/js/engine/preloader.js index 3535fdb361bc..564c68d26449 100644 --- a/platform/javascript/js/engine/preloader.js +++ b/platform/javascript/js/engine/preloader.js @@ -1,22 +1,5 @@ const Preloader = /** @constructor */ function () { // eslint-disable-line no-unused-vars function getTrackedResponse(response, load_status) { - let clen = 0; - let compressed = false; - response.headers.forEach(function (value, header) { - const h = header.toLowerCase().trim(); - // We can't accurately compute compressed stream length. - if (h === 'content-encoding') { - compressed = true; - } else if (h === 'content-length') { - const length = parseInt(value, 10); - if (!Number.isNaN(length) && length > 0) { - clen = length; - } - } - }); - if (!compressed && clen) { - load_status.total = clen; - } function onloadprogress(reader, controller) { return reader.read().then(function (result) { if (load_status.done) { diff --git a/platform/javascript/js/libs/library_godot_fetch.js b/platform/javascript/js/libs/library_godot_fetch.js index 4ae6a2359371..de5ae2b1ae08 100644 --- a/platform/javascript/js/libs/library_godot_fetch.js +++ b/platform/javascript/js/libs/library_godot_fetch.js @@ -49,25 +49,14 @@ const GodotFetch = { if (!obj) { return; } - let size = -1; - let compressed = false; let chunked = false; response.headers.forEach(function (value, header) { const v = value.toLowerCase().trim(); const h = header.toLowerCase().trim(); - if (h === 'content-encoding') { - compressed = true; - size = -1; - } else if (h === 'content-length') { - const len = Number.parseInt(value, 10); - if (!Number.isNaN(len) && !compressed) { - size = len; - } - } else if (h === 'transfer-encoding' && v === 'chunked') { + if (h === 'transfer-encoding' && v === 'chunked') { chunked = true; } }); - obj.bodySize = size; obj.status = response.status; obj.response = response; obj.reader = response.body.getReader();