diff options
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; + } +} |