nitcorn: optimize `FileServer::root` to be used later on each request
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 17 Sep 2014 20:17:16 +0000 (16:17 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 17 Sep 2014 20:17:33 +0000 (16:17 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/nitcorn/file_server.nit

index 2b94280..6c6d39c 100644 (file)
@@ -42,9 +42,23 @@ end
 class FileServer
        super Action
 
-       # Root of `self` file system
+       # Root folder of `self` file system
        var root: String
 
+       init
+       do
+               var root = self.root
+
+               # Simplify the root path as each file requested will also be simplified
+               root = root.simplify_path
+
+               # Make sure the root ends with '/', this makes a difference in the security
+               # check on each file access.
+               root = root + "/"
+
+               self.root = root
+       end
+
        # Error page template for a given `code`
        fun error_page(code: Int): Streamable do return new ErrorTemplate(code)
 
@@ -58,7 +72,6 @@ class FileServer
                var local_file = root.join_path(turi.strip_start_slashes)
                local_file = local_file.simplify_path
 
-
                # Is it reachable?
                #
                # This make sure that the requested file is within the root folder.