diff --git a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php index 92c7617..f137504 100644 --- a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php +++ b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php @@ -67,7 +67,7 @@ class BaseCsrfVerifier implements IMiddleware $token = $request->getInputHandler()->value( static::POST_KEY, - $request->getHeader(static::HEADER_KEY) ?? $request->getHeader('HTTP-' . static::HEADER_KEY), + $request->getHeader(static::HEADER_KEY), Request::$requestTypesPost ); diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index bc187c9..8d6dab9 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -4,6 +4,7 @@ namespace Pecee\Http; use Pecee\Http\Exceptions\MalformedUrlException; use Pecee\Http\Input\InputHandler; +use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\RouteUrl; use Pecee\SimpleRouter\SimpleRouter; @@ -110,14 +111,14 @@ class Request { foreach ($_SERVER as $key => $value) { $this->headers[strtolower($key)] = $value; - $this->headers[strtolower(str_replace('_', '-', $key))] = $value; + $this->headers[str_replace('_', '-', strtolower($key))] = $value; } $this->setHost($this->getHeader('http-host')); // Check if special IIS header exist, otherwise use default. $this->setUrl(new Url($this->getHeader('unencoded-url', $this->getHeader('request-uri')))); - + $this->method = strtolower($this->getHeader('request-method')); $this->inputHandler = new InputHandler($this); $this->method = strtolower($this->inputHandler->value('_method', $this->getHeader('request-method'))); @@ -186,7 +187,7 @@ class Request */ public function getCsrfToken(): ?string { - return $this->getHeader('x-csrf-token') ?? $this->getHeader('http-x-csrf-token'); + return $this->getHeader(BaseCsrfVerifier::HEADER_KEY); } /** @@ -204,15 +205,13 @@ class Request */ public function getIp(): ?string { - if ($this->getHeader('http-cf-connecting-ip') !== null) { - return $this->getHeader('http-cf-connecting-ip'); - } - - if ($this->getHeader('http-x-forwarded-for') !== null) { - return $this->getHeader('http-x-forwarded-for'); - } - - return $this->getHeader('remote-addr'); + return $this->getHeader( + 'http-cf-connecting-ip', + $this->getHeader( + 'http-x-forwarded-for', + $this->getHeader('remote-addr') + ) + ); } /** @@ -247,14 +246,28 @@ class Request /** * Get header value by name * - * @param string $name - * @param string|null $defaultValue + * @param string $name Name of the header. + * @param string|null $defaultValue Value to be returned if header is not found. + * @param bool $tryParse When enabled the method will try to find the header from both from client (http) and server-side variants, if the header is not found. * * @return string|null */ - public function getHeader($name, $defaultValue = null): ?string + public function getHeader(string $name, $defaultValue = null, $tryParse = true): ?string { - return $this->headers[strtolower($name)] ?? $defaultValue; + $name = strtolower($name); + $header = $this->headers[$name] ?? null; + + if ($tryParse === true && $header === null) { + if (strpos($name, 'http-') === 0) { + // Trying to find client header variant which was not found, searching for header variant without http- prefix. + $header = $this->headers[str_replace('http-', '', $name)] ?? null; + } else { + // Trying to find server variant which was not found, searching for client variant with http- prefix. + $header = $this->headers['http-' . $name] ?? null; + } + } + + return $header ?? $defaultValue; } /** diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 8b1378e..758204f 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -305,8 +305,8 @@ class Router * Start the routing * * @return string|null - * @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException - * @throws \Pecee\Http\Middleware\Exceptions\TokenMismatchException + * @throws NotFoundHttpException + * @throws TokenMismatchException * @throws HttpException * @throws \Exception */ diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 8fe6118..284078b 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -466,7 +466,7 @@ class SimpleRouter /** * Get the request * - * @return \Pecee\Http\Request + * @return Request */ public static function request(): Request {