Files
swift-nio-imap/Sources/NIOIMAP/Client/FrameDecoder.swift
T
David EvansandCory Benfield 275a907a86 Implement the beginnings of a framing parser (#614)
* Boilerplate

* Add extremely simple tests cases

* Basic literal support

* Add literal+, literal-, and binary support

* Fix backtracking

* Start some integration tests

* A few type renames

* Make the framer throwing

* Soundness

* Address some of Cory's comments

* More graceful handling or CRs and LFs

* Apple Cory suggestions part 2

* Swallow LF

* Make it all work

* Refactor

* Define LineFeedStrategy enum

* Refactor integer parsing and add frame status

* Digit constants

* Refactor adjusting frame length

* Defend against infinite size

* Fix literal bugs

* Soundness

* Add some docs

* Update Sources/NIOIMAP/Client/FramingParser.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Prefer () over break

* Use UInt64 where appropriate

* Implement a stepback function

* Fix FrameSatus typo

* Don't allocate 2 buffers

* Rename LiteralSubstate to LiteralHeaderState

* Use Float64

* Remove frame debugging function

* Fix empty frame logic

* Discard empty bytes

* Fix handing optional LF in a literal

* Soundness

* Remove random Darwin import

* Cory nits

* Discard when we've read over half the buffer

* Read at least 1KB before cleaning

* Soundness

* Fix precondition

Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-09-15 16:38:03 +01:00

37 lines
1.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 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 FrameDecoder: ByteToMessageDecoder {
public typealias InboundOut = ByteBuffer
private var framingParser = FramingParser()
public init() {}
public mutating func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
let frames = try self.framingParser.appendAndFrameBuffer(&buffer)
guard frames.count > 0 else {
return .needMoreData
}
for frame in frames {
context.fireChannelRead(self.wrapInboundOut(frame))
}
return .continue
}
}