]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/HTTPRequest.php
Merge pull request #10551 from annando/profiler
[friendica.git] / src / Network / HTTPRequest.php
index a0d940414e0a7c331be5cd63d08c7f0fa5c5ee4c..daf84dc9acf528b0d556d1bed8dade40dcb0f237 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
  *
@@ -70,12 +70,17 @@ class HTTPRequest implements IHTTPRequest
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function get(string $url, array $opts = [], int &$redirects = 0)
+       public function get(string $url, array $opts = [], &$redirects = 0)
        {
-               $stamp1 = microtime(true);
+               $this->profiler->startRecording('network');
+
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
 
                if (strlen($url) > 1000) {
                        $this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
+                       $this->profiler->stopRecording();
                        return CurlResult::createErrorCurl(substr($url, 0, 200));
                }
 
@@ -94,12 +99,14 @@ class HTTPRequest implements IHTTPRequest
 
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.', ['url' => $url]);
+                       $this->profiler->stopRecording();
                        return CurlResult::createErrorCurl($url);
                }
 
                $ch = @curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
+                       $this->profiler->stopRecording();
                        return CurlResult::createErrorCurl($url);
                }
 
@@ -201,12 +208,13 @@ class HTTPRequest implements IHTTPRequest
                        $redirects++;
                        $this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]);
                        @curl_close($ch);
+                       $this->profiler->stopRecording();
                        return $this->get($curlResponse->getRedirectUrl(), $opts, $redirects);
                }
 
                @curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, 'network');
+               $this->profiler->stopRecording();
 
                return $curlResponse;
        }
@@ -218,18 +226,24 @@ class HTTPRequest implements IHTTPRequest
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function post(string $url, $params, array $headers = [], int $timeout = 0, int &$redirects = 0)
+       public function post(string $url, $params, array $headers = [], int $timeout = 0, &$redirects = 0)
        {
-               $stamp1 = microtime(true);
+               $this->profiler->startRecording('network');
+
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
 
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.' . ['url' => $url]);
+                       $this->profiler->stopRecording();
                        return CurlResult::createErrorCurl($url);
                }
 
                $ch = curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
+                       $this->profiler->stopRecording();
                        return CurlResult::createErrorCurl($url);
                }
 
@@ -289,12 +303,13 @@ class HTTPRequest implements IHTTPRequest
                        $redirects++;
                        $this->logger->info('Post redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]);
                        curl_close($ch);
+                       $this->profiler->stopRecording();
                        return $this->post($curlResponse->getRedirectUrl(), $params, $headers, $redirects, $timeout);
                }
 
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, 'network');
+               $this->profiler->stopRecording();
 
                // Very old versions of Lighttpd don't like the "Expect" header, so we remove it when needed
                if ($curlResponse->getReturnCode() == 417) {
@@ -321,6 +336,10 @@ class HTTPRequest implements IHTTPRequest
         */
        public function finalUrl(string $url, int $depth = 1, bool $fetchbody = false)
        {
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
+
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.', ['url' => $url]);
                        return $url;
@@ -339,7 +358,7 @@ class HTTPRequest implements IHTTPRequest
 
                $url = trim($url, "'");
 
-               $stamp1 = microtime(true);
+               $this->profiler->startRecording('network');
 
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
@@ -355,7 +374,7 @@ class HTTPRequest implements IHTTPRequest
                $http_code = $curl_info['http_code'];
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, "network");
+               $this->profiler->stopRecording();
 
                if ($http_code == 0) {
                        return $url;
@@ -384,7 +403,7 @@ class HTTPRequest implements IHTTPRequest
                        return $url;
                }
 
-               $stamp1 = microtime(true);
+               $this->profiler->startRecording('network');
 
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
@@ -398,7 +417,7 @@ class HTTPRequest implements IHTTPRequest
                $body = curl_exec($ch);
                curl_close($ch);
 
-               $this->profiler->saveTimestamp($stamp1, "network");
+               $this->profiler->stopRecording();
 
                if (trim($body) == "") {
                        return $url;
@@ -440,7 +459,7 @@ class HTTPRequest implements IHTTPRequest
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
+       public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', &$redirects = 0)
        {
                $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar, $redirects);
 
@@ -454,7 +473,7 @@ class HTTPRequest implements IHTTPRequest
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
+       public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', &$redirects = 0)
        {
                return $this->get(
                        $url,