From 09cd9ce33a28c87f85e93bbe1fc7e16a81876858 Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 24 Sep 2017 06:37:24 -0500 Subject: [PATCH] Abort img prefetch if Content-Length exceeds limit If the Content-Length header is present in the response when an image is prefetched, The Lounge can avoid wasting bandwidth (both for itself and for the image's host) if the value of the header exceeds the prefetch size limit by aborting the request immediately. --- src/plugins/irc-events/link.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index 06049b15..45a969c5 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -159,7 +159,14 @@ function fetch(uri, cb) { var limit = Helper.config.prefetchMaxImageSize * 1024; req .on("response", function(res) { - if (!(/^image\/.+/.test(res.headers["content-type"]))) { + if (/^image\/.+/.test(res.headers["content-type"])) { + // response is an image + // if Content-Length header reports a size exceeding the prefetch limit, abort fetch + const contentLength = parseInt(res.headers["content-length"], 10) || 0; + if (contentLength > limit) { + req.abort(); + } + } else { // if not image, limit download to 50kb, since we need only meta tags // twitter.com sends opengraph meta tags within ~20kb of data for individual tweets limit = 1024 * 50;