From 2e8ad098b9917dca48889c6bb36e167596e475a7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 11 Oct 2020 23:25:47 +0200 Subject: [PATCH] Revert "HTTPRequest: Replace getInfo() with new parameter 'content_length'" This reverts commit f3cd973c --- src/Network/CurlResult.php | 6 ++++++ src/Network/GuzzleResponse.php | 5 +++++ src/Network/HTTPRequest.php | 13 ++----------- src/Network/IHTTPRequest.php | 1 - src/Network/IHTTPResult.php | 5 +++++ src/Network/Probe.php | 7 ++++++- src/Util/ParseUrl.php | 7 ++++++- 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index b2eefcbaa9..b0e801010d 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -319,6 +319,12 @@ class CurlResult implements IHTTPResult return $this->body; } + /** {@inheritDoc} */ + public function getInfo() + { + return $this->info; + } + /** {@inheritDoc} */ public function isRedirectUrl() { diff --git a/src/Network/GuzzleResponse.php b/src/Network/GuzzleResponse.php index 69e88b4025..c6cab7c9e8 100644 --- a/src/Network/GuzzleResponse.php +++ b/src/Network/GuzzleResponse.php @@ -121,6 +121,11 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface return $this->url; } + public function getInfo() + { + // TODO: Implement getInfo() method. + } + /** {@inheritDoc} */ public function isRedirectUrl() { diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 1f6b262921..ffc095b66a 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -30,7 +30,6 @@ use Friendica\Util\Network; use Friendica\Util\Profiler; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; -use GuzzleHttp\Exception\TransferException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -187,13 +186,6 @@ class HTTPRequest implements IHTTPRequest $this->logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]); }; - $onHeaders = function (ResponseInterface $response) use ($opts) { - if (!empty($opts['content_length']) && - $response->getHeaderLine('Content-Length') > $opts['content_length']) { - throw new TransferException('The file is too big!'); - } - }; - $client = new Client([ 'allow_redirect' => [ 'max' => 8, @@ -210,9 +202,8 @@ class HTTPRequest implements IHTTPRequest try { $response = $client->get($url); return new GuzzleResponse($response, $url); - } catch (TransferException $exception) { - if ($exception instanceof RequestException && - $exception->hasResponse()) { + } catch (RequestException $exception) { + if ($exception->hasResponse()) { return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), $exception->getMessage()); } else { return new CurlResult($url, '', ['http_code' => $exception->getCode()], $exception->getCode(), $exception->getMessage()); diff --git a/src/Network/IHTTPRequest.php b/src/Network/IHTTPRequest.php index 49993085e6..3f9b7f27a1 100644 --- a/src/Network/IHTTPRequest.php +++ b/src/Network/IHTTPRequest.php @@ -75,7 +75,6 @@ interface IHTTPRequest * 'nobody' => only return the header * 'cookiejar' => path to cookie jar file * 'header' => header array - * 'content_length' => int maximum File content length * * @return IHTTPResult */ diff --git a/src/Network/IHTTPResult.php b/src/Network/IHTTPResult.php index 77ee86976b..5904bcfa38 100644 --- a/src/Network/IHTTPResult.php +++ b/src/Network/IHTTPResult.php @@ -82,6 +82,11 @@ interface IHTTPResult */ public function getBody(); + /** + * @return array + */ + public function getInfo(); + /** * @return boolean */ diff --git a/src/Network/Probe.php b/src/Network/Probe.php index cfd0368439..3fe035286f 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -423,11 +423,16 @@ class Probe */ private static function getHideStatus($url) { - $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]); + $curlResult = DI::httpRequest()->get($url); if (!$curlResult->isSuccess()) { return false; } + // If the file is too large then exit + if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) { + return false; + } + // If it isn't a HTML file then exit if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) { return false; diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 1a81a256ca..c3cbda70e3 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -160,11 +160,16 @@ class ParseUrl return $siteinfo; } - $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]); + $curlResult = DI::httpRequest()->get($url); if (!$curlResult->isSuccess()) { return $siteinfo; } + // If the file is too large then exit + if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) { + return $siteinfo; + } + // If it isn't a HTML file then exit if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) { return $siteinfo;