mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-29 05:24:23 +00:00
The grammar now runs on the ~Escapable ParseInput cursor; ParserLibrary and its ParseBuffer are deleted. The stream parsers are generic over ConsumableParserInputBuffer and gather once per call: CommandParser frames synchronizing literals and parses through the same cursor, narrowing its limit instead of slicing. The NIO handlers still feed ByteBuffers, through its ParserInputBuffer conformance. Review focus: CommandParser.parseCommandStream and ResponseParser.parseResponseStream (what is consumed on each exit path). Behavior changes: - Stray spaces before a line ending: up to ParseInput.maximumStraySpaces (1024) are accepted; more throw ParserError. On main, PL.parseNewline recursed through composite once per space, so ~100 threw TooMuchRecursion from the shared 100-frame tracker. Both a looser limit and a different error type. - Trailing spaces with no line ending yet report IncompleteMessage while under the cap, where they used to throw ParserError. Fixes the client side (ResponseParser), which has no framing layer, for a read split as "* 1 EXISTS " | "\r\n". - From an IMAPInputBuffer, payload pieces of at least 1 KiB taken from a retained read (>= 1 KiB, fixed address) refer to it; a large Data is retained as the buffer's one copy. Smaller pieces and tail bytes are copied. From a bare ByteBuffer, payloads are copied. Grammar edits beyond the cursor: the QUOTA resource loop binds parseOptional's result to an explicit `Void?`, and parseLookupKeyword loses a comment about the tracker it no longer takes. ParserError.hint gains a note to keep hot-path hints literal: ParserLibrary's interpolated ones, deleted here, allocated on every mismatch. Public API break: parseCommandStream(buffer:) and parseResponseStream(buffer:) are generic. ByteBuffer conforms in NIOIMAP, so a caller importing only NIOIMAPCore must import NIOIMAP to pass one. Temporary: `typealias ParseBuffer = ParseInput` keeps the grammar's spelling until "Rename ParseBuffer to ParseInput (scripted)". Removes LegacySubParser, the ParseBuffer cursor-method shim and the framing bridge from "Move SynchronizingLiteralParser onto ParserInputBuffer".
126 lines
5.4 KiB
Swift
126 lines
5.4 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Decodes `bytes` as UTF-8.
|
|
///
|
|
/// A free function rather than a `String` initialiser because it throws a ``ParserError`` carrying the
|
|
/// source location that asked for the decode, which is what the grammar wants and what a general-purpose
|
|
/// initialiser has no business reporting.
|
|
///
|
|
/// - Throws: ``ParserError`` when `bytes` are not valid UTF-8.
|
|
func decodeUTF8(
|
|
_ bytes: some Collection<UInt8>,
|
|
file: StaticString = (#fileID),
|
|
line: Int = #line
|
|
) throws -> String {
|
|
guard let string = String(validatingUTF8Bytes: bytes) else {
|
|
throw ParserError.invalidUTF8(file: file, line: line)
|
|
}
|
|
return string
|
|
}
|
|
|
|
/// The parser needs more bytes before it can decide.
|
|
///
|
|
/// Thrown, rather than returned, because it can arise anywhere in the grammar; the entry points turn
|
|
/// it back into "no message yet".
|
|
struct IncompleteMessage: Error {
|
|
internal init() {}
|
|
}
|
|
|
|
/// Parsing a tagged command failed. Includes the tag of the invalid command, and the parsing error.
|
|
///
|
|
/// When the server receives a command from a client, it includes a tag for correlation.
|
|
/// If the command line cannot be parsed, this error includes both the tag (so the server
|
|
/// can send back a properly-tagged error response) and details about what went wrong.
|
|
///
|
|
/// - SeeAlso: ``ParserError``, [RFC 3501 Section 6.1](https://datatracker.ietf.org/doc/html/rfc3501#section-6.1)
|
|
public struct BadCommand: Error {
|
|
/// The tag of the bad command.
|
|
///
|
|
/// Allows the server to send a properly-tagged error response to the client,
|
|
/// maintaining IMAP's request/response correlation even when the command is malformed.
|
|
public var commandTag: Command.Tag
|
|
|
|
/// Why parsing failed.
|
|
///
|
|
/// Contains a human-readable hint about the parsing error and source location information.
|
|
public var parserError: ParserError
|
|
}
|
|
|
|
/// An error occurred when parsing an IMAP command or response.
|
|
///
|
|
/// `ParserError` is thrown when the IMAP protocol parser encounters bytes that cannot be
|
|
/// interpreted according to the IMAP grammar (RFC 3501 or extensions). Common causes include:
|
|
/// - Invalid UTF-8 sequences in string fields
|
|
/// - Malformed protocol syntax (for example, unexpected characters or missing required elements)
|
|
/// - Non-conforming protocol elements
|
|
///
|
|
/// The ``hint`` field provides a developer-friendly description of what went wrong.
|
|
/// The internal file and line information is useful for debugging parser issues.
|
|
///
|
|
/// - SeeAlso: [RFC 3501 Section 4](https://datatracker.ietf.org/doc/html/rfc3501#section-4) (grammar)
|
|
public struct ParserError: Error {
|
|
static func invalidUTF8(file: StaticString = (#fileID), line: Int = #line) -> Self {
|
|
ParserError(hint: "Invalid UTF8", file: file, line: line)
|
|
}
|
|
|
|
/// If possible, a description of the error and why it occurred.
|
|
///
|
|
/// Describes the parsing failure in human-readable terms, such as
|
|
/// "Invalid UTF8", "Missing CRLF", "Unexpected character", etc. It's intended
|
|
/// for logging and debugging purposes.
|
|
///
|
|
/// - Note: These errors are thrown as part of ordinary control flow — `parseOneOf` throws one
|
|
/// for every alternative that doesn't match — so the hint must not be built by string
|
|
/// interpolation on any path that is taken often. A plain literal costs nothing; an
|
|
/// interpolation heap-allocates as soon as the result exceeds 15 UTF-8 bytes.
|
|
public var hint: String
|
|
/// `StaticString` rather than `String`: `#fileID` is longer than the 15-byte small-string limit,
|
|
/// so a `String` here would heap-allocate and retain on *every* throw, hint or no hint.
|
|
var file: StaticString
|
|
var line: Int
|
|
|
|
init(hint: String = "Unknown", file: StaticString = (#fileID), line: Int = #line) {
|
|
self.hint = hint
|
|
self.file = file
|
|
self.line = line
|
|
}
|
|
}
|
|
|
|
/// Signals that a protocol message was too complex and required excessive recursive parsing.
|
|
///
|
|
/// IMAP protocol elements can nest (for example, nested parenthesized lists in BODYSTRUCTURE),
|
|
/// and parsing uses recursion to handle this. To prevent stack overflow attacks,
|
|
/// the parser enforces a maximum recursion depth. If this limit is exceeded, this error
|
|
/// is thrown.
|
|
///
|
|
/// A safety limit to prevent malicious or extremely unusual protocol messages
|
|
/// from causing a stack overflow.
|
|
///
|
|
/// - SeeAlso: [RFC 3501 Section 4.3](https://datatracker.ietf.org/doc/html/rfc3501#section-4.3) (protocol syntax)
|
|
public struct TooMuchRecursion: Error {
|
|
/// The maximum number of recursive calls when parsing data before throwing an error.
|
|
///
|
|
/// The parser maintains a recursion depth counter and throws this error when the
|
|
/// depth would exceed this limit. This prevents stack overflow from deeply nested
|
|
/// protocol structures.
|
|
///
|
|
/// Currently fixed at compile-time and not configurable at runtime.
|
|
public var limit: Int
|
|
|
|
init(limit: Int) {
|
|
self.limit = limit
|
|
}
|
|
}
|