@@ -95,13 +95,25 @@ public struct Response: ResponseFieldsProvider {
95
95
*/
96
96
public final class Router {
97
97
98
- private typealias Route = ( method: HTTPMethod , handler: RouteHandler )
98
+ private class Route : Hashable {
99
+ let path : String
100
+ let method : HTTPMethod
101
+
102
+ init ( path: String , method: HTTPMethod ) {
103
+ self . path = path
104
+ self . method = method
105
+ }
106
+
107
+ private var hashValue : Int {
108
+ return path. hashValue ^ method. hashValue
109
+ }
110
+ }
99
111
100
112
private enum HTTPMethod : String {
101
113
case GET, POST, PUT, DELETE
102
114
}
103
115
104
- private var routes : [ String : Route ] = [ : ]
116
+ private var routes : [ Route : RouteHandler ] = [ : ]
105
117
106
118
/// The `baseURL` of the Router
107
119
public let baseURL : String
@@ -163,8 +175,8 @@ public final class Router {
163
175
guard let requestURL = request. URL
164
176
where requestURL. absoluteString? . containsString ( baseURL) ?? false else { return false }
165
177
166
- for (key, route ) in routes where route . method. rawValue == request. HTTPMethod {
167
- if matchRoute ( baseURL, path: key, requestURL: requestURL) != nil {
178
+ for (key, _ ) in routes where key . method. rawValue == request. HTTPMethod {
179
+ if matchRoute ( baseURL, path: key. path , requestURL: requestURL) != nil {
168
180
return true
169
181
}
170
182
}
@@ -180,14 +192,14 @@ public final class Router {
180
192
var headerFields : [ String : String ] ? = [ " Content-Type " : " application/json " ]
181
193
var serializableObject : Serializable ?
182
194
183
- for (key, route ) in routes {
184
- if let info = matchRoute ( baseURL, path: key, requestURL: requestURL) {
195
+ for (key, handler ) in routes where key . method . rawValue == server . request . HTTPMethod {
196
+ if let info = matchRoute ( baseURL, path: key. path , requestURL: requestURL) {
185
197
// If the request body is nil use `NSURLProtocol` property see swizzling in `NSMutableURLRequest.m`
186
198
// using a literal string because a bridging header in the podspec will be more problematic.
187
199
let dataBody = server. request. HTTPBody ?? NSURLProtocol . propertyForKey ( " kkp_requestHTTPBody " , inRequest: server. request) as? NSData
188
200
189
201
let request = Request ( components: info. components, queryParameters: info. queryParameters, HTTPBody: dataBody, HTTPHeaders: server. request. allHTTPHeaderFields)
190
- serializableObject = route . handler ( request)
202
+ serializableObject = handler ( request)
191
203
break
192
204
}
193
205
}
@@ -217,6 +229,10 @@ public final class Router {
217
229
}
218
230
}
219
231
}
232
+
233
+ private func addRoute( with path: String , method: HTTPMethod , handler: RouteHandler ) {
234
+ routes [ Route ( path: path, method: method) ] = handler
235
+ }
220
236
221
237
/**
222
238
Registers a GET request with the given path.
@@ -237,7 +253,7 @@ public final class Router {
237
253
- parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request
238
254
*/
239
255
public func get( path: String , handler: RouteHandler ) {
240
- routes [ path] = ( . GET, handler)
256
+ addRoute ( with : path, method : . GET, handler : handler)
241
257
}
242
258
243
259
/**
@@ -259,7 +275,7 @@ public final class Router {
259
275
- parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request
260
276
*/
261
277
public func post( path: String , handler: RouteHandler ) {
262
- routes [ path] = ( . POST, handler)
278
+ addRoute ( with : path, method : . POST, handler : handler)
263
279
}
264
280
265
281
/**
@@ -281,7 +297,7 @@ public final class Router {
281
297
- parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request
282
298
*/
283
299
public func del( path: String , handler: RouteHandler ) {
284
- routes [ path] = ( . DELETE, handler)
300
+ addRoute ( with : path, method : . DELETE, handler : handler)
285
301
}
286
302
287
303
/**
@@ -303,7 +319,11 @@ public final class Router {
303
319
- parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request
304
320
*/
305
321
public func put( path: String , handler: RouteHandler ) {
306
- routes [ path] = ( . PUT, handler)
322
+ addRoute ( with : path, method : . PUT, handler : handler)
307
323
}
308
324
309
325
}
326
+
327
+ private func == ( lhs: Router . Route , rhs: Router . Route ) -> Bool {
328
+ return lhs. path == rhs. path && lhs. method == rhs. method
329
+ }
0 commit comments