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)) + } +}