Files
swift-nio-imap/Sources/Proxy/ProxyToMailServerHandler.swift
T
David Evans d7b4b5226d Rename ResponseStream to Response (#56)
* Rename ResponseStream to Response

* Fix conflicts
2020-04-17 18:25:17 +01:00

49 lines
1.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
import NIOIMAP
class ProxyToMailServerHandler: ChannelInboundHandler {
typealias InboundIn = NIOIMAP.Response
let mailAppToProxyChannel: Channel
var parser = NIOIMAP.ResponseParser()
init(mailAppToProxyChannel: Channel) {
self.mailAppToProxyChannel = mailAppToProxyChannel
}
func channelActive(context: ChannelHandlerContext) {
self.mailAppToProxyChannel.closeFuture.whenSuccess {
context.close(promise: nil)
}
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let stream = self.unwrapInboundIn(data)
var buffer = context.channel.allocator.buffer(capacity: 1024)
buffer.writeResponse(stream)
self.mailAppToProxyChannel.writeAndFlush(buffer, promise: nil)
}
func errorCaught(context: ChannelHandlerContext, error: Error) {
print("ERROR: \(error)")
context.channel.close(promise: nil)
context.fireErrorCaught(error)
}
}