Merge: doc: fixed some typos and other misc. corrections
[nit.git] / lib / nitcorn / file_server.nit
index 0245d1f..96293f6 100644 (file)
@@ -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,8 +92,9 @@ 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 `/`.
                                        var uri = request.uri
@@ -106,24 +113,35 @@ class FileServer
                                        end
                                end
 
-                               var is_dir = local_file.file_stat.is_dir
-                               if show_directory_listing and is_dir then
+                               file_stat = local_file.file_stat
+                               if show_directory_listing and file_stat != null and file_stat.is_dir then
                                        response = answer_directory_listing(request, turi, local_file)
-                               else if not is_dir then # It's a single file
+                               else if file_stat != null and not file_stat.is_dir then # It's a single file
                                        response = answer_file(local_file)
-                               else response = new HttpResponse(404)
-                       else response = new HttpResponse(404)
+                               else response = answer_default
+                       else response = answer_default
                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
+                       response.body = tmpl
                end
 
                return response
        end
 
+       # Answer the `default_file` if any.
+       fun answer_default: HttpResponse do
+               var default_file = self.default_file
+               if default_file == null then
+                       return new HttpResponse(404)
+               end
+
+               var local_file = (root / default_file).simplify_path
+               return answer_file(local_file)
+       end
+
        # Answer a 303 redirection to `location`.
        fun answer_redirection(location: String): HttpResponse do
                var response = new HttpResponse(303)