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 TCPSSL 0026 @_exported import HTTP 0027 0028 public struct Server: 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:64 extension Server {Server_.swift:65 public func start(failure: ErrorType -> Void = Server.printError) throws {Server_.swift:117 public func startInBackground(failure: ErrorType -> Void = Server.printError) {(address: String? = nil, port: Int = 8080, certificate: String, privateKey: String, certificateChain: String? = nil, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., responder: ResponderType, serializer: ResponseSerializerType = ResponseSerializer()) throws { 0036 self.server = try TCPSSLStreamServer( 0037 address: address, 0038 port: port, 0039 certificate: certificate, 0040 privateKey: privateKey, 0041 certificateChain: certificateChain 0042 ) 0043 self.parser = parser 0044 self.middleware = middleware 0045 self.responder = responder 0046 self.serializer = serializer 0047 } 0048 0049 public init(address: String? = nil, port: Int = 8080, certificate: String, privateKey: String, certificateChain: String? = nil, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., serializer: ResponseSerializerType = ResponseSerializer(), respond: Respond) throws { 0050 self.server = try TCPSSLStreamServer( 0051 address: address, 0052 port: port, 0053 certificate: certificate, 0054 privateKey: privateKey, 0055 certificateChain: certificateChain 0056 ) 0057 self.parser = parser 0058 self.middleware = middleware 0059 self.responder = Responder(respond: respond) 0060 self.serializer = serializer 0061 } 0062 } 0063 0064 extension Server { 0065 public func start(failure: ErrorType -> Void = Server.printError) throws { 0066 while true { 0067 let stream = try server.accept() 0068 co { 0069 do { 0070 try self.processStream(stream) 0071 } catch { 0072 failure(error) 0073 } 0074 } 0075 } 0076 } 0077 0078 private func processStream(stream: StreamType) throws { 0079 while !stream.closed { 0080 do { 0081 let data = try stream.receive() 0082 if let request = try parser.parse(data) { 0083 var request = request 0084 request.ip = stream.ip 0085 0086 let response = try middleware.intercept(responder).respond(request) 0087 try serialize(response, stream: stream) 0088 0089 if let upgrade = response.upgrade { 0090 try upgrade(request, stream) 0091 stream.close() 0092 } 0093 0094 if !request.isKeepAlive { 0095 stream.close() 0096 break 0097 } 0098 } 0099 } catch StreamError.ClosedStream { 0100 break 0101 } catch { 0102 let response = Response(status: .InternalServerError) 0103 try serialize(response, stream: stream) 0104 throw error 0105 } 0106 } 0107 } 0108 0109 private func serialize(response: Response, stream: StreamType) throws { 0110 try serializer.serialize(response) { data in 0111 try stream.send(data) 0112 } 0113 0114 try stream.flush() 0115 } 0116 0117 public func startInBackground(failure: ErrorType -> Void = Server.printError) { 0118 co { 0119 do { 0120 try self.start() 0121 } catch { 0122 failure(error) 0123 } 0124 } 0125 } 0126 0127 private static func printError
Server_.swift:49 public init(address: String? = nil, port: Int = 8080, certificate: String, privateKey: String, certificateChain: String? = nil, parser: RequestParserType = RequestParser(), middleware: MiddlewareType..., serializer: ResponseSerializerType = ResponseSerializer(), respond: Respond) throws {(error: ErrorType) -> Void { 0128 print("Error: \(error)") 0129 } 0130 } 0131 0132 extension Request { 0133 public var ip: IP? { 0134 get { 0135 return storage["ip"] as? IP 0136 } 0137 0138 set { 0139 storage["ip"] = newValue 0140 } 0141 } 0142 }
Server_.swift:65 public func start(failure: ErrorType -> Void = Server.printError) throws {Server_.swift:117 public func startInBackground(failure: ErrorType -> Void = Server.printError) {