0001 /* 0002 The MIT License (MIT) 0003 0004 Copyright (c) 2015 Shun Takebayashi 0005 0006 Permission is hereby granted, free of charge, to any person obtaining a copy 0007 of this software and associated documentation files (the "Software"), to deal 0008 in the Software without restriction, including without limitation the rights 0009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0010 copies of the Software, and to permit persons to whom the Software is 0011 furnished to do so, subject to the following conditions: 0012 0013 The above copyright notice and this permission notice shall be included in all 0014 copies or substantial portions of the Software. 0015 0016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0019 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0020 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0021 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0022 SOFTWARE. 0023 */ 0024 0025 protocol Matcher{ 0026 0027 func match
Matcher.swift:37 extension String: Matcher {Router.swift:30 var patterns = [(String, Matcher, Handler)]()Router.swift:32 func addPattern(method method: String, pattern: Matcher, handler: Handler) {(input: String) -> MatcherResult? 0028 0029 } 0030 0031 struct MatcherResult
Router.swift:39 if let result = entry.1.match(request.path) {{ 0032 0033 var params
Matcher.swift:27 func match(input: String) -> MatcherResult?Matcher.swift:39 func match(input: String) -> MatcherResult? {Matcher.swift:42 return MatcherResult()Matcher.swift:49 var result = MatcherResult()= [String: String]() 0034 0035 } 0036 0037 extension String: Matcher { 0038 0039 func match(input: String) -> MatcherResult? { 0040 // static routing 0041 if input == self { 0042 return MatcherResult() 0043 } 0044 // parameterized routing 0045 if self.characters.contains(":") { 0046 let inputComponents = input.characters.split("/") 0047 let matcherComponents = self.characters.split("/") 0048 if (inputComponents.count == matcherComponents.count) { 0049 var result = MatcherResult() 0050 for i in 0..<inputComponents.count { 0051 let ic = String(inputComponents[i]) 0052 let mc = String(matcherComponents[i]) 0053 if (mc[mc.startIndex] == ":") { 0054 let k = mc[mc.startIndex.advancedBy(1)..<mc.endIndex] 0055 result.params[k] = ic 0056 } 0057 else if ic != mc { 0058 return nil 0059 } 0060 } 0061 return result 0062 } 0063 } 0064 return nil 0065 } 0066 0067 } 0068
Matcher.swift:55 result.params[k] = icRouter.swift:40 if result.params.count > 0 {Router.swift:43 parameters: result.params