Files
swift-nio-imap/Sources/Proxy/ProxyToMailServerHandler.swift
T
David EvansandCory Benfield 027494d589 Fix response streaming. (#4)
* Start response streaming

* Fix the tests

* It's fixed

* Fix parsing response data

* Fix typo

Co-Authored-By: Cory Benfield <lukasa@apple.com>

* Fix CLI

* Fix buffer

* Uncomment tests

* Uncomment tests

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-09 13:01:46 +00: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.ResponseStream
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.writeResponseStream(stream)
self.mailAppToProxyChannel.writeAndFlush(buffer, promise: nil)
}
func errorCaught(context: ChannelHandlerContext, error: Error) {
print("ERROR: \(error)")
context.channel.close(promise: nil)
context.fireErrorCaught(error)
}
}