Skip to content

Commit

Permalink
http and server structure + error handler
Browse files Browse the repository at this point in the history
* http and server structures
* add error handler
  • Loading branch information
mattvb91 authored Aug 3, 2022
1 parent 1323eb4 commit f2e2585
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 11 deletions.
49 changes: 44 additions & 5 deletions src/Config/Apps/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
class Http implements App
{
use IterableProps;


private int $_httpPort;

private int $_httpsPort;

private int $_gracePeriod;

/** @var Server[] */
private array $_servers = [];

Expand All @@ -20,16 +26,49 @@ public function addServer(string $key, Server $server): static
return $this;
}

public function setHttpPort(int $httpPort): static
{
$this->_httpPort = $httpPort;

return $this;
}

public function setHttpsPort(int $httpsPort): static
{
$this->_httpsPort = $httpsPort;

return $this;
}

public function setGracePeriod(int $gracePeriod): static
{
$this->_gracePeriod = $gracePeriod;
return $this;
}

public function toArray(): array
{
$servers = [];
$config = [];

if (isset($this->_httpPort)) {
$config['http_port'] = $this->_httpPort;
}

if (isset($this->_httpsPort)) {
$config['https_port'] = $this->_httpsPort;
}

if (isset($this->_gracePeriod)) {
$config['grace_period'] = $this->_gracePeriod;
}

$servers = [];
array_map(static function (Server $server, string $key) use (&$servers) {
$servers[$key] = $server->toArray();
}, $this->_servers, array_keys($this->_servers));

return [
'servers' => $servers,
];
$config['servers'] = $servers;

return $config;
}
}
114 changes: 113 additions & 1 deletion src/Config/Apps/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ class Server implements Arrayable
/** @var Route[] */
private array $_routes = [];

private int $_readTimeout;

private int $_readHeaderTimeout;

private int $_writeTimeout;

private int $_idleTimeout;

private int $_maxHeaderBytes;

private bool $_strictSniHost;

private bool $_experimentalHttp3;

private bool $_allowH2c;

public function setListen(array $listen): static
{
Expand All @@ -33,13 +48,110 @@ public function addRoute(Route $route): static
return $this;
}

public function setRoutes(array $routes): static
{
$this->_routes = $routes;

return $this;
}

public function setReadTimeout(int $readTimeout): static
{
$this->_readTimeout = $readTimeout;

return $this;
}

public function setReadHeaderTimeout(int $readHeaderTimeout): static
{
$this->_readHeaderTimeout = $readHeaderTimeout;

return $this;
}

public function setWriteTimeout(int $writeTimeout): static
{
$this->_writeTimeout = $writeTimeout;

return $this;
}

public function setIdleTimeout(int $idleTimeout): static
{
$this->_idleTimeout = $idleTimeout;

return $this;
}

public function setMaxHeaderBytes(int $maxHeaderBytes): static
{
$this->_maxHeaderBytes = $maxHeaderBytes;

return $this;
}

public function setStrictSniHost(bool $strictSniHost): static
{
$this->_strictSniHost = $strictSniHost;

return $this;
}

public function setExperimentalHttp3(bool $experimentalHttp3): static
{
$this->_experimentalHttp3 = $experimentalHttp3;

return $this;
}

public function setAllowH2c(bool $allowH2c): static
{
$this->_allowH2c = $allowH2c;

return $this;
}

public function toArray(): array
{
return [
$config = [
'listen' => $this->_listen,
'routes' => [...array_map(static function (Route $route) {
return $route->toArray();
}, $this->_routes)],
];

if (isset($this->_readTimeout)) {
$config['read_timeout'] = $this->_readTimeout;
}

if (isset($this->_readHeaderTimeout)) {
$config['read_header_timeout'] = $this->_readHeaderTimeout;
}

if (isset($this->_writeTimeout)) {
$config['write_timeout'] = $this->_writeTimeout;
}

if (isset($this->_idleTimeout)) {
$config['idle_timeout'] = $this->_idleTimeout;
}

if (isset($this->_maxHeaderBytes)) {
$config['max_header_bytes'] = $this->_maxHeaderBytes;
}

if (isset($this->_strictSniHost)) {
$config['strict_sni_host'] = $this->_strictSniHost;
}

if (isset($this->_experimentalHttp3)) {
$config['experimental_http3'] = $this->_experimentalHttp3;
}

if (isset($this->_allowH2c)) {
$config['allow_h2c'] = $this->_allowH2c;
}

return $config;
}
}
49 changes: 49 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle;

use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface;

class Error implements HandlerInterface
{
private string $_error;

private string|int $_statusCode;

public function setError(string $error): static
{
$this->_error = $error;

return $this;
}

public function setStatusCode(int|string $statusCode): static
{
$this->_statusCode = $statusCode;

return $this;
}


public function toArray(): array
{
$config = [
'handler' => $this->getHandler()
];

if(isset($this->_error)) {
$config['error'] = $this->_error;
}

if(isset($this->_statusCode)) {
$config['status_code'] = $this->_statusCode;
}

return $config;
}

public function getHandler(): string
{
return 'error';
}
}
23 changes: 23 additions & 0 deletions tests/Integration/Apps/Http/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Integration\Apps\Http;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use mattvb91\CaddyPhp\Caddy;
use mattvb91\CaddyPhp\Config\Apps\Http;
use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication;
use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication\Providers\HttpBasic\Account;
use Tests\TestCase;
Expand Down Expand Up @@ -44,4 +46,25 @@ public function test_auth_handler()
],
], $handler->toArray());
}

/**
* @coversNothing
*/
public function test_error_handler()
{
$caddy = new Caddy();
$caddy->addApp((new Http())
->addServer('errorServer', (new Http\Server())
->addRoute((new Http\Server\Route())
->addHandle((new Http\Server\Routes\Handle\Error())
->setError("this is an error")
->setStatusCode(501)
)
)
)
);

$this->assertCaddyConfigLoaded($caddy);
}

}
23 changes: 23 additions & 0 deletions tests/Integration/Apps/Http/ServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Integration\Apps\Http;

use mattvb91\CaddyPhp\Caddy;
use mattvb91\CaddyPhp\Config\Apps\Http;
use Tests\TestCase;

class ServerTest extends TestCase
{
/**
* @coversNothing
*/
public function test_server()
{
$server = $this->getServerForTest();

$caddy = (new Caddy())
->addApp((new Http())->addServer('test', $server));

$this->assertCaddyConfigLoaded($caddy);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/CaddyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function test_http_basic_auth()
// $caddy->addApp(
// (new Http())->addServer(
// 'server1', (new Http\Server())->addRoute(
// (new Route())->addHandle(
// (new Routes())->addHandle(
// new StaticResponse('phpunit', 200)
// )
// ))
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use mattvb91\CaddyPhp\Caddy;
use mattvb91\CaddyPhp\Config\Apps\Http\Server;

class TestCase extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -43,4 +44,18 @@ public function assertCaddyConfigLoaded(Caddy $caddy): void
$this->fail('Couldnt verify the config has been loaded');
}
}

public function getServerForTest(): Server
{
return (new Server())
->setListen([':80'])
->setReadTimeout(1)
->setReadHeaderTimeout(2)
->setWriteTimeout(3)
->setIdleTimeout(4)
->setMaxHeaderBytes(5)
->setStrictSniHost(true)
->setExperimentalHttp3(true)
->setAllowH2c(true);
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Apps/Http/Server/Routes/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Unit\Apps\Http\Server\Routes;

use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Error;
use Tests\TestCase;

class HandlerTest extends TestCase
{
/**
* @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Error::getHandler
* @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Error::toArray
* @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Error::setError
* @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Error::setStatusCode
*/
public function test_error_hander()
{
$error = (new Error())
->setStatusCode(501)
->setError('this is an error');

$this->assertEquals([
'handler' => 'error',
'status_code' => 501,
'error' => 'this is an error'
], $error->toArray());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit\Apps;
namespace Tests\Unit\Apps\Http\Server\Routes;

use mattvb91\CaddyPhp\Config\Apps\Http\Server\Route;
use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\StaticResponse;
Expand Down
Loading

0 comments on commit f2e2585

Please sign in to comment.