Commits

Thomas Weinert committed 8b8a4a9

Renaming Carica/Io/Network/Http/Route/File to Carica/Io/Network/Http/Route/Directory, delivering files from directories
Added new Carica/Io/Network/Http/Route/File to deliver a single file

Comments (0)

Files changed (3)

example/Http/route.php

     return $response;
   }
 );
-$route->startsWith('/files', new Http\Route\File(__DIR__));
+$route->match('/hello', new Http\Route\File(__DIR__.'/files/hello.html'));
+$route->startsWith('/files', new Http\Route\Directory(__DIR__));
 
 $server = new Carica\Io\Network\Http\Server($route);
 $server->listen(8080);

src/Carica/Io/Network/Http/Route/Directory.php

+<?php
+
+namespace Carica\Io\Network\Http\Route {
+
+  use Carica\Io;
+  use Carica\Io\Network\Http;
+
+  class Directory {
+
+    use Io\File\Access\Aggregation;
+
+    private $_documentRoot = '';
+
+    private $_encodings = array(
+      'text/plain' => 'utf-8',
+      'text/html' => 'utf-8',
+      'text/javascript' => 'utf-8',
+      'application/x-javascript' => 'utf-8',
+      'application/xml' => 'utf-8'
+    );
+
+    public function __construct($documentRoot, array $encodings = array()) {
+      $this->setDocumentRoot($documentRoot);
+      foreach ($encodings as $mimetype => $encoding) {
+        $this->setEncoding($mimetype, $encoding);
+      }
+    }
+
+    public function setDocumentRoot($documentRoot) {
+      if ($directory = $this->fileAccess()->getRealPath($documentRoot)) {
+        $this->_documentRoot = $directory;
+        return;
+      }
+      throw new \LogicException(
+        sprintf(
+         'Invalid document root: Directory "%s" not found.',
+         $documentRoot
+        )
+      );
+    }
+
+    public function setEncoding($mimetype, $encoding) {
+      $this->_encodings[$mimetype] = (string)$encoding;
+    }
+
+    public function getEncoding($mimetype) {
+      if (isset($this->_encodings[$mimetype])) {
+        return $this->_encodings[$mimetype];
+      }
+      return '';
+    }
+
+    public function __invoke() {
+      return call_user_func_array(array($this, 'call'), func_get_args());
+    }
+
+    public function call(Http\Request $request) {
+      if ($file = $this->getFileInfo($request)) {
+        if ($file->isFile() && $file->isReadable()) {
+          $response = $request->createResponse();
+          $localFile = $file->getRealPath();
+          $mimetype = $this->fileAccess()->getMimeType($localFile);
+          $encoding = $this->getEncoding($mimetype);
+          $response->content = new Http\Response\Content\File(
+            $localFile, $mimetype, $encoding
+          );
+          return $response;
+        } else {
+          return new Http\Response\Error(
+            $request, 403
+          );
+        }
+      }
+      return NULL;
+    }
+
+    /**
+     * @param Http\Request $request
+     * @return \SplFileInfo
+     */
+    private function getFileInfo(Http\Request $request) {
+      if ($localFile = $this->fileAccess()->getRealPath($this->_documentRoot.$request->path)) {
+        if (0 === strpos($localFile, $this->_documentRoot.DIRECTORY_SEPARATOR)) {
+          return $this->fileAccess()->getInfo($localFile);
+        }
+      }
+      return FALSE;
+    }
+  }
+}

src/Carica/Io/Network/Http/Route/File.php

 
     use Io\File\Access\Aggregation;
 
-    private $_documentRoot = '';
+    private $_file = '';
 
-    private $_encodings = array(
-      'text/plain' => 'utf-8',
-      'text/html' => 'utf-8',
-      'text/javascript' => 'utf-8',
-      'application/x-javascript' => 'utf-8',
-      'application/xml' => 'utf-8'
-    );
+    private $_encoding = 'utf-8';
 
-    public function __construct($documentRoot, array $encodings = array()) {
-      $this->setDocumentRoot($documentRoot);
-      foreach ($encodings as $mimetype => $encoding) {
-        $this->setEncoding($mimetype, $encoding);
-      }
+    public function __construct($file, $encoding = 'utf-8') {
+      $this->setFile($file);
+      $this->_encoding = $encoding;
     }
 
-    public function setDocumentRoot($documentRoot) {
-      if ($directory = $this->fileAccess()->getRealPath($documentRoot)) {
-        $this->_documentRoot = $directory;
+    public function setFile($documentRoot) {
+      if ($file = $this->fileAccess()->getRealPath($documentRoot)) {
+        $this->_file = $file;
         return;
       }
       throw new \LogicException(
         sprintf(
-         'Invalid document root: Directory "%s" not found.',
-         $documentRoot
+         'Invalid file: "%s" not found.',
+         $file
         )
       );
     }
 
-    public function setEncoding($mimetype, $encoding) {
-      $this->_encodings[$mimetype] = (string)$encoding;
-    }
-
-    public function getEncoding($mimetype) {
-      if (isset($this->_encodings[$mimetype])) {
-        return $this->_encodings[$mimetype];
-      }
-      return '';
-    }
-
     public function __invoke() {
       return call_user_func_array(array($this, 'call'), func_get_args());
     }
 
     public function call(Http\Request $request) {
-      if ($file = $this->getFileInfo($request)) {
+      if ($file = $this->getFileInfo()) {
         if ($file->isFile() && $file->isReadable()) {
           $response = $request->createResponse();
           $localFile = $file->getRealPath();
           $mimetype = $this->fileAccess()->getMimeType($localFile);
-          $encoding = $this->getEncoding($mimetype);
+          $encoding = $this->_encoding;
           $response->content = new Http\Response\Content\File(
             $localFile, $mimetype, $encoding
           );
     }
 
     /**
-     * @param Http\Request $request
      * @return \SplFileInfo
      */
-    private function getFileInfo(Http\Request $request) {
-      if ($localFile = $this->fileAccess()->getRealPath($this->_documentRoot.$request->path)) {
-        if (0 === strpos($localFile, $this->_documentRoot.DIRECTORY_SEPARATOR)) {
-          return $this->fileAccess()->getInfo($localFile);
-        }
+    private function getFileInfo() {
+      if ($localFile = $this->fileAccess()->getRealPath($this->_file)) {
+        return $this->fileAccess()->getInfo($localFile);
       }
       return FALSE;
     }