X-Git-Url: http://nitlanguage.org diff --git a/lib/nitcorn/file_server.nit b/lib/nitcorn/file_server.nit index f9feb0f..a9ef5f6 100644 --- a/lib/nitcorn/file_server.nit +++ b/lib/nitcorn/file_server.nit @@ -74,6 +74,12 @@ class FileServer # Show directory listing? var show_directory_listing = true is writable + # Default file returned when no static file matches the requested URI. + # + # If no `default_file` is provided, the FileServer responds 404 error to + # unmatched queries. + var default_file: nullable String = null is writable + redef fun answer(request, turi) do var response @@ -86,15 +92,14 @@ class FileServer # This make sure that the requested file is within the root folder. if (local_file + "/").has_prefix(root) then # Does it exists? - if local_file.file_exists then - if local_file.file_stat.is_dir then + var file_stat = local_file.file_stat + if file_stat != null then + if file_stat.is_dir then # If we target a directory without an ending `/`, # redirect to the directory ending with `/`. - if not request.uri.is_empty and - request.uri.chars.last != '/' then - response = new HttpResponse(303) - response.header["Location"] = request.uri + "/" - return response + var uri = request.uri + if not uri.is_empty and uri.chars.last != '/' then + return answer_redirection(request.uri + "/") end # Show index file instead of the directory listing @@ -108,60 +113,13 @@ class FileServer end end - var is_dir = local_file.file_stat.is_dir - if show_directory_listing and is_dir then - # Show the directory listing - var title = turi - var files = local_file.files - - alpha_comparator.sort files - - var links = new Array[String] - if turi.length > 1 then - var path = (request.uri + "/..").simplify_path - links.add ".." - end - for file in files do - var local_path = local_file.join_path(file).simplify_path - var web_path = file.simplify_path - if local_path.file_stat.is_dir then web_path = web_path + "/" - links.add "{file}" - end - - var header = self.header - var header_code - if header != null then - header_code = header.write_to_string - else header_code = "" - - response = new HttpResponse(200) - response.body = """ - -
- - - - -