예제 #1
0
파일: Router.php 프로젝트: igez/gaiaehr
 /**
  * {@inheritdoc}
  * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface
  */
 public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
 {
     if (null === $request) {
         throw new \UnexpectedValueException('$request can not be null');
     }
     $context = $this->_matcher->getContext();
     $context->setMethod($request->getMethod());
     $context->setHost($request->getHost());
     try {
         $route = $this->_matcher->match($request->getPath());
     } catch (MethodNotAllowedException $nae) {
         return $this->close($conn, 403);
     } catch (ResourceNotFoundException $nfe) {
         return $this->close($conn, 404);
     }
     if (is_string($route['_controller']) && class_exists($route['_controller'])) {
         $route['_controller'] = new $route['_controller']();
     }
     if (!$route['_controller'] instanceof HttpServerInterface) {
         throw new \UnexpectedValueException('All routes must implement Ratchet\\Http\\HttpServerInterface');
     }
     $parameters = array();
     foreach ($route as $key => $value) {
         if (is_string($key) && '_' !== substr($key, 0, 1)) {
             $parameters[$key] = $value;
         }
     }
     $url = Url::factory($request->getPath());
     $url->setQuery($parameters);
     $request->setUrl($url);
     $conn->controller = $route['_controller'];
     $conn->controller->onOpen($conn, $request);
 }
예제 #2
0
 /**
  * @override
  * @inheritDoc
  */
 public function handleMessage(IoConnectionInterface $conn, IoMessageInterface $message)
 {
     if (!$message instanceof HttpRequestInterface) {
         $conn->controller->handleMessage($conn, $message);
         return;
     }
     if (($header = $message->getHeaderLine('Origin')) !== '') {
         $origin = parse_url($header, PHP_URL_HOST) ?: $header;
         if ($origin !== '' && $this->isBlocked($origin)) {
             return $this->close($conn, 403);
         }
     }
     $context = $this->matcher->getContext();
     $context->setMethod($message->getMethod());
     $context->setHost($message->getUri()->getHost());
     $route = [];
     try {
         $route = $this->matcher->match($message->getUri()->getPath());
     } catch (Error $ex) {
         return $this->close($conn, 500);
     } catch (MethodNotAllowedException $nae) {
         return $this->close($conn, 403);
     } catch (ResourceNotFoundException $nfe) {
         return $this->close($conn, 404);
     }
     $conn->controller = $route['_controller'];
     try {
         $conn->controller->handleConnect($conn);
         $conn->controller->handleMessage($conn, $message);
     } catch (Error $ex) {
         $conn->controller->handleError($conn, $ex);
     } catch (Exception $ex) {
         $conn->controller->handleError($conn, $ex);
     }
 }
예제 #3
0
 /**
  * Constructor.
  *
  * RequestStack will become required in 3.0.
  *
  * @param UrlMatcherInterface|RequestMatcherInterface $matcher      The Url or Request matcher
  * @param RequestStack                                $requestStack A RequestStack instance
  * @param RequestContext|null                         $context      The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
  * @param LoggerInterface|null                        $logger       The logger
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($matcher, $requestStack = null, $context = null, $logger = null)
 {
     if ($requestStack instanceof RequestContext || $context instanceof LoggerInterface || $logger instanceof RequestStack) {
         $tmp = $requestStack;
         $requestStack = $logger;
         $logger = $context;
         $context = $tmp;
         @trigger_error('The ' . __METHOD__ . ' method now requires a RequestStack to be given as second argument as ' . __CLASS__ . '::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
     } elseif (!$requestStack instanceof RequestStack) {
         @trigger_error('The ' . __METHOD__ . ' method now requires a RequestStack instance as ' . __CLASS__ . '::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
     }
     if (null !== $requestStack && !$requestStack instanceof RequestStack) {
         throw new \InvalidArgumentException('RequestStack instance expected.');
     }
     if (null !== $context && !$context instanceof RequestContext) {
         throw new \InvalidArgumentException('RequestContext instance expected.');
     }
     if (null !== $logger && !$logger instanceof LoggerInterface) {
         throw new \InvalidArgumentException('Logger must implement LoggerInterface.');
     }
     if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
         throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
     }
     if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
         throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
     }
     $this->matcher = $matcher;
     $this->context = $context ?: $matcher->getContext();
     $this->requestStack = $requestStack;
     $this->logger = $logger;
 }
예제 #4
0
 /**
  * Constructor.
  *
  * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
  * @param RequestContext|null                         $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
  * @param LoggerInterface|null                        $logger  The logger
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null)
 {
     if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
         throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
     }
     if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
         throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
     }
     $this->matcher = $matcher;
     $this->context = $context ?: $matcher->getContext();
     $this->logger = $logger;
 }
예제 #5
0
 /**
  * {@inheritdoc}
  * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface
  */
 public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
 {
     if (null === $request) {
         throw new \UnexpectedValueException('$request can not be null');
     }
     $context = $this->_matcher->getContext();
     $context->setMethod($request->getMethod());
     $context->setHost($request->getHost());
     try {
         $route = $this->_matcher->match($request->getPath());
     } catch (MethodNotAllowedException $nae) {
         return $this->close($conn, 403);
     } catch (ResourceNotFoundException $nfe) {
         return $this->close($conn, 404);
     }
     if (is_string($route['_controller']) && class_exists($route['_controller'])) {
         $route['_controller'] = new $route['_controller']();
     }
     if (!$route['_controller'] instanceof HttpServerInterface) {
         throw new \UnexpectedValueException('All routes must implement Ratchet\\Http\\HttpServerInterface');
     }
     $conn->controller = $route['_controller'];
     $conn->controller->onOpen($conn, $request);
 }
예제 #6
0
 public function onKernelRequest(KernelEvent $event)
 {
     if (strtoupper($event->getRequest()->getMethod()) !== 'LINK' && strtoupper($event->getRequest()->getMethod()) !== 'UNLINK') {
         return;
     }
     if (!$event->getRequest()->headers->has('link')) {
         throw new BadRequestHttpException('Please specify at least one Link.');
     }
     $requestMethod = $this->urlMatcher->getContext()->getMethod();
     $this->urlMatcher->getContext()->setMethod('GET');
     $links = [];
     /*
      * Due to limitations, multiple same-name headers are sent as comma
      * separated values.
      *
      * This breaks those headers into Link headers following the format
      * http://tools.ietf.org/html/rfc2068#section-19.6.2.4
      */
     foreach (explode(',', $event->getRequest()->headers->get('link')) as $header) {
         $header = trim($header);
         $link = new LinkHeader($header);
         try {
             if ($urlParameters = $this->urlMatcher->match($link->getValue())) {
                 $link->setUrlParameters($urlParameters);
             }
         } catch (ResourceNotFoundException $exception) {
         }
         try {
             $link->setResource($this->resourceTransformer->getResourceProxy($link->getValue()));
         } catch (InvalidArgumentException $e) {
         }
         $links[] = $link;
     }
     $this->urlMatcher->getContext()->setMethod($requestMethod);
     $event->getRequest()->attributes->set('links', $links);
 }
 /**
  * Constructor.
  *
  * RequestStack will become required in 3.0.
  *
  * @param UrlMatcherInterface|RequestMatcherInterface $matcher      The Url or Request matcher
  * @param RequestContext|null                         $context      The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
  * @param LoggerInterface|null                        $logger       The logger
  * @param RequestStack|null                           $requestStack A RequestStack instance
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null, RequestStack $requestStack = null)
 {
     if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
         throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
     }
     if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
         throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
     }
     if (!$requestStack instanceof RequestStack) {
         @trigger_error('The ' . __METHOD__ . ' method now requires a RequestStack instance as ' . __CLASS__ . '::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
     }
     $this->matcher = $matcher;
     $this->context = $context ?: $matcher->getContext();
     $this->requestStack = $requestStack;
     $this->logger = $logger;
 }
 /**
  * Makes a subrequest to retrieve the default error page.
  *
  * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  *   The event to process.
  * @param string $url
  *   The path/url to which to make a subrequest for this error message.
  * @param int $status_code
  *   The status code for the error being handled.
  */
 protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $status_code)
 {
     $request = $event->getRequest();
     $exception = $event->getException();
     try {
         // Reuse the exact same request (so keep the same URL, keep the access
         // result, the exception, et cetera) but override the routing information.
         // This means that aside from routing, this is identical to the master
         // request. This allows us to generate a response that is executed on
         // behalf of the master request, i.e. for the original URL. This is what
         // allows us to e.g. generate a 404 response for the original URL; if we
         // would execute a subrequest with the 404 route's URL, then it'd be
         // generated for *that* URL, not the *original* URL.
         $sub_request = clone $request;
         // The routing to the 404 page should be done as GET request because it is
         // restricted to GET and POST requests only. Otherwise a DELETE request
         // would for example trigger a method not allowed exception.
         $request_context = clone $this->accessUnawareRouter->getContext();
         $request_context->setMethod('GET');
         $this->accessUnawareRouter->setContext($request_context);
         $sub_request->attributes->add($this->accessUnawareRouter->match($url));
         // Add to query (GET) or request (POST) parameters:
         // - 'destination' (to ensure e.g. the login form in a 403 response
         //   redirects to the original URL)
         // - '_exception_statuscode'
         $parameters = $sub_request->isMethod('GET') ? $sub_request->query : $sub_request->request;
         $parameters->add($this->redirectDestination->getAsArray() + ['_exception_statuscode' => $status_code]);
         $response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
         // Only 2xx responses should have their status code overridden; any
         // other status code should be passed on: redirects (3xx), error (5xx)…
         // @see https://www.drupal.org/node/2603788#comment-10504916
         if ($response->isSuccessful()) {
             $response->setStatusCode($status_code);
         }
         // Persist any special HTTP headers that were set on the exception.
         if ($exception instanceof HttpExceptionInterface) {
             $response->headers->add($exception->getHeaders());
         }
         $event->setResponse($response);
     } catch (\Exception $e) {
         // If an error happened in the subrequest we can't do much else. Instead,
         // just log it. The DefaultExceptionSubscriber will catch the original
         // exception and handle it normally.
         $error = Error::decodeException($e);
         $this->logger->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
     }
 }
 /**
  * @param FilterControllerEvent $event A FilterControllerEvent instance
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     if (!$event->getRequest()->headers->has('link')) {
         return;
     }
     $links = array();
     $header = $event->getRequest()->headers->get('link');
     /*
      * Due to limitations, multiple same-name headers are sent as comma
      * separated values.
      *
      * This breaks those headers into Link headers following the format
      * http://tools.ietf.org/html/rfc2068#section-19.6.2.4
      */
     while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) {
         $header = trim(substr($header, strlen($matches[0])));
         $links[] = $matches[1];
     }
     if ($header) {
         $links[] = $header;
     }
     $requestMethod = $this->urlMatcher->getContext()->getMethod();
     // Force the GET method to avoid the use of the
     // previous method (LINK/UNLINK)
     $this->urlMatcher->getContext()->setMethod('GET');
     // The controller resolver needs a request to resolve the controller.
     $stubRequest = new Request();
     foreach ($links as $idx => $link) {
         $linkParams = explode(';', trim($link));
         $resource = array_shift($linkParams);
         $resource = preg_replace('/<|>/', '', $resource);
         if (preg_match('#^/|https?://#', $resource) === 0) {
             $resource = '/' . $resource;
         }
         try {
             $route = $this->urlMatcher->match($resource);
         } catch (\Exception $e) {
             // If we don't have a matching route we return
             // the original Link header
             continue;
         }
         $stubRequest->attributes->replace($route);
         if (false === ($controller = $this->resolver->getController($stubRequest))) {
             continue;
         }
         try {
             $stubEvent = new FilterControllerEvent($this->httpKernel, $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST);
             $this->eventDispatcher->dispatch(KernelEvents::CONTROLLER, $stubEvent);
             $arguments = $this->resolver->getArguments($stubRequest, $controller);
             $result = call_user_func_array($controller, $arguments);
             // By convention the controller action must return an array
             if (!is_array($result)) {
                 continue;
             }
             // The key of first item is discarded
             $links[$idx] = current($result);
         } catch (\Exception $e) {
             continue;
         }
     }
     $event->getRequest()->attributes->set('link', $links);
     $this->urlMatcher->getContext()->setMethod($requestMethod);
 }
 public function onKernelRequest(GetResponseEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
         return;
     }
     if (!$event->getRequest()->headers->has('link')) {
         return;
     }
     $links = array();
     $header = $event->getRequest()->headers->get('link');
     /*
      * Due to limitations, multiple same-name headers are sent as comma
      * separated values.
      *
      * This breaks those headers into Link headers following the format
      * http://tools.ietf.org/html/rfc2068#section-19.6.2.4
      */
     while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) {
         $header = trim(substr($header, strlen($matches[0])));
         $links[] = $matches[1];
     }
     if ($header) {
         $links[] = $header;
     }
     $requestMethod = $this->urlMatcher->getContext()->getMethod();
     // Force the GET method to avoid the use of the
     // previous method (LINK/UNLINK)
     $this->urlMatcher->getContext()->setMethod('GET');
     // The controller resolver needs a request to resolve the controller.
     $stubRequest = new Request();
     foreach ($links as $idx => $link) {
         $linkHeader = $this->parseLinkHeader($link);
         $resource = $this->parseResource($linkHeader, $event->getRequest());
         try {
             $route = $this->urlMatcher->match($resource);
         } catch (\Exception $e) {
             // If we don't have a matching route we return
             // the original Link header
             continue;
         }
         $stubRequest->attributes->replace($route);
         if (false === ($controller = $this->resolver->getController($stubRequest))) {
             continue;
         }
         // Make sure @ParamConverter and some other annotations are called
         $subEvent = new FilterControllerEvent($event->getKernel(), $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST);
         $event->getDispatcher()->dispatch(KernelEvents::CONTROLLER, $subEvent);
         $controller = $subEvent->getController();
         $arguments = $this->resolver->getArguments($stubRequest, $controller);
         try {
             $result = call_user_func_array($controller, $arguments);
             $value = is_array($result) ? current($result) : $result;
             if ($linkHeader->hasRel()) {
                 unset($links[$idx]);
                 $links[$linkHeader->getRel()][] = $value;
             } else {
                 $links[$idx] = $value;
             }
         } catch (\Exception $e) {
             continue;
         }
     }
     $event->getRequest()->attributes->set('links', $links);
     $this->urlMatcher->getContext()->setMethod($requestMethod);
 }
 /**
  * @param GetResponseEvent $event
  *
  * @return array
  */
 public function onKernelRequest(GetResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher)
 {
     if (!$event->getRequest()->headers->has('link')) {
         return;
     }
     $links = [];
     $header = $event->getRequest()->headers->get('link');
     /*
      * Due to limitations, multiple same-name headers are sent as comma
      * separated values.
      *
      * This breaks those headers into Link headers following the format
      * http://tools.ietf.org/html/rfc2068#section-19.6.2.4
      */
     while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) {
         $header = trim(substr($header, strlen($matches[0])));
         $links[] = $matches[1];
     }
     if ($header) {
         $links[] = $header;
     }
     $requestMethod = $this->urlMatcher->getContext()->getMethod();
     // The controller resolver needs a request to resolve the controller.
     $stubRequest = new Request();
     foreach ($links as $idx => $link) {
         // Force the GET method to avoid the use of the previous method (LINK/UNLINK)
         $this->urlMatcher->getContext()->setMethod('GET');
         $linkParams = explode(';', trim($link));
         $resourceType = null;
         if (count($linkParams) > 1) {
             $resourceType = trim(preg_replace('/<|>/', '', $linkParams[1]));
             $resourceType = str_replace('"', '', str_replace('rel=', '', $resourceType));
         }
         $resource = array_shift($linkParams);
         $resource = preg_replace('/<|>/', '', $resource);
         // Assume that no resource is specified here if there is no path separator, because urlMatcher will return homepage
         if (strpos($resource, '/') === false) {
             continue;
         }
         $tempRequest = Request::create($resource);
         try {
             $route = $this->urlMatcher->match($tempRequest->getRequestUri());
         } catch (\Exception $e) {
             // If we don't have a matching route we return the original Link header
             continue;
         }
         $stubRequest->attributes->replace($route);
         $stubRequest->server = $event->getRequest()->server;
         if (false === ($controller = $this->resolver->getController($stubRequest))) {
             continue;
         }
         $subEvent = new FilterControllerEvent($event->getKernel(), $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST);
         $kernelSubEvent = new GetResponseEvent($event->getKernel(), $stubRequest, HttpKernelInterface::SUB_REQUEST);
         $dispatcher->dispatch(KernelEvents::REQUEST, $kernelSubEvent);
         $dispatcher->dispatch(KernelEvents::CONTROLLER, $subEvent);
         $controller = $subEvent->getController();
         $arguments = $this->resolver->getArguments($stubRequest, $controller);
         if (!isset($arguments[0])) {
             continue;
         }
         $arguments[0]->attributes->set('_link_request', true);
         try {
             $result = call_user_func_array($controller, $arguments);
             // Our api returns objects for single resources
             if (!is_object($result)) {
                 continue;
             }
             $links[$idx] = ['object' => $result, 'resourceType' => $resourceType];
         } catch (\Exception $e) {
             $links[$idx] = ['object' => $e, 'resourceType' => 'exception'];
             continue;
         }
     }
     $event->getRequest()->attributes->set('links', $links);
     $this->urlMatcher->getContext()->setMethod($requestMethod);
     return $links;
 }