Files
swift-nio-imap/Sources/NIOIMAPCore/NIOIMAPCore.docc/ParsingAndEncoding.md
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

3.4 KiB

Parsing and Encoding

Parse IMAP server responses and encode client commands using these streaming parsers and encode buffers.

Overview

NIOIMAPCore provides comprehensive parsing and encoding capabilities. These types handle the conversion between the IMAP wire format and strongly-typed Swift structures, including error handling and advanced features like pipelining.

On the way in, the caller owns the received bytes and the parser borrows them. Append each read to an IMAPInputBuffer — a read of 1 KiB or more at a fixed address is not copied; a Data is; for a DispatchData, IMAPInputBuffer/append(contentsOf:) keeps large regions without copying — and pass it to ResponseParser/parseResponseStream(buffer:) or CommandParser/parseCommandStream(buffer:), which consume what they parse and leave the rest. Small fields come out as owned values such as String and [UInt8]. Large payloads — message bodies, literals — come out as IMAPBuffer: a piece taken from a read the buffer retained refers to it rather than copying it (a large Data is retained as the buffer's one copy) if it is at least 512 bytes and at least a third of the memory that read keeps alive, so a payload never pins more than three times its size; other pieces, and bytes from smaller reads, are copied. To parse a ByteBuffer, DispatchData, array or Data, append it to an IMAPInputBuffer; the parsers take nothing else.

On the way out, CommandEncodeBuffer and ResponseEncodeBuffer write into an EncoderOutputBuffer — IMAPOutputBuffer unless you pass another — and hand bytes out as an IMAPBufferList — ordered IMAPBuffer regions, usually one — from CommandEncodeBuffer/nextChunk() and ResponseEncodeBuffer/readBytes(). A command chunk ends at each synchronizing literal header; its CommandEncodeBuffer/Chunk/nextStep says whether to wait for the server's + before sending the next. A large payload you supply as an IMAPBuffer is kept by reference, so forwarding a message costs no copy. Handed-out bytes never move or change, so a chunk is Sendable and can be written while encoding continues. Convert it with IMAPBufferList/makeDispatchData(), makeByteBuffers() in NIOIMAP, or IMAPBufferList/copiedBytes().

Topics

Parsers

Streaming parsers for different contexts.

  • ResponseParser
  • CommandParser
  • SynchronizingLiteralParser

Encoders

Buffers for encoding protocol elements back to wire format.

  • CommandEncodeBuffer
  • ResponseEncodeBuffer
  • CommandEncodingOptions
  • ResponseEncodingOptions

Bytes

Types for carrying raw message payloads without copying them.

  • IMAPBuffer
  • IMAPBufferList
  • IMAPByteStorage
  • ByteRegion
  • StableAddressByteRegion

Parser input

Where received bytes wait to be parsed, and how the parser reads them.

  • IMAPInputBuffer
  • ParseInput
  • RegionSink

Encoder output

Where encoded bytes go.

  • EncoderOutputBuffer
  • IMAPOutputBuffer

Pipelining

Types related to command pipelining behavior.

  • PipeliningRequirement
  • PipeliningBehavior

Errors

Parsing and encoding errors that may occur.

  • BadCommand
  • ParserError
  • TooMuchRecursion
  • ExceededMaximumMessageAttributesError
  • ExceededMaximumBodySizeError
  • ExceededLiteralSizeLimitError

Utilities

Utility types and protocol defaults.

  • ModifiedUTF7
  • IMAPDefaults