|
@@ -80,6 +80,13 @@ public class HttpRouter {
|
|
|
|
|
|
|
|
private func findHandler(_ node: inout Node, params: inout [String: String], generator: inout IndexingIterator<[String]>) -> ((HttpRequest) -> HttpResponse)? {
|
|
private func findHandler(_ node: inout Node, params: inout [String: String], generator: inout IndexingIterator<[String]>) -> ((HttpRequest) -> HttpResponse)? {
|
|
|
guard let pathToken = generator.next() else {
|
|
guard let pathToken = generator.next() else {
|
|
|
|
|
+ // if it's the last element of the requested URL, check if there is a pattern with variable tail.
|
|
|
|
|
+ if let variableNode = node.nodes.filter({ $0.0.characters.first == ":" }).first {
|
|
|
|
|
+ if variableNode.value.nodes.isEmpty {
|
|
|
|
|
+ params[variableNode.0] = ""
|
|
|
|
|
+ return variableNode.value.handler
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
return node.handler
|
|
return node.handler
|
|
|
}
|
|
}
|
|
|
let variableNodes = node.nodes.filter { $0.0.characters.first == ":" }
|
|
let variableNodes = node.nodes.filter { $0.0.characters.first == ":" }
|
|
@@ -88,7 +95,7 @@ public class HttpRouter {
|
|
|
// if it's the last element of the pattern and it's a variable, stop the search and
|
|
// if it's the last element of the pattern and it's a variable, stop the search and
|
|
|
// append a tail as a value for the variable.
|
|
// append a tail as a value for the variable.
|
|
|
let tail = generator.joined(separator: "/")
|
|
let tail = generator.joined(separator: "/")
|
|
|
- if tail.utf8.count > 0 {
|
|
|
|
|
|
|
+ if tail.characters.count > 0 {
|
|
|
params[variableNode.0] = pathToken + "/" + tail
|
|
params[variableNode.0] = pathToken + "/" + tail
|
|
|
} else {
|
|
} else {
|
|
|
params[variableNode.0] = pathToken
|
|
params[variableNode.0] = pathToken
|