HTTPRequest: Replace getInfo() with new parameter 'content_length'
authorPhilipp <admin@philipp.info>
Fri, 9 Oct 2020 18:52:52 +0000 (20:52 +0200)
committerPhilipp <admin@philipp.info>
Sat, 10 Oct 2020 21:11:30 +0000 (23:11 +0200)
src/Network/CurlResult.php
src/Network/GuzzleResponse.php
src/Network/HTTPRequest.php
src/Network/IHTTPRequest.php
src/Network/IHTTPResult.php
src/Network/Probe.php
src/Util/ParseUrl.php

index b0e8010..b2eefcb 100644 (file)
@@ -319,12 +319,6 @@ class CurlResult implements IHTTPResult
                return $this->body;
        }
 
-       /** {@inheritDoc} */
-       public function getInfo()
-       {
-               return $this->info;
-       }
-
        /** {@inheritDoc} */
        public function isRedirectUrl()
        {
index c6cab7c..69e88b4 100644 (file)
@@ -121,11 +121,6 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
                return $this->url;
        }
 
-       public function getInfo()
-       {
-               // TODO: Implement getInfo() method.
-       }
-
        /** {@inheritDoc} */
        public function isRedirectUrl()
        {
index e0400b6..3e34c01 100644 (file)
@@ -30,6 +30,7 @@ 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;
@@ -177,10 +178,18 @@ 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,
                                'on_redirect' => $onRedirect,
+                               'on_headers' => $onHeaders,
                                'track_redirect' => true,
                                'strict' => true,
                                'referer' => true,
@@ -191,8 +200,9 @@ class HTTPRequest implements IHTTPRequest
                try {
                        $response = $client->get($url);
                        return new GuzzleResponse($response, $url);
-               } catch (RequestException $exception) {
-                       if ($exception->hasResponse()) {
+               } catch (TransferException $exception) {
+                       if ($exception instanceof RequestException &&
+                               $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());
index 3f9b7f2..4999308 100644 (file)
@@ -75,6 +75,7 @@ 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
         */
index 5904bcf..77ee869 100644 (file)
@@ -82,11 +82,6 @@ interface IHTTPResult
         */
        public function getBody();
 
-       /**
-        * @return array
-        */
-       public function getInfo();
-
        /**
         * @return boolean
         */
index 3fe0352..cfd0368 100644 (file)
@@ -423,16 +423,11 @@ class Probe
         */
        private static function getHideStatus($url)
        {
-               $curlResult = DI::httpRequest()->get($url);
+               $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]);
                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;
index c3cbda7..1a81a25 100644 (file)
@@ -160,16 +160,11 @@ class ParseUrl
                        return $siteinfo;
                }
 
-               $curlResult = DI::httpRequest()->get($url);
+               $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]);
                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;