nitcorn: fix path resolution for vararg routes
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 20 Apr 2016 04:53:47 +0000 (00:53 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 20 Apr 2016 04:53:47 +0000 (00:53 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/nitcorn/reactor.nit
lib/nitcorn/vararg_routes.nit

index e888361..88d1cbd 100644 (file)
@@ -72,7 +72,7 @@ class HttpServer
                                request.uri_params = route.parse_params(request.uri)
 
                                var handler = route.handler
-                               var root = route.path
+                               var root = route.resolve_path(request)
                                var turi
                                if root != null then
                                        turi = ("/" + request.uri.substring_from(root.length)).simplify_path
index 9099284..f1a1630 100644 (file)
@@ -108,6 +108,20 @@ redef class Route
                parse_pattern(path)
        end
 
+       # Replace `self.path` parameters with concrete values from the `request` URI.
+       fun resolve_path(request: HttpRequest): nullable String do
+               if pattern_parts.is_empty then return self.path
+               var path = "/"
+               for part in pattern_parts do
+                       if part isa UriString then
+                               path /= part.string
+                       else if part isa UriParam then
+                               path /= request.param(part.name) or else part.name
+                       end
+               end
+               return path
+       end
+
        # Cut `path` into `UriParts`.
        private fun parse_pattern(path: nullable String) do
                if path == null then return