diff --git a/VERSION b/VERSION index 701ed6b..fb1318b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2410.42339 +0.2410.51358 diff --git a/src/Http/HttpRequest.php b/src/Http/HttpRequest.php index 5d9a1c4..deaf681 100644 --- a/src/Http/HttpRequest.php +++ b/src/Http/HttpRequest.php @@ -1,7 +1,7 @@ remoteAddr; + } + + /** + * Returns whether the request was made over HTTPS or not. + * + * @return bool true if HTTPS, false if not. + */ + public function isSecure(): bool { + return $this->secure; + } + /** * Retrieves the HTTP request method. * @@ -136,6 +158,8 @@ class HttpRequest extends HttpMessage { */ public static function fromRequest(): HttpRequest { $build = new HttpRequestBuilder; + $build->setRemoteAddress((string)filter_input(INPUT_SERVER, 'REMOTE_ADDR')); + $build->setSecure(filter_has_var(INPUT_SERVER, 'HTTPS')); $build->setHttpVersion((string)filter_input(INPUT_SERVER, 'SERVER_PROTOCOL')); $build->setMethod((string)filter_input(INPUT_SERVER, 'REQUEST_METHOD')); diff --git a/src/Http/HttpRequestBuilder.php b/src/Http/HttpRequestBuilder.php index 3974870..5aec39e 100644 --- a/src/Http/HttpRequestBuilder.php +++ b/src/Http/HttpRequestBuilder.php @@ -1,14 +1,18 @@ */ private array $cookies = []; + /** + * Returns the origin remote address. + * + * @return string Printable remote address. + */ + protected function getRemoteAddress(): string { + return $this->remoteAddr; + } + + /** + * Sets the remote address from request originated. + * + * @param string $remoteAddr Remote address. + */ + public function setRemoteAddress(string $remoteAddr): void { + if(filter_var($remoteAddr, FILTER_VALIDATE_IP) === false) + throw new InvalidArgumentException('$remoteAddr must be a valid remote address'); + + $this->remoteAddr = $remoteAddr; + } + + /** + * Returns whether the request was made over HTTPS or not. + * + * @return bool true if HTTPS, false if not. + */ + protected function isSecure(): bool { + return $this->secure; + } + + /** + * Sets whether the request was made over HTTPS. + * + * @param bool $secure true if HTTPS. + */ + public function setSecure(bool $secure): void { + $this->secure = $secure; + } + /** * Returns HTTP request method. * @@ -135,6 +178,8 @@ class HttpRequestBuilder extends HttpMessageBuilder { */ public function toRequest(): HttpRequest { return new HttpRequest( + $this->getRemoteAddress(), + $this->isSecure(), $this->getHttpVersion(), $this->getMethod(), $this->getPath(), diff --git a/tests/RouterTest.php b/tests/RouterTest.php index f6c7fdb..e746862 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -1,7 +1,7 @@ get('/test', fn() => 'also unexpected'); ob_start(); - $router->dispatch(new HttpRequest('1.1', 'GET', '/', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); ob_start(); - $router->dispatch(new HttpRequest('1.1', 'GET', '/test', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/test', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); ob_start(); - $router->dispatch(new HttpRequest('1.1', 'GET', '/error', [], [], new HttpHeaders([]), null)); + $router->dispatch(new HttpRequest('::1', true, '1.1', 'GET', '/error', [], [], new HttpHeaders([]), null)); $this->assertEquals('expected', ob_get_clean()); } }