Subscription enumeration, tweaks

This commit is contained in:
2024-03-11 14:39:58 +01:00
parent 2747b59abc
commit 8cbd12ee61
6 changed files with 76 additions and 23 deletions

View File

@ -13,8 +13,6 @@ use React\Promise\PromiseInterface;
class ApiHandler
{
public static string $indexPage;
public function __construct(
private Configuration $config,
private TopicManager $topicManager
@ -36,12 +34,32 @@ class ApiHandler
$path = $request->getUri()->getPath();
// FIXME remove this when done debugging
if ($path === "/index.html") {
$resolve(Response::html(self::$indexPage));
$resolve(Response::html(
<<<ENDHTML
<html>
<head>
</head>
<body>
<script type="text/javascript">
const events = new EventSource("http://127.0.0.1:9000/.well-known/mercure?topic=https://example.com/books/1");
events.onmessage = function (msg) {
console.log(msg);
const message = document.createElement('div');
message.innerText = msg.data;
document.getElementById('messages').appendChild(message);
};
</script>
<div id="messages">
</body>
</html>
ENDHTML
));
}
switch (true) {
case preg_match('<^/.well-known/mercure/subscriptions(/.+?)$>', $path, $m):
case preg_match('<^/.well-known/mercure/subscriptions(/.+?){0,1}$>', $path, $m):
$query = explode("/", trim($m[1]??null, "/"));
$topic = array_shift($query);
$subscription = array_shift($query);
@ -77,14 +95,14 @@ class ApiHandler
{
// TODO implement once we can enumerate topics and subscriptions
// mock data
$subscriptions = $this->topicManager->getSubscriptions();
$lastEventId = "urn:uuid:5e94c686-2c0b-4f9b-958c-92ccc3bbb4eb";
$data = [
"@context" => "https://mercure.rocks/",
"id" => "/.well-known/mercure/subscriptions",
"type" => "Subscriptions",
"lastEventID" => $lastEventId,
"subscriptions" => []
"subscriptions" => $subscriptions
];
return Response::json($data)
@ -94,17 +112,3 @@ class ApiHandler
}
ApiHandler::$indexPage = <<<ENDHTML
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' http: 'unsafe-eval' 'unsafe-inline'; style-src 'self';">
</head>
<body>
<script type="text/javascript">
const events = new EventSource("http://127.0.0.1:9000/.well-known/mercure?topic=https://example.com/books/1");
events.onmessage = msg => console.log(msg);
</script>
</body>
</html>
ENDHTML;

View File

@ -30,8 +30,8 @@ class Server
private SplObjectStorage $webSocketClients;
private TopicManager $topicManager;
private Logger|LoggerInterface $logger;
private Logger $logger;
private ResponseMiddleware $responseMiddleware;
private SecurityMiddleware $securityMiddleware;
@ -73,7 +73,7 @@ class Server
$this->server->listen($socket);
}
private function createLogger(): LoggerInterface
private function createLogger(): Logger
{
$handlers = [
new StreamHandler(STDOUT)