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.
This commit is contained in:
dgw 2017-09-24 06:37:24 -05:00
parent 9e008a76b7
commit 09cd9ce33a

View File

@ -159,7 +159,14 @@ function fetch(uri, cb) {
var limit = Helper.config.prefetchMaxImageSize * 1024; var limit = Helper.config.prefetchMaxImageSize * 1024;
req req
.on("response", function(res) { .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 // 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 // twitter.com sends opengraph meta tags within ~20kb of data for individual tweets
limit = 1024 * 50; limit = 1024 * 50;