blob: 9689d5b3767a18888fd7718b35854777383a22a0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}
}
|