Expose REMOTE_ADDR and HTTPS in HttpRequest.

This commit is contained in:
flash 2024-10-05 13:58:24 +00:00
parent d3e4d0985a
commit efe7d181d4
4 changed files with 76 additions and 7 deletions

View file

@ -1 +1 @@
0.2410.42339
0.2410.51358

View file

@ -1,7 +1,7 @@
<?php
// HttpRequest.php
// Created: 2022-02-08
// Updated: 2024-10-02
// Updated: 2024-10-05
namespace Index\Http;
@ -15,6 +15,8 @@ use Index\Json\JsonHttpContent;
*/
class HttpRequest extends HttpMessage {
/**
* @param string $remoteAddr Origin remote address.
* @param bool $secure true if HTTPS.
* @param string $version HTTP message version.
* @param string $method HTTP request method.
* @param string $path HTTP request path.
@ -24,6 +26,8 @@ class HttpRequest extends HttpMessage {
* @param ?HttpContent $content Body contents.
*/
public function __construct(
private string $remoteAddr,
private bool $secure,
string $version,
private string $method,
private string $path,
@ -35,6 +39,24 @@ class HttpRequest extends HttpMessage {
parent::__construct($version, $headers, $content);
}
/**
* Returns the origin remote address.
*
* @return string Printable remote address.
*/
public function getRemoteAddress(): string {
return $this->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'));

View file

@ -1,14 +1,18 @@
<?php
// HttpRequestBuilder.php
// Created: 2022-02-08
// Updated: 2024-08-03
// Updated: 2024-10-05
namespace Index\Http;
use InvalidArgumentException;
/**
* Represents a HTTP request message builder.
*/
class HttpRequestBuilder extends HttpMessageBuilder {
private string $remoteAddr = '::';
private bool $secure = false;
private string $method = 'GET';
private string $path = '/';
@ -18,6 +22,45 @@ class HttpRequestBuilder extends HttpMessageBuilder {
/** @var array<string, string> */
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(),

View file

@ -1,7 +1,7 @@
<?php
// RouterTest.php
// Created: 2022-01-20
// Updated: 2024-10-02
// Updated: 2024-10-05
declare(strict_types=1);
@ -174,15 +174,15 @@ final class RouterTest extends TestCase {
$router->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());
}
}