0001    //
0002    //  Middeware2.swift.swift
0003    //  todoapi
0004    //
0005    //  Created by ito on 1/2/16.
0006    //  Copyright © 2016 Yusuke Ito. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    public protocol RouteWrap
Router.swift:18
public extension RouteWrap {
Router.swift:47
public struct Route: RouteWrap {
Router.swift:128
public struct Mount: RouteWrap {
: MiddlewareType { 0012 var inner
Router.swift:28
        let res = try inner.handleIfNeeded(ctx)
: MiddlewareType { get } 0013 func rewritePath
Router.swift:37
        let newPath = rewritePath(ctx.path)
(path: String) -> String 0014 func shouldHandle
Router.swift:23
        return shouldHandle(ctx, path: ctx.path)
(ctx: ContextBox, path: String) -> Bool 0015 func rewriteBefore
Router.swift:27
        rewriteBefore(ctx)
(ctx: ContextBox) 0016 } 0017 0018 public extension RouteWrap { 0019 func rewritePath(path: String) -> String { 0020 return path 0021 } 0022 func shouldHandle(ctx: ContextBox) -> Bool { 0023 return shouldHandle(ctx, path: ctx.path) 0024 } 0025 func handle(ctx: ContextBox) throws -> MiddlewareResult { 0026 let orig = ctx.path 0027 rewriteBefore(ctx) 0028 let res = try inner.handleIfNeeded(ctx) 0029 switch res { 0030 case .Next: 0031 ctx.path = orig 0032 default: break 0033 } 0034 return res 0035 } 0036 func rewriteBefore(ctx: ContextBox) { 0037 let newPath = rewritePath(ctx.path) 0038 if ctx.path == newPath { 0039 return 0040 } 0041 print("\(ctx.path) > \(newPath)(\(newPath.characters.count))") 0042 ctx.path = newPath 0043 return 0044 } 0045 } 0046 0047 public struct Route
Router.swift:172
        routes.append(Route(path, MethodMiddleware(methods: methods, handler: handler)))
: RouteWrap { 0048 0049 struct PathMatch
Router.swift:90
        case Match(PathMatch)
Router.swift:107
        if let match = PathMatch(path) {
{ 0050 let patts
Router.swift:56
            self.patts = pattern.characters.split("/").map(String.init)
Router.swift:57
            for p in patts {
Router.swift:63
            let keys:[String?] = patts.map{ $0.characters.count > 0 && $0.characters.first == Character(":") ? $0 : nil }
Router.swift:69
            if pathSlice.count != patts.count {
Router.swift:77
                    if pathSlice[i] != patts[i] {
: [String] // /:id/sub/:val -> :id, sub, :val 0051 let paramKeys
Router.swift:64
            self.paramKeys = keys.map({ $0.flatMap({ $0[$0.startIndex.advancedBy(1)..<$0.endIndex] }) }) // trim first ":" each key
Router.swift:74
                if let key = paramKeys[i] {
: [String?] // -> "id", nil, "val" 0052 init
Router.swift:107
        if let match = PathMatch(path) {
?(_ pattern: String) { 0053 if pattern.characters.contains(":") == false { 0054 return nil 0055 } 0056 self.patts = pattern.characters.split("/").map(String.init) 0057 for p in patts { 0058 if p.characters.count == 0 { 0059 print("invalid path pattern \(pattern)") 0060 return nil 0061 } 0062 } 0063 let keys:[String?] = patts.map{ $0.characters.count > 0 && $0.characters.first == Character(":") ? $0 : nil } 0064 self.paramKeys = keys.map({ $0.flatMap({ $0[$0.startIndex.advancedBy(1)..<$0.endIndex] }) }) // trim first ":" each key 0065 //print(patts, paramKeys) 0066 } 0067 func match
Router.swift:85
            return (match(path) as [String: String]).count > 0
Router.swift:120
            for (k, v) in match.match(path) {
(path: String) -> [String: String] { 0068 let pathSlice = path.characters.split("/").map(String.init) 0069 if pathSlice.count != patts.count { 0070 return [:] 0071 } 0072 var params:[String:String] = [:] 0073 for i in 0..<pathSlice.count { 0074 if let key = paramKeys[i] { 0075 params[key] = pathSlice[i] 0076 } else { 0077 if pathSlice[i] != patts[i] { 0078 return [:] 0079 } 0080 } 0081 } 0082 return params 0083 } 0084 func match
Router.swift:101
            return match.match(path)
(path: String) -> Bool { 0085 return (match(path) as [String: String]).count > 0 0086 } 0087 } 0088 0089 enum Pattern
Router.swift:93
    let pattern: Pattern
{ 0090 case Match
Router.swift:100
        case .Match(let match):
Router.swift:108
            self.pattern = .Match(match)
Router.swift:119
        case .Match(let match):
(PathMatch) 0091 case Path
Router.swift:98
        case .Path(let pat):
Router.swift:110
            self.pattern = .Path(path)
(String) 0092 } 0093 let pattern
Router.swift:97
        switch pattern {
Router.swift:108
            self.pattern = .Match(match)
Router.swift:110
            self.pattern = .Path(path)
Router.swift:118
        switch pattern {
: Pattern 0094 0095 public let inner
Router.swift:106
        self.inner = inner
: MiddlewareType 0096 public func shouldHandle(ctx: ContextBox, path: String) -> Bool { 0097 switch pattern { 0098 case .Path(let pat): 0099 return pat == path 0100 case .Match(let match): 0101 return match.match(path) 0102 } 0103 } 0104 0105 public init
Router.swift:172
        routes.append(Route(path, MethodMiddleware(methods: methods, handler: handler)))
(_ path: String, _ inner: MiddlewareType) { 0106 self.inner = inner 0107 if let match = PathMatch(path) { 0108 self.pattern = .Match(match) 0109 } else { 0110 self.pattern = .Path(path) 0111 } 0112 } 0113 public func rewriteBefore(ctx: ContextBox) { 0114 let path = ctx.path 0115 guard path.characters.count > 0 else { 0116 return 0117 } 0118 switch pattern { 0119 case .Match(let match): 0120 for (k, v) in match.match(path) { 0121 ctx.parameters[k] = v 0122 } 0123 default: break 0124 } 0125 } 0126 } 0127 0128 public struct Mount: RouteWrap { 0129 let prefix
Router.swift:133
        if path.hasPrefix(prefix) {
Router.swift:145
        newPath.replaceRange(prefix.startIndex..<prefix.startIndex.advancedBy(prefix.characters.count), with: "")
Router.swift:145
        newPath.replaceRange(prefix.startIndex..<prefix.startIndex.advancedBy(prefix.characters.count), with: "")
Router.swift:145
        newPath.replaceRange(prefix.startIndex..<prefix.startIndex.advancedBy(prefix.characters.count), with: "")
Router.swift:149
        self.prefix = prefix
: String 0130 public let inner
Router.swift:150
        self.inner = inner
: MiddlewareType 0131 0132 public func shouldHandle(ctx: ContextBox, path: String) -> Bool { 0133 if path.hasPrefix(prefix) { 0134 let newPath = rewritePath(path) 0135 // check that "/private" does not match "/privateeee" 0136 if newPath.characters.count == 0 || 0137 (newPath.characters.count > 0 && newPath.characters.first == Character("/")) { 0138 return true 0139 } 0140 } 0141 return false 0142 } 0143 public func rewritePath
Router.swift:134
            let newPath = rewritePath(path)
(path: String) -> String { 0144 var newPath = path 0145 newPath.replaceRange(prefix.startIndex..<prefix.startIndex.advancedBy(prefix.characters.count), with: "") 0146 return newPath 0147 } 0148 public init(_ prefix: String, _ inner: MiddlewareType) { 0149 self.prefix = prefix 0150 self.inner = inner 0151 } 0152 } 0153 0154 public class Router: MiddlewareType { 0155 var outer
Router.swift:173
        self.outer = compose(routes)
Router.swift:204
        return outer.shouldHandle(ctx)
Router.swift:207
        return try outer.handle(ctx)
: MiddlewareType = GenericMiddleware { ctx in .Next } 0156 var routes
Router.swift:172
        routes.append(Route(path, MethodMiddleware(methods: methods, handler: handler)))
Router.swift:173
        self.outer = compose(routes)
: [MiddlewareType] = [] 0157 0158 struct MethodMiddleware
Router.swift:172
        routes.append(Route(path, MethodMiddleware(methods: methods, handler: handler)))
: MethodHandleable, MiddlewareType { 0159 let methods: Set<Method> 0160 let handler
Router.swift:162
            return try handler(ctx)
: MiddlewareHandler 0161 func handle(ctx: ContextBox) throws -> MiddlewareResult { 0162 return try handler(ctx) 0163 } 0164 } 0165 0166 0167 public init() { 0168 0169 } 0170 0171 func handle
Router.swift:177
        handle(Set([
Router.swift:188
        handle(Set([.GET]), path: path, handler: handler)
Router.swift:192
        handle(Set([.POST]), path: path, handler: handler)
Router.swift:196
        handle(Set([.PUT]), path: path, handler: handler)
Router.swift:200
        handle(Set([.DELETE]), path: path, handler: handler)
(methods: Set<Method>, path: String, handler: MiddlewareHandler) { 0172 routes.append(Route(path, MethodMiddleware(methods: methods, handler: handler))) 0173 self.outer = compose(routes) 0174 } 0175 0176 public func all(path: String, _ handler: MiddlewareHandler) { 0177 handle(Set([ 0178 .DELETE, 0179 .GET, 0180 .HEAD, 0181 .POST, 0182 .PUT, 0183 .OPTIONS 0184 ]), path: path, handler: handler) 0185 } 0186 0187 public func get(path: String, _ handler: MiddlewareHandler) { 0188 handle(Set([.GET]), path: path, handler: handler) 0189 } 0190 0191 public func post(path: String, _ handler: MiddlewareHandler) { 0192 handle(Set([.POST]), path: path, handler: handler) 0193 } 0194 0195 public func put(path: String, _ handler: MiddlewareHandler) { 0196 handle(Set([.PUT]), path: path, handler: handler) 0197 } 0198 0199 public func delete(path: String, _ handler: MiddlewareHandler) { 0200 handle(Set([.DELETE]), path: path, handler: handler) 0201 } 0202 0203 public func shouldHandle(ctx: ContextBox) -> Bool { 0204 return outer.shouldHandle(ctx) 0205 } 0206 public func handle(ctx: ContextBox) throws -> MiddlewareResult { 0207 return try outer.handle(ctx) 0208 } 0209 } 0210 0211 0212 0213