0001    // Server.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import TCP
0026    @_exported import HTTP
0027    
0028    public struct Server
Server.swift:52
extension Server {
Server.swift:53
    public func start(failure: ErrorType -> Void = Server.printError) throws {
Server.swift:105
    public func startInBackground(failure: ErrorType -> Void = Server.printError) {
: ServerType { 0029 public let server: StreamServerType 0030 public let parser: RequestParserType 0031 public let middleware: [MiddlewareType] 0032 public let responder: ResponderType 0033 public let serializer: ResponseSerializerType 0034 0035 public init
Server.swift:43
    public init(address: String? = nil, port: Int = 8080, reusePort: Bool = false, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., serializer: ResponseSerializerType = ResponseSerializer(), respond: Respond) throws {
(address: String? = nil, port: Int = 8080, reusePort: Bool = false, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., responder: ResponderType, serializer: ResponseSerializerType = ResponseSerializer()) throws { 0036 self.server = try TCPStreamServer(address: address, port: port, reusePort: reusePort) 0037 self.parser = parser 0038 self.middleware = middleware 0039 self.responder = responder 0040 self.serializer = serializer 0041 } 0042 0043 public init(address: String? = nil, port: Int = 8080, reusePort: Bool = false, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., serializer: ResponseSerializerType = ResponseSerializer(), respond: Respond) throws { 0044 self.server = try TCPStreamServer(address: address, port: port, reusePort: reusePort) 0045 self.parser = parser 0046 self.middleware = middleware 0047 self.responder = Responder(respond: respond) 0048 self.serializer = serializer 0049 } 0050 } 0051 0052 extension Server { 0053 public func start(failure: ErrorType -> Void = Server.printError) throws { 0054 while true { 0055 let stream = try server.accept() 0056 co { 0057 do { 0058 try self.processStream(stream) 0059 } catch { 0060 failure(error) 0061 } 0062 } 0063 } 0064 } 0065 0066 private func processStream(stream: StreamType) throws { 0067 while !stream.closed { 0068 do { 0069 let data = try stream.receive() 0070 if let request = try parser.parse(data) { 0071 var request = request 0072 request.ip = stream.ip 0073 0074 let response = try middleware.intercept(responder).respond(request) 0075 try serialize(response, stream: stream) 0076 0077 if let upgrade = response.upgrade { 0078 try upgrade(request, stream) 0079 stream.close() 0080 } 0081 0082 if !request.isKeepAlive { 0083 stream.close() 0084 break 0085 } 0086 } 0087 } catch StreamError.ClosedStream { 0088 break 0089 } catch { 0090 let response = Response(status: .InternalServerError) 0091 try serialize(response, stream: stream) 0092 throw error 0093 } 0094 } 0095 } 0096 0097 private func serialize(response: Response, stream: StreamType) throws { 0098 try serializer.serialize(response) { data in 0099 try stream.send(data) 0100 } 0101 0102 try stream.flush() 0103 } 0104 0105 public func startInBackground(failure: ErrorType -> Void = Server.printError) { 0106 co { 0107 do { 0108 try self.start() 0109 } catch { 0110 failure(error) 0111 } 0112 } 0113 } 0114 0115 private static func printError
Server.swift:53
    public func start(failure: ErrorType -> Void = Server.printError) throws {
Server.swift:105
    public func startInBackground(failure: ErrorType -> Void = Server.printError) {
(error: ErrorType) -> Void { 0116 print("Error: \(error)") 0117 } 0118 } 0119 0120 extension Request { 0121 public var ip: IP? { 0122 get { 0123 return storage["ip"] as? IP 0124 } 0125 0126 set { 0127 storage["ip"] = newValue 0128 } 0129 } 0130 }