]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/HTTPRequest.php
Merge branch '2021.03-rc' into copyright-2021
[friendica.git] / src / Network / HTTPRequest.php
index 839586880ca1b9782ddc8090b0b5a2d712d1d352..74cf9cd0554d01a62beb0a1486b391bb734f4f69 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU APGL version 3 or any later version
  *
@@ -52,6 +52,17 @@ class HTTPRequest implements IHTTPRequest
                $this->baseUrl  = $baseUrl->get();
        }
 
+       /** {@inheritDoc}
+        *
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function head(string $url, array $opts = [])
+       {
+               $opts['nobody'] = true;
+
+               return $this->get($url, $opts);
+       }
+
        /**
         * {@inheritDoc}
         *
@@ -59,12 +70,13 @@ 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);
 
                if (strlen($url) > 1000) {
                        $this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return CurlResult::createErrorCurl(substr($url, 0, 200));
                }
 
@@ -83,12 +95,14 @@ class HTTPRequest implements IHTTPRequest
 
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.', ['url' => $url]);
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return CurlResult::createErrorCurl($url);
                }
 
                $ch = @curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return CurlResult::createErrorCurl($url);
                }
 
@@ -130,6 +144,7 @@ class HTTPRequest implements IHTTPRequest
                curl_setopt($ch, CURLOPT_ENCODING, '');
 
                if (!empty($opts['headers'])) {
+                       $this->logger->notice('Wrong option \'headers\' used.');
                        @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
                }
 
@@ -137,6 +152,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,13 +187,6 @@ 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.
-
                $s         = @curl_exec($ch);
                $curl_info = @curl_getinfo($ch);
 
@@ -190,16 +200,17 @@ 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);
+                       $this->profiler->saveTimestamp($stamp1, 'network');
+                       return $this->get($curlResponse->getRedirectUrl(), $opts, $redirects);
                }
 
                @curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, 'network', System::callstack());
+               $this->profiler->saveTimestamp($stamp1, 'network');
 
                return $curlResponse;
        }
@@ -217,12 +228,14 @@ class HTTPRequest implements IHTTPRequest
 
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.' . ['url' => $url]);
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return CurlResult::createErrorCurl($url);
                }
 
                $ch = curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return CurlResult::createErrorCurl($url);
                }
 
@@ -238,6 +251,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,16 +291,17 @@ 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);
+                       $this->profiler->saveTimestamp($stamp1, 'network');
                        return $this->post($curlResponse->getRedirectUrl(), $params, $headers, $redirects, $timeout);
                }
 
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, 'network', System::callstack());
+               $this->profiler->saveTimestamp($stamp1, 'network');
 
                // Very old versions of Lighttpd don't like the "Expect" header, so we remove it when needed
                if ($curlResponse->getReturnCode() == 417) {
@@ -312,6 +328,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 +352,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());
@@ -335,7 +362,7 @@ class HTTPRequest implements IHTTPRequest
                $http_code = $curl_info['http_code'];
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, "network", System::callstack());
+               $this->profiler->saveTimestamp($stamp1, "network");
 
                if ($http_code == 0) {
                        return $url;
@@ -370,6 +397,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());
@@ -377,7 +405,7 @@ class HTTPRequest implements IHTTPRequest
                $body = curl_exec($ch);
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, "network", System::callstack());
+               $this->profiler->saveTimestamp($stamp1, "network");
 
                if (trim($body) == "") {
                        return $url;
@@ -419,9 +447,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 +461,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,