From 9ae1a5d1c95492cdaecdb208f9458ec5aab71baa Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Fri, 17 Aug 2018 10:21:20 +0200
Subject: [PATCH] fix #2466. Avoid caching half fetched files

When a remote resource exceeds the fetchsize but the remote server does
not return a Content-Length, we read only the fetchsize amount of bytes
but failed to detect that this was a partial read, thus a partial
resource got cached.

This fix will read fetchsize+1, which will then be correctly determined
as too big and thrown away.
---
 inc/HTTPClient.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php
index e20b7d98f..9a20dc598 100644
--- a/inc/HTTPClient.php
+++ b/inc/HTTPClient.php
@@ -434,14 +434,14 @@ class HTTPClient {
                 // read up to the content-length or max_bodysize
                 // for keep alive we need to read the whole message to clean up the socket for the next read
                 if(!$this->keep_alive && $this->max_bodysize && $this->max_bodysize < $this->resp_headers['content-length']){
-                    $length = $this->max_bodysize;
+                    $length = $this->max_bodysize+1;
                 }else{
                     $length = $this->resp_headers['content-length'];
                 }
 
                 $r_body = $this->_readData($socket, $length, 'response (content-length limited)', true);
             }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){
-                $r_body = $this->_readData($socket, $this->max_bodysize, 'response (content-length limited)', true);
+                $r_body = $this->_readData($socket, $this->max_bodysize+1, 'response (content-length limited)', true);
             } elseif ((int)$this->status === 204) {
                 // request has no content
             } else{
@@ -451,7 +451,7 @@ class HTTPClient {
                 }
             }
 
-            // recheck body size, we might had to read the whole body, so we abort late or trim here
+            // recheck body size, we might have read max_bodysize+1 or even the whole body, so we abort late here
             if($this->max_bodysize){
                 if(strlen($r_body) > $this->max_bodysize){
                     if ($this->max_bodysize_abort) {
-- 
GitLab