Files
swift-nio-imap/Sources/NIOIMAP/Coders/IMAPServerHandler.swift
T
Daniel Eggert d309926c18 Deliver received literals zero-copy via IMAPInputProcessor
The handlers stage reads in an IMAPInputBuffer (IMAPInputProcessor)
instead of NIOSingleStepByteToMessageProcessor's coalesced ByteBuffer,
so a FETCH/APPEND literal piece of at least 1 KiB within one read of
at least 1 KiB is delivered as a reference to the received bytes.
Shorter pieces are copied; bytes from smaller reads are copied twice,
into a tail on arrival and out of it on delivery. The decoders keep
their ByteBuffer entry point for ByteToMessageHandler and the decoder
verifier; that path copies.

A decoder error or PayloadTooLargeError is final: the buffer is dropped
and later reads rethrow it without decoding (ByteToMessageHandler also
stops decoding, but fires dataReceivedInErrorState). Keeping the
unparsed bytes would make every later read append, fail on the same
bytes and copy all of them into IMAPDecoderError.buffer: unbounded
memory and quadratic copying, past the maximumBufferSize check. Errors
thrown by the message receiver (e.g. UnexpectedResponse) pass through
and are not final.

Reads under 1 KiB are copied into IMAPInputBuffer's tails, so 1-byte
reads cost a region per KiB and pin no receive buffers. A larger read
still pins its whole receive buffer, which maximumBufferSize does not
count.

Review focus: IMAPInputProcessor.

Behavior changes:
- A stream whose input failed to parse stays failed.
- IMAPDecoderError.buffer holds the bytes waiting to be parsed when the
  error was thrown.
- Leftover literal bytes followed by a read that starts a new region
  reach the handler's consumer as two .streamingBytes events, not
  one. Every read of at least 1 KiB starts a region; a smaller one
  does when the last region is a retained read, or a tail it does not
  fit or that is not uniquely referenced.
  ParseInput.extractBuffer(upTo:) takes bytes from the current region
  only, where the old code took min(remaining, readable) across the
  whole buffer. Legal, but observable. A caller passing a multi-region
  buffer to the stream parsers directly sees this from the previous
  commit on.
2026-11-25 21:19:40 +09:00

157 lines
6.2 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 NIOIMAPCore
public struct ReceivedInvalidFrame: Hashable, Sendable {
public var frame: ByteBuffer
public init(frame: ByteBuffer) {
self.frame = frame
}
}
public struct ReceivedIncompleteFrame: Hashable, Sendable {
public var requiredBytes: Int
public init(requiredBytes: Int) {
self.requiredBytes = requiredBytes
}
}
public final class IMAPServerHandler: ChannelDuplexHandler {
public typealias InboundIn = FramingResult
public typealias InboundOut = CommandStreamPart
public typealias OutboundIn = Response
public typealias OutboundOut = ByteBuffer
private var _continuationRequest: ContinuationRequest
public var continuationRequest: ContinuationRequest {
get {
self._continuationRequest
}
set {
self._continuationRequest = newValue
self.continuationRequestBytes = Self.encode(newValue, capabilities: self.capabilities)
}
}
/// Received bytes, parsed where they landed rather than coalesced into one buffer first.
private let decoder: IMAPInputProcessor<CommandDecoder>
private var numberOfOutstandingContinuationRequests = 0
/// The encoded continuation request, written once per outstanding request.
///
/// Coalesced into one `ByteBuffer` rather than kept as regions, because it is a handful of bytes
/// that gets written many times over the life of the connection.
private var continuationRequestBytes: ByteBuffer
/// The buffer every response is encoded into, kept for the lifetime of the connection.
///
/// Default sink, `IMAPOutputBuffer`, on purpose: a sink handing the channel slices of one
/// `ByteBuffer` rather than copies allocates a fresh `ByteBuffer` per chunk while earlier ones are
/// still in flight, and is slower on a stream of small responses.
private var responseEncodeBuffer = ResponseEncodeBuffer(
options: .init(),
loggingMode: false
)
public var capabilities: [Capability] = []
public init(
continuationRequest: ContinuationRequest = .responseText(ResponseText(text: "OK")),
literalSizeLimit: Int = IMAPDefaults.literalSizeLimit
) {
self.decoder = IMAPInputProcessor(
CommandDecoder(literalSizeLimit: literalSizeLimit),
maximumBufferSize: IMAPDefaults.lineLengthLimit
)
self._continuationRequest = continuationRequest
self.continuationRequestBytes = Self.encode(continuationRequest, capabilities: self.capabilities)
}
/// Encodes one continuation request into a single `ByteBuffer`.
private static func encode(_ request: ContinuationRequest, capabilities: [Capability]) -> ByteBuffer {
var encodeBuffer = ResponseEncodeBuffer(capabilities: capabilities, loggingMode: false)
encodeBuffer.writeContinuationRequest(request)
return encodeBuffer.readBytes().makeByteBuffer()
}
public func read(context: ChannelHandlerContext) {
defer {
context.read()
}
let outstanding = self.numberOfOutstandingContinuationRequests
guard outstanding != 0 else { return }
for _ in 0..<outstanding {
context.write(self.wrapOutboundOut(self.continuationRequestBytes), promise: nil)
}
self.numberOfOutstandingContinuationRequests = 0
context.flush()
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let frame = self.unwrapInboundIn(data)
switch frame {
case .complete(let buffer), .insideLiteral(let buffer, remainingBytes: _):
do {
try self.decoder.process(buffer: buffer) { command in
self.numberOfOutstandingContinuationRequests += command.numberOfSynchronisingLiterals
if let command = command.commandPart {
context.fireChannelRead(self.wrapInboundOut(command))
}
}
} catch {
context.fireErrorCaught(error)
}
case .incomplete(let requiredBytes):
context.fireUserInboundEventTriggered(ReceivedIncompleteFrame(requiredBytes: requiredBytes))
case .invalid(let buffer):
context.fireUserInboundEventTriggered(ReceivedInvalidFrame(frame: buffer))
}
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let response = self.unwrapOutboundIn(data)
self.responseEncodeBuffer.writeResponse(response)
let bytes = self.responseEncodeBuffer.readBytes()
// Usually one write. Several when the response carries a payload the encoder kept by reference —
// a streamed literal — so that the payload reaches the network without being copied. See
// `IMAPBufferList.makeOutboundBuffers()`.
switch bytes.regionCount {
case 0:
context.write(self.wrapOutboundOut(ByteBuffer()), promise: promise)
case 1:
context.write(self.wrapOutboundOut(bytes.regions[0].byteBuffer), promise: promise)
default:
let buffers = bytes.makeOutboundBuffers()
for (index, buffer) in buffers.enumerated() {
let outbound = self.wrapOutboundOut(buffer)
if index == buffers.count - 1 {
context.write(outbound, promise: promise)
} else {
context.write(outbound).cascadeFailure(to: promise)
}
}
}
}
}
// `IMAPServerHandler` holds per-connection mutable state and is confined to a
// single `EventLoop`, so it is deliberately non-`Sendable`.
@available(*, unavailable)
extension IMAPServerHandler: Sendable {}