From e21c9b82908873b5e2adf93ef0a427aa615b0e46 Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Sun, 30 Jul 2017 12:41:07 +0400 Subject: [PATCH] Fixed not matching routes correctly because of the trailing slash `/` --- Sources/Request.swift | 7 +++---- Sources/Route.swift | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/Request.swift b/Sources/Request.swift index b519ea9..7925bd7 100644 --- a/Sources/Request.swift +++ b/Sources/Request.swift @@ -34,7 +34,7 @@ open class Request { public init(method: String, fullPath: String, version: String, headers: HeaderDictionary, body: [Byte]) { self.method = method - self.fullPath = fullPath + self.fullPath = fullPath.trimmedRoute self.version = version self.headers = headers self.body = body @@ -49,13 +49,14 @@ open class Request { if params.count > 0 { if params.count != route.paramNames.count { // this should not happen actually return false - } + } zip(route.paramNames, params).forEach { routeParams[$0] = Url.unescape($1) } return true } + return self.path == route.path } } @@ -69,5 +70,3 @@ extension String { return "Content-Type" } } - - diff --git a/Sources/Route.swift b/Sources/Route.swift index 4271c77..3173cda 100644 --- a/Sources/Route.swift +++ b/Sources/Route.swift @@ -17,7 +17,8 @@ open class Route { public init(method: String, path: String, handler: @escaping RouteHandler) { self.method = method - self.path = "/".appendingPathComponent(path) + let path = path.trimmedRoute + self.path = path self.handler = handler self.paramNames = try! Regex.matches(path, pattern: "\\{(.+?)\\}") @@ -36,3 +37,15 @@ open class Route { return self } } + +extension CharacterSet { + static var slashes: CharacterSet { + return CharacterSet.init(charactersIn: "/") + } +} + +extension String { + var trimmedRoute: String { + return "/".appendingPathComponent(self.trimmingCharacters(in: .slashes)) + } +}