nitcorn/file_server: extract answer_redirection method
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 23 May 2016 19:14:17 +0000 (15:14 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 26 May 2016 21:47:35 +0000 (17:47 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/nitcorn/file_server.nit

index b701aa9..0245d1f 100644 (file)
@@ -90,11 +90,9 @@ class FileServer
                                if local_file.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
@@ -126,6 +124,13 @@ class FileServer
                return response
        end
 
+       # Answer a 303 redirection to `location`.
+       fun answer_redirection(location: String): HttpResponse do
+               var response = new HttpResponse(303)
+               response.header["Location"] = location
+               return response
+       end
+
        # Build a reponse containing a single `local_file`.
        #
        # Returns a 404 error if local_file does not exists.