mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-26 04:57:51 +00:00
* Move tests to dedicated file * Add NOOP tests * Add capability tests * Add logout tests * Add starttls tests * Add select tests * Add examine tests * Fix sanity * Add create tests * Add delete tests * Fix sanity * Add subscribe tests * Add unsubscribe tests * Add missing test file header * Add check tests * Fix sanity * Rebase * Add list tests * Add LSUB tests * Add status tests * Add append tests * Setup response tests * Allow response parse mode override * Temp remove append * Conditional state tests * Bye tests * Capability tests * Flags tests * Exists tests * Recent tests * Expunge tests * Tagged response tests * List, LSUB, and status tests * Comment everything * Make tests use synchronizing literals * Replace CRLF * Test fetch responses * Fix sanity * Assert * Refactor error catching * Test that breaks synchronising literals * fix some stuff * Fix sanity * Fix all the things * Fix sanity Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
90 lines
3.7 KiB
Swift
90 lines
3.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 NIO
|
|
import NIOIMAPCore
|
|
|
|
public struct IMAPDecoderError: Error {
|
|
public var parserError: Error
|
|
public var buffer: ByteBuffer
|
|
}
|
|
|
|
public struct CommandDecoder: NIOSingleStepByteToMessageDecoder {
|
|
public typealias InboundOut = PartialCommandStream
|
|
|
|
private var ok: ByteBuffer?
|
|
private var parser: CommandParser
|
|
private var synchronisingLiteralParser = SynchronizingLiteralParser()
|
|
|
|
public struct PartialCommandStream: Equatable {
|
|
public var numberOfSynchronisingLiterals: Int
|
|
public var command: CommandStream?
|
|
|
|
internal init(numberOfSynchronisingLiterals: Int, command: CommandStream?) {
|
|
self.numberOfSynchronisingLiterals = numberOfSynchronisingLiterals
|
|
self.command = command
|
|
}
|
|
|
|
public init(_ command: CommandStream, numberOfSynchronisingLiterals: Int = 0) {
|
|
self = .init(numberOfSynchronisingLiterals: numberOfSynchronisingLiterals, command: command)
|
|
}
|
|
|
|
public init(numberOfSynchronisingLiterals: Int) {
|
|
self = .init(numberOfSynchronisingLiterals: numberOfSynchronisingLiterals, command: nil)
|
|
}
|
|
}
|
|
|
|
public init(bufferLimit: Int = 1_000) {
|
|
self.parser = CommandParser(bufferLimit: bufferLimit)
|
|
}
|
|
|
|
public mutating func decode(buffer: inout ByteBuffer) throws -> PartialCommandStream? {
|
|
let save = buffer
|
|
do {
|
|
let framingResult = try self.synchronisingLiteralParser.parseContinuationsNecessary(buffer)
|
|
var result = PartialCommandStream(numberOfSynchronisingLiterals: framingResult.synchronizingLiteralCount,
|
|
command: nil)
|
|
|
|
let readables = buffer.readableBytes
|
|
if let command = try self.parser.parseCommandStream(buffer: &buffer) {
|
|
let consumedBytes = buffer.readerIndex - save.readerIndex
|
|
assert(buffer.writerIndex == save.writerIndex,
|
|
"the writer index of the buffer moved whilst parsing which is not supported: \(buffer), \(save)")
|
|
assert(consumedBytes > 0,
|
|
"allegedly, we consumed a negative amount of bytes: \(consumedBytes)")
|
|
self.synchronisingLiteralParser.consumed(consumedBytes)
|
|
assert(consumedBytes <= framingResult.maximumValidBytes,
|
|
"We consumed \(consumedBytes) which is more than the framing parser thought are maximally " +
|
|
"valid: \(framingResult), \(self.synchronisingLiteralParser)")
|
|
result.command = command
|
|
return result
|
|
} else {
|
|
assert(readables == buffer.readableBytes,
|
|
"parser consumed bytes on nil: readableBytes before parse: \(readables), buffer: \(buffer)")
|
|
if result.numberOfSynchronisingLiterals == 0 {
|
|
return nil
|
|
} else {
|
|
return result
|
|
}
|
|
}
|
|
} catch {
|
|
throw IMAPDecoderError(parserError: error, buffer: save)
|
|
}
|
|
}
|
|
|
|
public mutating func decodeLast(buffer: inout ByteBuffer, seenEOF: Bool) throws -> PartialCommandStream? {
|
|
try self.decode(buffer: &buffer)
|
|
}
|
|
}
|