From 511d99598145c1931f748f49f16f94a7e944b706 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Thu, 28 Oct 2021 06:51:01 -0500 Subject: [PATCH] added 405 response The site only allows GET requests. --- public/index.php | 33 ++++++++++++++++++++++++----- setup-errors/405.md | 12 +++++++++++ {500-errors => setup-errors}/500.md | 0 {500-errors => setup-errors}/502.md | 0 src/Server.php | 14 ++++++++++++ tests/ServerTest.php | 13 ++++++++++++ 6 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 setup-errors/405.md rename {500-errors => setup-errors}/500.md (100%) rename {500-errors => setup-errors}/502.md (100%) diff --git a/public/index.php b/public/index.php index 88e5dd88..9e20c722 100644 --- a/public/index.php +++ b/public/index.php @@ -22,7 +22,7 @@ $server = JoshBruce\Site\Server::init($_SERVER); if ($server->isMissingRequiredValues()) { - $content = JoshBruce\Site\Content::init($projectRoot, 0, '/500-errors') + $content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors') ->for('/500.md'); JoshBruce\Site\Emitter::emitWithResponse( @@ -43,11 +43,34 @@ } /** - * Verifying specified content area exists. + * Verifying the method is supported by the app */ +if ($server->isUsingUnsupportedMethod()) { + $content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors') + ->for('/405.md'); -// TESTING -// $_SERVER['CONTENT_FOLDER'] = '/does/not/exist'; + JoshBruce\Site\Emitter::emitWithResponse( + 405, + [ + 'Cache-Control' => [ + 'no-cache', + 'must-revalidate' + ], + 'Allow' => $server->supportedMethods() + ], + Eightfold\HTMLBuilder\Document::create( + $markdownConverter->getFrontMatter($content->markdown())['title'] + )->body( + $markdownConverter->convert($content->markdown()) + )->build() + ); + exit; +} + + +/** + * Verifying specified content area exists. + */ $content = JoshBruce\Site\Content::init( $projectRoot, $server->contentUp(), @@ -55,7 +78,7 @@ ); if ($content->folderIsMissing()) { - $content = JoshBruce\Site\Content::init($projectRoot, 0, '/500-errors') + $content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors') ->for('/502.md'); JoshBruce\Site\Emitter::emitWithResponse( diff --git a/setup-errors/405.md b/setup-errors/405.md new file mode 100644 index 00000000..94b07c38 --- /dev/null +++ b/setup-errors/405.md @@ -0,0 +1,12 @@ +--- +title: Unsupported method +usage: The request is using an Unsupported method. +--- + +# 405: Unsupported method + +We cannot support that type of request. + +Please try again later. + +If this error persists, please contact [Josh Bruce](https://github.com/joshbruce). diff --git a/500-errors/500.md b/setup-errors/500.md similarity index 100% rename from 500-errors/500.md rename to setup-errors/500.md diff --git a/500-errors/502.md b/setup-errors/502.md similarity index 100% rename from 500-errors/502.md rename to setup-errors/502.md diff --git a/src/Server.php b/src/Server.php index b0a456bc..c7ebe4c3 100644 --- a/src/Server.php +++ b/src/Server.php @@ -51,6 +51,20 @@ public function isMissingRequiredValues(): bool return false; } + public function isUsingUnsupportedMethod(): bool + { + $requestMethod = $this->serverGlobals['REQUEST_METHOD']; + return ! in_array($requestMethod, $this->supportedMethods()); + } + + /** + * @return string[] [description] + */ + public function supportedMethods(): array + { + return ['GET']; + } + public function contentUp(): int { return intval($this->serverGlobals['CONTENT_UP']); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index a088e784..16b75e97 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -2,6 +2,19 @@ use JoshBruce\Site\Server; +it('limits request methods', function() { + expect( + Server::init(serverGlobals())->isUsingUnsupportedMethod() + )->toBeFalse(); + + $serverGlobals = serverGlobals(); + $serverGlobals['REQUEST_METHOD'] = 'INVALID'; + + expect( + Server::init($serverGlobals)->isUsingUnsupportedMethod() + )->toBeTrue(); +})->group('server'); + it('has required variables', function() { expect( Server::init(serverGlobals())->isMissingRequiredValues()