]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Request.php
Apply suggestions from code review
[friendica.git] / src / App / Request.php
index 43cabba856e513e76b39c0c4f7df14e491251e7a..d61303d5d729f410c8776aec4d534f04e53629b1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -22,6 +22,7 @@
 namespace Friendica\App;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\System;
 
 /**
  * Container for the whole request
@@ -32,11 +33,23 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
  */
 class Request
 {
-       /** @var string the default possible headers, which could contain the client IP */
-       const ORDERED_FORWARD_FOR_HEADER = 'HTTP_X_FORWARDED_FOR';
+       /**
+        * A comma separated list of default headers that could contain the client IP in a proxy request
+        *
+        * @var string
+        */
+       const DEFAULT_FORWARD_FOR_HEADER = 'HTTP_X_FORWARDED_FOR';
+       /**
+        * The default Request-ID header to retrieve the current transaction ID from the HTTP header (if set)
+        *
+        * @var string
+        */
+       const DEFAULT_REQUEST_ID_HEADER = 'HTTP_X_REQUEST_ID';
 
        /** @var string The remote IP address of the current request */
        protected $remoteAddress;
+       /** @var string The request-id of the current request */
+       protected $requestId;
 
        /**
         * @return string The remote IP address of the current request
@@ -48,9 +61,20 @@ class Request
                return $this->remoteAddress;
        }
 
+       /**
+        * @return string The request ID of the current request
+        *
+        * Do always use this instead of $_SERVER['X_REQUEST_ID']
+        */
+       public function getRequestId(): string
+       {
+               return $this->requestId;
+       }
+
        public function __construct(IManageConfigValues $config, array $server = [])
        {
                $this->remoteAddress = $this->determineRemoteAddress($config, $server);
+               $this->requestId = $server[static::DEFAULT_REQUEST_ID_HEADER] ?? System::createGUID(8, false);
        }
 
        /**
@@ -108,7 +132,7 @@ class Request
         * specified in this header will be returned instead.
         *
         * @param IManageConfigValues $config
-        * @param array               $server
+        * @param array               $server The $_SERVER array
         *
         * @return string
         */
@@ -118,7 +142,7 @@ class Request
                $trustedProxies = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $config->get('proxy', 'trusted_proxies', ''));
 
                if (\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) {
-                       $forwardedForHeaders = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $config->get('proxy', 'forwarded_for_headers')) ?? static::ORDERED_FORWARD_FOR_HEADER;
+                       $forwardedForHeaders = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $config->get('proxy', 'forwarded_for_headers', static::DEFAULT_FORWARD_FOR_HEADER));
 
                        foreach ($forwardedForHeaders as $header) {
                                if (isset($server[$header])) {