-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed NSRegularExpression dependency from HttpServer.
Introduced a first version of new routing mechanism ( ~Sinatra ).
- Loading branch information
1 parent
e8acc49
commit df24ced
Showing
10 changed files
with
206 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// HttpRouter.swift | ||
// Swifter | ||
// Copyright (c) 2015 Damian Kołakowski. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class HttpRouter { | ||
|
||
private var handlers: [(pattern: [String], handler: HttpServer.Handler)] = [] | ||
|
||
public func routes() -> [String] { | ||
return handlers.map { $0.pattern.joinWithSeparator("/") } | ||
} | ||
|
||
public func register(path: String, handler: HttpServer.Handler) { | ||
handlers.append((path.split("/"), handler)); | ||
handlers.sortInPlace { $0.0.pattern.count < $0.1.pattern.count } | ||
} | ||
|
||
public func select(url:String) -> ([String:String], HttpServer.Handler)? { | ||
let urlTokens = url.split("/") | ||
for (pattern, handler) in handlers { | ||
if let params = matchParams(pattern, valueTokens: urlTokens) { | ||
return (params, handler) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
public func matchParams(patternTokens: [String], valueTokens: [String]) -> [String: String]? { | ||
var params = [String: String]() | ||
for index in 0..<valueTokens.count { | ||
if index >= patternTokens.count { | ||
return nil | ||
} | ||
let patternToken = patternTokens[index] | ||
let valueToken = valueTokens[index] | ||
if patternToken.isEmpty { | ||
if patternToken != valueToken { | ||
return nil | ||
} | ||
} | ||
if patternToken.characters.first == ":" { | ||
params[patternToken.substringFromIndex(patternToken.characters.startIndex.successor())] = valueToken | ||
} else { | ||
if patternToken != valueToken { | ||
return nil | ||
} | ||
} | ||
} | ||
return params | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+7.01 KB
(100%)
...roject.xcworkspace/xcuserdata/damiankolakowski.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Oops, something went wrong.