diff --git a/src/Http/Middleware/MercureHandler.php b/src/Http/Middleware/MercureHandler.php index 9495b05..2f883ae 100644 --- a/src/Http/Middleware/MercureHandler.php +++ b/src/Http/Middleware/MercureHandler.php @@ -91,7 +91,7 @@ class MercureHandler code: RequestException::ERR_INVALID_REQUEST_DATA ); [ $name, $value ] = array_map('urldecode', explode("=", $param, 2)); - if ($name === 'topic') $topics[] = $value; + if ($name === 'topic' || $name === 'topic[]') $topics[] = $value; } // Grab the JWT token from the requests authorization attribute @@ -142,7 +142,9 @@ class MercureHandler // Parse out the urlencoded body. Pretty sure there is a better way to do this? $body = explode("&", (string)$request->getBody()); - $data = []; + $data = [ + 'topic' => [] + ]; foreach ($body as $param) { if (!str_contains($param, "=")) throw new RequestException( @@ -150,10 +152,8 @@ class MercureHandler code: RequestException::ERR_INVALID_REQUEST_DATA ); [ $name, $value ] = array_map('urldecode', explode("=", $param, 2)); - if (in_array($name, [ 'topic' ])) { - if (!isset($data[$name])) - $data[$name] = []; - $data[$name][] = $value; + if ($name === 'topic' || $name === 'topic[]') { + $data['topic'][] = $value; } else { $data[$name] = $value; } diff --git a/src/Http/Middleware/ResponseMiddleware.php b/src/Http/Middleware/ResponseMiddleware.php index eb5e0a7..ad19d29 100644 --- a/src/Http/Middleware/ResponseMiddleware.php +++ b/src/Http/Middleware/ResponseMiddleware.php @@ -6,6 +6,7 @@ use NoccyLabs\Mercureact\Configuration; use NoccyLabs\Mercureact\Http\Exception\SecurityException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Log\LoggerInterface; use React\Http\Message\Response; use React\Promise\Promise; use React\Promise\PromiseInterface; @@ -15,7 +16,8 @@ class ResponseMiddleware { public function __construct( - private Configuration $config + private Configuration $config, + private LoggerInterface $logger, ) { @@ -60,13 +62,18 @@ class ResponseMiddleware assert("\$response instanceof ResponseInterface"); $host = ($request->getServerParams()['SERVER_ADDR']??""); //. ":" . ($request->getServerParams()['SERVER_PORT']??"80"); - fprintf(STDOUT, "%s %3d %s %s %d\n", + $this->logger->debug(sprintf("%s %s %s → %3d (%d)", $request->getServerParams()['REMOTE_ADDR'], - $response->getStatusCode(), $request->getMethod(), $request->getUri()->getPath(), + $response->getStatusCode(), strlen($response->getBody()) - ); + ), [ + 'remote' => $request->getServerParams()['REMOTE_ADDR'], + 'status' => $response->getStatusCode(), + 'method' => $request->getMethod(), + 'path' => $request->getUri()->getPath(), + ]); return $response // ->withAddedHeader('Link', '; rel="mercure"') // ->withAddedHeader('Link', '; rel="mercure+ws"') diff --git a/src/Http/Server.php b/src/Http/Server.php index db9d61a..377bc39 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -2,6 +2,8 @@ namespace NoccyLabs\Mercureact\Http; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; use NoccyLabs\Mercureact\Broker\TopicManager; use NoccyLabs\Mercureact\Configuration; use NoccyLabs\Mercureact\Http\Middleware\ApiHandler; @@ -10,6 +12,7 @@ use NoccyLabs\Mercureact\Http\Middleware\NotFoundHandler; use NoccyLabs\Mercureact\Http\Middleware\ResponseMiddleware; use NoccyLabs\Mercureact\Http\Middleware\SecurityMiddleware; use NoccyLabs\Mercureact\Http\Middleware\WebSocketHandler; +use Psr\Log\LoggerInterface; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Http\HttpServer; @@ -28,6 +31,8 @@ class Server private TopicManager $topicManager; + private Logger|LoggerInterface $logger; + private ResponseMiddleware $responseMiddleware; private SecurityMiddleware $securityMiddleware; private WebSocketHandler $webSocketHandler; @@ -46,6 +51,8 @@ class Server $this->config = $config; + $this->logger = $this->createLogger(); + $this->topicManager = new TopicManager(); $this->webSocketClients = new SplObjectStorage(); @@ -63,6 +70,15 @@ class Server $this->server->listen($socket); } + private function createLogger(): LoggerInterface + { + $handlers = [ + new StreamHandler(STDOUT) + ]; + $logger = new Logger("main", $handlers); + return $logger; + } + /** * * @return HttpServer @@ -71,7 +87,8 @@ class Server { return new HttpServer( $this->responseMiddleware = new ResponseMiddleware( - config: $this->config + config: $this->config, + logger: $this->logger->withName("http"), ), $this->securityMiddleware = new SecurityMiddleware( config: $this->config