HTTPRequest: Replace getInfo() with new parameter 'content_length'

This commit is contained in:
Philipp 2021-08-20 19:48:20 +02:00
parent 89f718ec72
commit 3c074ab315
No known key found for this signature in database
GPG Key ID: 9A28B7D4FF5667BD
7 changed files with 14 additions and 25 deletions

View File

@ -319,12 +319,6 @@ class CurlResult implements IHTTPResult
return $this->body; return $this->body;
} }
/** {@inheritDoc} */
public function getInfo()
{
return $this->info;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isRedirectUrl() public function isRedirectUrl()
{ {

View File

@ -121,11 +121,6 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
return $this->url; return $this->url;
} }
public function getInfo()
{
// TODO: Implement getInfo() method.
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function isRedirectUrl() public function isRedirectUrl()
{ {

View File

@ -30,6 +30,7 @@ use Friendica\Util\Network;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
@ -204,6 +205,13 @@ class HTTPRequest implements IHTTPRequest
$this->logger->notice('Curl redirect.', ['url' => $request->getUri(), 'to' => $uri]); $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([ $client = new Client([
'allow_redirect' => [ 'allow_redirect' => [
'max' => 8, 'max' => 8,
@ -220,8 +228,9 @@ class HTTPRequest implements IHTTPRequest
try { try {
$response = $client->get($url); $response = $client->get($url);
return new GuzzleResponse($response, $url); return new GuzzleResponse($response, $url);
} catch (RequestException $exception) { } catch (TransferException $exception) {
if ($exception->hasResponse()) { if ($exception instanceof RequestException &&
$exception->hasResponse()) {
return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), $exception->getMessage()); return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), $exception->getMessage());
} else { } else {
return new CurlResult($url, '', ['http_code' => $exception->getCode()], $exception->getCode(), $exception->getMessage()); return new CurlResult($url, '', ['http_code' => $exception->getCode()], $exception->getCode(), $exception->getMessage());

View File

@ -79,6 +79,7 @@ interface IHTTPRequest
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file
* 'header' => header array * 'header' => header array
* 'content_length' => int maximum File content length
* *
* @return IHTTPResult * @return IHTTPResult
*/ */

View File

@ -82,11 +82,6 @@ interface IHTTPResult
*/ */
public function getBody(); public function getBody();
/**
* @return array
*/
public function getInfo();
/** /**
* @return boolean * @return boolean
*/ */

View File

@ -424,16 +424,11 @@ class Probe
*/ */
private static function getHideStatus($url) private static function getHideStatus($url)
{ {
$curlResult = DI::httpRequest()->get($url); $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return false; 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 it isn't a HTML file then exit
if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) { if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
return false; return false;

View File

@ -213,7 +213,7 @@ class ParseUrl
return $siteinfo; return $siteinfo;
} }
$curlResult = DI::httpRequest()->get($url); $curlResult = DI::httpRequest()->get($url, false, ['content_length' => 1000000]);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return $siteinfo; return $siteinfo;
} }