0001 // 0002 // Branch.swift 0003 // HelloServer 0004 // 0005 // Created by Logan Wright on 2/15/16. 0006 // Copyright © 2016 LoganWright. All rights reserved. 0007 // 0008 0009 /** 0010 When routing requests, different branches will be established, 0011 in a linked list style stemming from their host and request method. 0012 It can be represented as: 0013 0014 | host | request.method | branch -> branch -> branch 0015 */ 0016 internal final class Branch{ 0017 0018 /** 0019 The name of the branch, ie if we have a path hello/:name, 0020 the branch structure will be: 0021 Branch('hello') (connected to) Branch('name') 0022 0023 In cases where a slug is used, ie ':name' the slug 0024 will be used as the name and passed as a key in matching. 0025 */ 0026 let name
Branch.swift:48 private(set) var subBranches: [String : Branch] = [:]Branch.swift:109 let next = subBranches["*"] ?? Branch(name: substring)Branch.swift:113 let next = subBranches[key] ?? Branch(name: key)BranchRouter.swift:16 private final var tree: [Host : [Request.Method : Branch]] = [:]BranchRouter.swift:45 let branch = root[method] ?? Branch(name: ""): String 0027 0028 /** 0029 There are two types of branches, those that support a handler, 0030 and those that are a linker between branches, 0031 for example /users/messages/:id will have 3 connected branches, 0032 only one of which supports a handler. 0033 0034 Branch('users') -> Branch('messages') -> *Branches('id') 0035 0036 *indicates a supported branch. 0037 */ 0038 private var handler
Branch.swift:59 self.name = nameBranch.swift:80 request.parameters[wildcard.name] = key.stringByRemovingPercentEncoding: Request.Handler? 0039 0040 /** 0041 key or * 0042 0043 If it is a `key`, then it connects to an additional branch. 0044 0045 If it is `*`, it is a slug point and the name 0046 represents a key for a dynamic value. 0047 */ 0048 private(set) var subBranches
Branch.swift:60 self.handler = handlerBranch.swift:74 return handlerBranch.swift:99 self.handler = handler: [String : Branch] = [:] 0049 0050 /** 0051 Used to create a new branch 0052 0053 - parameter name: The name associated with the branch, or the key when dealing with a slug 0054 - parameter handler: The handler to be called if its a valid endpoint, or `nil` if this is a bridging branch 0055 0056 - returns: an initialized request Branch 0057 */ 0058 init
Branch.swift:77 if let next = subBranches[key] {Branch.swift:79 } else if let wildcard = subBranches["*"] {Branch.swift:109 let next = subBranches["*"] ?? Branch(name: substring)Branch.swift:111 subBranches["*"] = nextBranch.swift:113 let next = subBranches[key] ?? Branch(name: key)Branch.swift:115 subBranches[key] = next(name: String, handler: Request.Handler? = nil) { 0059 self.name = name 0060 self.handler = handler 0061 } 0062 0063 /** 0064 This function will recursively traverse the branch 0065 until the path is fulfilled or the branch ends 0066 0067 - parameter request: the request to use in matching 0068 - parameter comps: ordered pathway components generator 0069 0070 - returns: a request handler or nil if not supported 0071 */ 0072 func handle
Branch.swift:109 let next = subBranches["*"] ?? Branch(name: substring)Branch.swift:113 let next = subBranches[key] ?? Branch(name: key)BranchRouter.swift:45 let branch = root[method] ?? Branch(name: "")(request: Request, comps: CompatibilityGenerator<String>) -> Request.Handler? { 0073 guard let key = comps.next() else { 0074 return handler 0075 } 0076 0077 if let next = subBranches[key] { 0078 return next.handle(request, comps: comps) 0079 } else if let wildcard = subBranches["*"] { 0080 request.parameters[wildcard.name] = key.stringByRemovingPercentEncoding 0081 return wildcard.handle(request, comps: comps) 0082 } else { 0083 return nil 0084 } 0085 } 0086 0087 /** 0088 If a branch exists that is linked as: 0089 0090 Branch('one') -> Branch('two') 0091 0092 This branch will be extended with the given value 0093 0094 - parameter generator: the generator that will be used to match the path components. /users/messages/:id will return a generator that is 'users' <- 'messages' <- '*id' 0095 - parameter handler: the handler to assign to the end path component 0096 */ 0097 func extendBranch
Branch.swift:78 return next.handle(request, comps: comps)Branch.swift:81 return wildcard.handle(request, comps: comps)BranchRouter.swift:31 return branch.handle(request, comps: generator)(generator: CompatibilityGenerator<String>, handler: Request.Handler) { 0098 guard let key = generator.next() else { 0099 self.handler = handler 0100 return 0101 } 0102 0103 if key.hasPrefix(":") { 0104 let chars = key.characters 0105 let indexOne = chars.startIndex.advancedBy(1) 0106 let sub = key.characters.suffixFrom(indexOne) 0107 let substring = String(sub) 0108 0109 let next = subBranches["*"] ?? Branch(name: substring) 0110 next.extendBranch(generator, handler: handler) 0111 subBranches["*"] = next 0112 } else { 0113 let next = subBranches[key] ?? Branch(name: key) 0114 next.extendBranch(generator, handler: handler) 0115 subBranches[key] = next 0116 } 0117 } 0118 } 0119
Branch.swift:110 next.extendBranch(generator, handler: handler)Branch.swift:114 next.extendBranch(generator, handler: handler)BranchRouter.swift:48 branch.extendBranch(generator, handler: handler)