From 4d45cc64499de2de6f758ff7c6f33d1fdd06f73e Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 19 Dec 2023 01:08:06 +0100 Subject: add PhpServer --- src/Server/RequestHandlers/PhpServer.php | 45 ++++++++++++++++++++++++++++++++ test/server.php | 1 + test/test.php | 4 +++ 3 files changed, 50 insertions(+) create mode 100644 src/Server/RequestHandlers/PhpServer.php create mode 100644 test/test.php diff --git a/src/Server/RequestHandlers/PhpServer.php b/src/Server/RequestHandlers/PhpServer.php new file mode 100644 index 0000000..5447f72 --- /dev/null +++ b/src/Server/RequestHandlers/PhpServer.php @@ -0,0 +1,45 @@ +getPath()), '/'); + $filePath = $this->documentRoot . $requestPath; + + $documentPath = $filePath; + if (! is_file($documentPath)) { + $documentPath = $filePath . '/' . $this->indexFile; + } + + if (is_file($documentPath) && str_ends_with($documentPath, '.php')) { + ob_start(); + try { + include $documentPath; + } catch (\Throwable $exception) { + echo <<setBody($content); + $response->setStatusCode(Status::SUCCESS); + $response->setMeta('text/gemini; charset=utf-8'); + } else { + return parent::__invoke($response, $request); + } + + return $response; + } +} diff --git a/test/server.php b/test/server.php index adc9d05..2268d14 100644 --- a/test/server.php +++ b/test/server.php @@ -11,6 +11,7 @@ $server = new Server( 'key' => __DIR__ . '/key.rsa', 'passphrase' => '', ], + '0.0.0.0' ); $server->onRequest(new DocumentServer( diff --git a/test/test.php b/test/test.php new file mode 100644 index 0000000..d1bfe19 --- /dev/null +++ b/test/test.php @@ -0,0 +1,4 @@ +