From 7d87ae2b0e6d3218f23d879f8e67d3f711040e62 Mon Sep 17 00:00:00 2001 From: Sebastian Kostka Date: Sun, 9 Apr 2017 10:13:39 +0200 Subject: [PATCH 1/5] Work around file size issues at 32 Bit systems Replace stream_copy_to_stream with a chunked process, to handle file sizes hat do not fit into an integer. Implementation according to http://php.net/manual/en/function.stream-copy-to-stream.php#98119, proposed and implemented by https://github.com/rikmeijer in https://github.com/nextcloud/server/issues/1707#issuecomment-291835214. --- lib/Sapi.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Sapi.php b/lib/Sapi.php index dd49913..4101f82 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -88,7 +88,9 @@ static function sendResponse(ResponseInterface $response) { if ($contentLength !== null) { $output = fopen('php://output', 'wb'); if (is_resource($body) && get_resource_type($body) == 'stream') { - stream_copy_to_stream($body, $output, (int)$contentLength); + while (!feof($body)) { + fwrite($output,fread($body,8192)); + } } else { fwrite($output, $body, (int)$contentLength); } From 059b6800b51065aa5df1cb99fa87094868eb7c90 Mon Sep 17 00:00:00 2001 From: Sebastian Kostka Date: Sun, 11 Jun 2017 22:25:21 +0200 Subject: [PATCH 2/5] Create Sapi.php --- lib/Sapi.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Sapi.php b/lib/Sapi.php index 4101f82..534a8f0 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -88,8 +88,14 @@ static function sendResponse(ResponseInterface $response) { if ($contentLength !== null) { $output = fopen('php://output', 'wb'); if (is_resource($body) && get_resource_type($body) == 'stream') { - while (!feof($body)) { - fwrite($output,fread($body,8192)); + if PHP_INT_SIZE != 4){ + // use the dedicated function on 64 Bit systems + stream_copy_to_stream($body, $output, $contentLength); + } else { + // workaround for 32 Bit systems to avoid stream_copy_to_stream + while (!feof($body)) { + fwrite($output,fread($body,8192)); + } } } else { fwrite($output, $body, (int)$contentLength); From 98246e17ef89d6aef8414cf8acad5705f02d2305 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 12 Jun 2017 09:41:50 +0200 Subject: [PATCH 3/5] fixed typos --- lib/Sapi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Sapi.php b/lib/Sapi.php index 534a8f0..73d16e3 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -88,14 +88,14 @@ static function sendResponse(ResponseInterface $response) { if ($contentLength !== null) { $output = fopen('php://output', 'wb'); if (is_resource($body) && get_resource_type($body) == 'stream') { - if PHP_INT_SIZE != 4){ + if (PHP_INT_SIZE != 4){ // use the dedicated function on 64 Bit systems stream_copy_to_stream($body, $output, $contentLength); } else { // workaround for 32 Bit systems to avoid stream_copy_to_stream while (!feof($body)) { fwrite($output,fread($body,8192)); - } + } } } else { fwrite($output, $body, (int)$contentLength); From 528aca31ae1324d053c6c362085bc7b766098d31 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 12 Jun 2017 09:42:24 +0200 Subject: [PATCH 4/5] fixed CS --- lib/Sapi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Sapi.php b/lib/Sapi.php index 73d16e3..76d1c1f 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -94,7 +94,7 @@ static function sendResponse(ResponseInterface $response) { } else { // workaround for 32 Bit systems to avoid stream_copy_to_stream while (!feof($body)) { - fwrite($output,fread($body,8192)); + fwrite($output, fread($body, 8192)); } } } else { From 95a0e202ba51b77d4035a5c4c3e2581cd1812425 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 12 Jun 2017 09:43:17 +0200 Subject: [PATCH 5/5] strict type check --- lib/Sapi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Sapi.php b/lib/Sapi.php index 76d1c1f..a93ea1e 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -88,7 +88,7 @@ static function sendResponse(ResponseInterface $response) { if ($contentLength !== null) { $output = fopen('php://output', 'wb'); if (is_resource($body) && get_resource_type($body) == 'stream') { - if (PHP_INT_SIZE != 4){ + if (PHP_INT_SIZE !== 4){ // use the dedicated function on 64 Bit systems stream_copy_to_stream($body, $output, $contentLength); } else {