/** * Handles react request and create http compliant request/response objects * * @param ReactRequestEvent $event */ public function onRequest(ReactRequestEvent $event) { $event->stopPropagation(); $request = RequestFactory::createFromReact($event->getRequest(), $event->getData()); $response = new Response(); $response->setReactResponse($event->getResponse()); try { $this->dispatcher->trigger($requestEvent = new HttpRequestEvent($request, $response)); if ($requestEvent->shouldPropagate()) { $response->setStatusCode(new StatusCode(StatusCode::NOT_FOUND))->setContent(""); } } catch (AbstractHttpException $e) { $this->dispatcher->trigger($exceptionEvent = new HttpExceptionEvent($e, $request, $response)); if ($exceptionEvent->shouldPropagate()) { $response->setStatusCode($e->getStatusCode()); if (trim($e->getMessage()) != '') { $response->setContent($e->getMessage()); } else { $response->getHeaders()->remove('Content-Type'); $response->setContent(""); } } } catch (\Exception $e) { $this->dispatcher->trigger($event = new UnknownHttpExceptionEvent($e, $request, $response)); if ($event->shouldPropagate()) { $response->setStatusCode(new StatusCode(StatusCode::INTERNAL_SERVER_ERROR)); $response->getHeaders()->remove('Content-Type'); $response->setContent(""); } } }
/** * Handles request event * * @param HttpRequestEvent $event * * @throws NotFoundHttpException */ public function onRequest(HttpRequestEvent $event) { $event->getResponse()->getHeaders()->set('X-Powered-By', 'ThinFrame/Karma (v0.2, engine ReactPHP)'); $matcher = new UrlMatcher($this->routeCollection, new RequestContext('', $event->getRequest()->getMethod())); try { $route = $matcher->match($event->getRequest()->getPath()); $this->handleRoute($route, $event->getRequest(), $event->getResponse()); $event->stopPropagation(); } catch (ResourceNotFoundException $exception) { throw new NotFoundHttpException(); } }
/** * Handle http request * * @param HttpRequestEvent $event */ public function onRequest(HttpRequestEvent $event) { $filePath = $event->getRequest()->getPath(); foreach ($this->application->getMetadata() as $name => $metadata) { /* @var $metadata \PhpCollection\Map */ if ($metadata->containsKey('assets')) { $assumedFilePath = $metadata->get('application_path')->get() . DIRECTORY_SEPARATOR . $metadata->get('assets')->get() . DIRECTORY_SEPARATOR . $filePath; $assumedFilePath = realpath($assumedFilePath); if (file_exists($assumedFilePath) && is_file($assumedFilePath)) { $event->stopPropagation(); $fileStream = new Stream(fopen($assumedFilePath, 'r'), $this->serverLoop); $event->getResponse()->getHeaders()->set('Content-Type', MimeTypeGuesser::getMimeType($assumedFilePath)); $event->getResponse()->dispatchHeaders(); $fileStream->pipe($event->getResponse()->getReactResponse()); $fileStream->on('end', 'gc_collect_cycles'); gc_collect_cycles(); } } } }