mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-28 05:14:27 +00:00
78 lines
2.7 KiB
Swift
78 lines
2.7 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 Logging
|
|
import NIO
|
|
import NIOIMAP
|
|
@_spi(NIOIMAPInternal) import NIOIMAPCore
|
|
import NIOSSL
|
|
|
|
enum CommandRoundtripError: Error {
|
|
case incompleteCommand
|
|
}
|
|
|
|
public class CommandRoundtripHandler: ChannelOutboundHandler {
|
|
public typealias OutboundIn = ByteBuffer
|
|
public typealias OutboundOut = ByteBuffer
|
|
|
|
let logger: Logger
|
|
private var parser = CommandParser()
|
|
public var capabilities: [Capability] = []
|
|
|
|
public init(logger: Logger) {
|
|
self.logger = logger
|
|
}
|
|
|
|
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
var originalBuffer = self.unwrapOutboundIn(data)
|
|
do {
|
|
var originalBufferCopy = originalBuffer
|
|
guard let commandStream = try parser.parseCommandStream(buffer: &originalBufferCopy),
|
|
let command = commandStream.commandPart
|
|
else {
|
|
// this is fine because the command is input by the user, so *should* be valid
|
|
throw CommandRoundtripError.incompleteCommand
|
|
}
|
|
|
|
var encodeBuffer = CommandEncodeBuffer(
|
|
buffer: context.channel.allocator.buffer(capacity: originalBuffer.readableBytes),
|
|
capabilities: self.capabilities,
|
|
loggingMode: false
|
|
)
|
|
encodeBuffer.writeCommandStream(command)
|
|
var roundtripBuffer = encodeBuffer.buffer.nextChunk().bytes
|
|
|
|
if originalBuffer != roundtripBuffer {
|
|
self.logger.warning("Input command vs roundtrip output is different")
|
|
self.logger.warning(
|
|
"Command (original):\n\(originalBuffer.readString(length: originalBuffer.readableBytes)!)"
|
|
)
|
|
self.logger.warning(
|
|
"Command (roundtrip):\n\(roundtripBuffer.readString(length: roundtripBuffer.readableBytes)!)"
|
|
)
|
|
}
|
|
|
|
context.write(data, promise: promise)
|
|
} catch {
|
|
promise?.fail(error)
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// `CommandRoundtripHandler` holds mutable state and is confined to a single
|
|
// `EventLoop`, so it is deliberately non-`Sendable`.
|
|
@available(*, unavailable)
|
|
extension CommandRoundtripHandler: Sendable {}
|