0001 // RegexRouteMatcher.swift 0002 // 0003 // The MIT License (MIT) 0004 // 0005 // Copyright (c) 2015 Zewo 0006 // 0007 // Permission is hereby granted, free of charge, to any person obtaining a copy 0008 // of this software and associated documentation files (the "Software"), to deal 0009 // in the Software without restriction, including without limitation the rights 0010 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0011 // copies of the Software, and to permit persons to whom the Software is 0012 // furnished to do so, subject to the following conditions: 0013 // 0014 // The above copyright notice and this permission notice shall be included in all 0015 // copies or substantial portions of the Software. 0016 // 0017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0022 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0023 // SOFTWARE. 0024 0025 @_exported import HTTP 0026 @_exported import POSIXRegex 0027 0028 public struct RegexRouteMatcher: RouteMatcherType { 0029 public let routes: [Route] 0030 public let regexRoutes: [(Route, RegexRoute)] 0031 0032 public init(routes: [Route]) { 0033 self.regexRoutes = routes.map { 0034 let regexRoute = RegexRoute(route: $0) 0035 let route = Route( 0036 methods: regexRoute.route.methods, 0037 path: regexRoute.route.path, 0038 middleware: regexRoute.route.middleware, 0039 responder: regexRoute 0040 ) 0041 return (route, regexRoute) 0042 } 0043 self.routes = regexRoutes.map({$0.0}) 0044 } 0045 0046 public func match(request: Request) -> Route? { 0047 for (route, regexRoute) in regexRoutes where regexRoute.matches(request) { 0048 return route 0049 } 0050 return nil 0051 } 0052 } 0053 0054 public struct RegexRoute: ResponderType { 0055 public let route: Route 0056 private let parameterKeys: [String] 0057 private let regularExpression: Regex 0058 0059 public init(route: Route) { 0060 self.route = route 0061 0062 let parameterRegularExpression = try! Regex(pattern: ":([[:alnum:]]+)") 0063 let pattern = parameterRegularExpression.replace(route.path, withTemplate: "([[:alnum:]_-]+)") 0064 0065 self.parameterKeys = parameterRegularExpression.groups(route.path) 0066 self.regularExpression = try! Regex(pattern: "^" + pattern + "$") 0067 } 0068 0069 public func matches(request: Request) -> Bool { 0070 return regularExpression.matches(request.uri.path!) && route.methods.contains(request.method) 0071 } 0072 0073 public func respond(request: Request) throws -> Response { 0074 var request = request 0075 0076 guard let path = request.path else { 0077 return Response(status: .BadRequest) 0078 } 0079 0080 let values = regularExpression.groups(path) 0081 0082 request.pathParameters = [:] 0083 0084 for (index, key) in parameterKeys.enumerate() { 0085 request.pathParameters[key] = values[index] 0086 } 0087 0088 return try route.responder.respond(request) 0089 } 0090 } 0091
RegexRouteMatcher.swift:84 for (index, key) in parameterKeys.enumerate() {