blob: 37a3bd61a9964dae4eb341dd6efae51a0ee66f61 (
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 . urldecode($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;
}
}
|