X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FHTTPRequest.php;h=6442295273857e7af78df608b614f9e97e30fa22;hb=335cd76baf1cf4352e56f779c898118eb9f36ff1;hp=87177b1a4081fbb5a6d85a11a87d91463e5bdfb0;hpb=74bc3de4723af3f2bf61705edba090caa102d171;p=friendica.git diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 87177b1a40..6442295273 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -59,7 +59,7 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function get(string $url, bool $binary = false, array $opts = [], int &$redirects = 0) + public function get(string $url, array $opts = [], int &$redirects = 0) { $stamp1 = microtime(true); @@ -137,6 +137,8 @@ class HTTPRequest implements IHTTPRequest @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']); } + @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + if (!empty($opts['timeout'])) { @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']); } else { @@ -170,12 +172,7 @@ class HTTPRequest implements IHTTPRequest curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); } - if ($binary) { - @curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); - } - - // don't let curl abort the entire application - // if it throws any errors. + $logger = $this->logger; $s = @curl_exec($ch); $curl_info = @curl_getinfo($ch); @@ -190,11 +187,11 @@ class HTTPRequest implements IHTTPRequest $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch)); - if ($curlResponse->isRedirectUrl()) { + if (!Network::isRedirectBlocked($url) && $curlResponse->isRedirectUrl()) { $redirects++; $this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]); @curl_close($ch); - return $this->get($curlResponse->getRedirectUrl(), $binary, $opts, $redirects); + return $this->get($curlResponse->getRedirectUrl(), $opts, $redirects); } @curl_close($ch); @@ -238,6 +235,8 @@ class HTTPRequest implements IHTTPRequest curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); } + @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + if (intval($timeout)) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } else { @@ -276,7 +275,7 @@ class HTTPRequest implements IHTTPRequest $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch)); - if ($curlResponse->isRedirectUrl()) { + if (!Network::isRedirectBlocked($url) && $curlResponse->isRedirectUrl()) { $redirects++; $this->logger->info('Post redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]); curl_close($ch); @@ -312,6 +311,16 @@ class HTTPRequest implements IHTTPRequest */ public function finalUrl(string $url, int $depth = 1, bool $fetchbody = false) { + if (Network::isUrlBlocked($url)) { + $this->logger->info('Domain is blocked.', ['url' => $url]); + return $url; + } + + if (Network::isRedirectBlocked($url)) { + $this->logger->info('Domain should not be redirected.', ['url' => $url]); + return $url; + } + $url = Network::stripTrackingQueryParams($url); if ($depth > 10) { @@ -326,6 +335,7 @@ class HTTPRequest implements IHTTPRequest curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent()); @@ -370,6 +380,7 @@ class HTTPRequest implements IHTTPRequest curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent()); @@ -419,9 +430,9 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function fetch(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) + public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) { - $ret = $this->fetchFull($url, $binary, $timeout, $accept_content, $cookiejar, $redirects); + $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar, $redirects); return $ret->getBody(); } @@ -433,11 +444,10 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function fetchFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) + public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) { return $this->get( $url, - $binary, [ 'timeout' => $timeout, 'accept_content' => $accept_content,