diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-11-12 11:16:56 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-11-20 17:30:04 +0100 |
commit | 6df3d321d9b67c4541f50158b087d37c4b22e886 (patch) | |
tree | 06031988bb4b957b76f051af366ced448a48545f /src/Server/RequestHandlers |
initial commit
Diffstat (limited to 'src/Server/RequestHandlers')
-rw-r--r-- | src/Server/RequestHandlers/DocumentServer.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Server/RequestHandlers/DocumentServer.php b/src/Server/RequestHandlers/DocumentServer.php new file mode 100644 index 0000000..9689d5b --- /dev/null +++ b/src/Server/RequestHandlers/DocumentServer.php @@ -0,0 +1,39 @@ +<?php + +namespace GeminiFoundation\Server\RequestHandlers; + +use GeminiFoundation\Request; +use GeminiFoundation\RequestHandlerInterface; +use GeminiFoundation\Response; +use GeminiFoundation\Status; +use function GeminiFoundation\mime_content_type; + +class DocumentServer implements RequestHandlerInterface +{ + protected string $documentRoot; + + public function __construct(string $documentRoot = '') + { + $this->documentRoot = $documentRoot ?: getcwd(); + } + + public function __invoke(Response $response, Request $request): Response + { + $documentPath = $this->documentRoot . $request->getPath(); + if (! is_file($documentPath)) { + $documentPath = $this->documentRoot . $request->getPath() . '/index.gmi'; + } + + if (is_file($documentPath)) { + $content = file_get_contents($documentPath); + $response->setBody($content); + + $response->setStatusCode(Status::SUCCESS); + $response->setMeta(mime_content_type($documentPath) . '; charset=utf-8'); + } else { + $response->setStatusCode(Status::NOT_FOUND); + } + + return $response; + } +} |