Esempio n. 1
0
 public function __construct(Configuration $config, RequestInterface $request)
 {
     $this->_uriComponents = array();
     $this->_config = $config;
     $requestHeaders = $request->getHeaders();
     $this->scheme($request->isSecure() ? 'https' : 'http');
     if (isset($requestHeaders['host'])) {
         $this->host($requestHeaders['host']);
     }
     // set the port number using the one from the Request, or the default
     // for the request scheme
     if ($request->getPort()) {
         $this->port($request->getPort());
     } else {
         if ('http' == $this->_uriComponents['scheme']) {
             $this->port(80);
         } else {
             if ('ftp' == $this->_uriComponents['scheme']) {
                 $this->port(21);
             }
         }
     }
     $this->_queryParams = $request->getQueryParams();
     $requestUri = $request->getRequestUri();
     if (($hashOffset = strpos('#', $requestUri)) !== FALSE) {
         $this->replacePath(strstr($requestUri, '#', true));
         $this->fragment(substr(strstr($requestUri, '#'), 1));
     } else {
         $this->replacePath($requestUri);
     }
 }
Esempio n. 2
0
 /**
  * Find candidate routes for an incoming request.
  *
  * @param RequestInterface $request        The incoming request
  * @param array            $pathParameters The set of Path parameters
  *                                         matched on the incoming Request
  *                                         path.
  *
  * @throws \Sonno\Http\Exception\MethodNotAllowedException
  * @throws \Sonno\Http\Exception\NotFoundException
  * @throws \InvalidArgumentException
  * @throws \Sonno\Http\Exception\NotAcceptableException
  *
  * @return array A collection of candidate Route objects.
  *
  * @todo   When filtering candidate routes by matching the incoming
  *         media type, Sonno is ignoring any Content-Type parameters
  *         including the charset. This should be resolved, otherwise there
  *         will be unintended consequences while dealing with charsets and
  *         other Content-Type parameters.
  */
 public function match(RequestInterface $request, &$pathParameters = array())
 {
     if (null == $request) {
         throw new InvalidArgumentException('Missing function argument: request');
     }
     $candidateRoutes = array();
     $allRoutes = $this->_config->getRoutes();
     $requestPath = $request->getRequestUri();
     $requestMethod = $request->getMethod();
     $requestContentType = $request->getContentType();
     // drop the base path from the beginning of the incoming path
     $basePath = $this->_config->getBasePath();
     if ($basePath && strstr($requestPath, $basePath) !== FALSE) {
         $requestPath = substr($requestPath, strlen($basePath));
     }
     // locate matching routes using the incoming request path
     /** @var $route \Sonno\Configuration\Route */
     foreach ($allRoutes as $route) {
         $params = $this->_matchPath($requestPath, $route->getPath());
         if (false !== $params) {
             $pathParameters = $params;
             $candidateRoutes[] = $route;
         }
     }
     if (empty($candidateRoutes)) {
         throw new NotFoundException();
     }
     // filter candidate routes further by matching the incoming request
     // method
     $allowedMethods = array();
     foreach ($candidateRoutes as $i => $route) {
         if ($route->getHttpMethod() != $requestMethod) {
             $allowedMethods[] = $route->getHttpMethod();
             unset($candidateRoutes[$i]);
         }
     }
     if (empty($candidateRoutes)) {
         throw new MethodNotAllowedException($allowedMethods);
     }
     // filter candidate routes further by matching the incoming media type
     if (!empty($requestContentType)) {
         foreach ($candidateRoutes as $i => $route) {
             if (($offset = strpos($requestContentType, ';')) !== false) {
                 $requestContentType = substr($requestContentType, 0, $offset);
             }
             if (!in_array($requestContentType, $route->getConsumes())) {
                 unset($candidateRoutes[$i]);
             }
         }
     }
     if (empty($candidateRoutes)) {
         throw new NotAcceptableException();
     }
     return $candidateRoutes;
 }
Esempio n. 3
0
 public function testRequestInjection()
 {
     return $this->_request->getRequestUri();
 }