X-Git-Url: http://nitlanguage.org diff --git a/lib/nitcorn/file_server.nit b/lib/nitcorn/file_server.nit index 3dadab9..96293f6 100644 --- a/lib/nitcorn/file_server.nit +++ b/lib/nitcorn/file_server.nit @@ -42,14 +42,43 @@ 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}}} @@ -119,32 +222,7 @@ class FileServer """ - response.header["Content-Type"] = media_types["html"].as(not null) - else - # It's a single file - var file = new IFStream.open(local_file) - response.body = file.read_all - - var ext = local_file.file_extension - if ext != null then - var media_type = media_types[ext] - if media_type != null then - response.header["Content-Type"] = media_type - else response.header["Content-Type"] = "application/octet-stream" - end - - file.close - end - - else response = new HttpResponse(404) - else response = new HttpResponse(403) - - if response.status_code != 200 then - var tmpl = error_page(response.status_code) - if header != null and tmpl isa ErrorTemplate then tmpl.header = header - response.body = tmpl.to_s - end - + response.header["Content-Type"] = media_types["html"].as(not null) return response end end