X-Git-Url: http://nitlanguage.org diff --git a/lib/nitcorn/file_server.nit b/lib/nitcorn/file_server.nit index d9b4e45..5995558 100644 --- a/lib/nitcorn/file_server.nit +++ b/lib/nitcorn/file_server.nit @@ -42,14 +42,37 @@ 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) + fun error_page(code: Int): Writable do return new ErrorTemplate(code) # Header of each directory page - var header: nullable Streamable = null is writable + var header: nullable Writable = null is writable + + # Custom JavaScript code added within a ` {{{title}}} @@ -129,10 +158,10 @@ class FileServer """ response.header["Content-Type"] = media_types["html"].as(not null) - else + else if not is_dir then # It's a single file - var file = new IFStream.open(local_file) - response.body = file.read_all + response = new HttpResponse(200) + response.files.add local_file var ext = local_file.file_extension if ext != null then @@ -142,9 +171,10 @@ class FileServer else response.header["Content-Type"] = "application/octet-stream" end - file.close - end + # Cache control + response.header["cache-control"] = cache_control + else response = new HttpResponse(404) else response = new HttpResponse(404) else response = new HttpResponse(403)