Skip to content

Commit

Permalink
Fixed not matching routes correctly because of the trailing slash /
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Jul 30, 2017
1 parent a440df4 commit e21c9b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 3 additions & 4 deletions Sources/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -69,5 +70,3 @@ extension String {
return "Content-Type"
}
}


15 changes: 14 additions & 1 deletion Sources/Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "\\{(.+?)\\}")
Expand All @@ -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))
}
}

0 comments on commit e21c9b8

Please sign in to comment.