0001    // SSLServerStream.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, INCLUDINbG 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    import COpenSSL
0026    @_exported import Stream
0027    
0028    public final class SSLServerStream: StreamType {
0029        private(set) public var metadata: [String: Any] = [:]
0030    	private let context: SSLServerContext
0031        private let rawStream: StreamType
0032        private let readIO: IO
0033        private let writeIO: IO
0034    	private let ssl
ServerStream.swift:58
        while !ssl.initializationFinished {
ServerStream.swift:60
                try ssl.handshake()
: Session 0035 0036 public var closed: Bool = false 0037 0038 public init(context: SSLServerContext, rawStream: StreamType) throws { 0039 OpenSSL.initialize() 0040 0041 metadata = rawStream.metadata 0042 0043 self.context = context 0044 self.rawStream = rawStream 0045 0046 readIO = try IO(method: .Memory) 0047 writeIO = try IO(method: .Memory) 0048 0049 ssl = try Session(context: context) 0050 ssl.setIO(readIO: readIO, writeIO: writeIO) 0051 ssl.setAcceptState() 0052 } 0053 0054 public func receive() throws -> Data { 0055 let data = try rawStream.receive() 0056 try readIO.write(data) 0057 0058 while !ssl.initializationFinished { 0059 do { 0060 try ssl.handshake() 0061 } catch Session.Error.WantRead {} 0062 try send() 0063 try rawStream.flush() 0064 let data = try rawStream.receive() 0065 try readIO.write(data) 0066 } 0067 0068 var decriptedData = Data() 0069 0070 while true { 0071 do { 0072 decriptedData += try ssl.read() 0073 } catch Session.Error.WantRead { 0074 if decriptedData.count > 0 { 0075 return decriptedData 0076 } 0077 let data = try rawStream.receive() 0078 try readIO.write(data) 0079 } 0080 } 0081 } 0082 0083 public func send(data: Data) throws { 0084 ssl.write(data) 0085 try send() 0086 } 0087 0088 public func flush() throws { 0089 try rawStream.flush() 0090 } 0091 0092 public func close() -> Bool { 0093 return rawStream.close() 0094 } 0095 0096 private func send() throws { 0097 do { 0098 let data = try writeIO.read() 0099 try rawStream.send(data) 0100 } catch IO.Error.ShouldRetry { 0101 return 0102 } 0103 } 0104 } 0105