Ejemplo n.º 1
0
 public function parseHeader($header)
 {
     $request = new Request();
     $header = new Text($header);
     $lines = $header->lines();
     $first = array_shift($lines);
     $parts = explode(' ', $first);
     foreach ($parts as $index => $value) {
         switch ($index) {
             case 0:
                 $request->setMethod($value);
                 break;
             case 1:
                 $path = parse_url($value, PHP_URL_PATH);
                 $query = parse_url($value, PHP_URL_QUERY);
                 $request->setUri($path);
                 $request->setPath($path);
                 $request->setQueryString($query);
                 break;
             case 2:
                 list($protocol, $version) = explode('/', trim($value));
                 $request->setProtocol($protocol);
                 $request->setProtocolVersion($version);
                 break;
         }
     }
     foreach ($lines as $line) {
         if (!$line->trim()->isEmpty()) {
             list($name, $value) = explode(':', $line, 2);
             $request->setHeader(trim($name), trim($value));
         }
     }
     return $request;
 }
Ejemplo n.º 2
0
 /**
  * @param Request $request
  *
  * @return \SplFileInfo
  * @throws ResourceNotFoundException
  */
 protected function findFile(Request $request)
 {
     $path = $this->joinPath($this->options['docroot'], $request->getUri());
     $file = new \SplFileInfo($path);
     if ($file->isDir()) {
         // find indexes
         foreach ($this->options['indexes'] as $index) {
             $fixedPath = $this->joinPath($path, $index);
             $fixedFile = new \SplFileInfo($fixedPath);
             if ($fixedFile->isFile()) {
                 return $fixedFile;
             }
         }
     }
     if ($file->isFile()) {
         if (substr($path, -1) == '/') {
             $request->setPathInfo('/');
         }
         return $file;
     }
     if (preg_match('{(^[^\\.]*?\\.[^/]+)(/.*?$)}', $request->getUri(), $matches)) {
         $file = new \SplFileInfo($this->joinPath($this->options['docroot'], $matches[1]));
         if ($file->isFile()) {
             $request->setPathInfo($matches[2]);
             $request->setPath($matches[1]);
             return $file;
         }
     }
     // find fallback
     $path = $this->joinPath($this->options['docroot'], $this->options['fallback']);
     $file = new \SplFileInfo($path);
     if ($file->isFile()) {
         return $file;
     }
     throw new ResourceNotFoundException();
 }