* Reject invalid response text when writing
`.no(.init(text: "No mailbox x\r\n* BYE pwned"))` was written verbatim,
injecting an untagged response; likewise tagged, BYE, PREAUTH and
continuation responses.
RFC 9051: `text = 1*(TEXT-CHAR / UTF8-2 / UTF8-3 / UTF8-4)`. `writeText`
now asserts no NUL, CR or LF. Empty stays valid. The parser now accepts
UTF-8 text.
* Add Command.Tag for command and response tags
`TaggedCommand(tag: "A1 NOOP\r\nA2 DELETE INBOX\r\nA3", command: .noop)`
was written verbatim, injecting a command; likewise `APPEND` start.
`TaggedResponse(tag: "* BYE x\r\nA1", …)` injected a response.
RFC 3501: `tag = 1*<any ASTRING-CHAR except "+">`. New `Command.Tag`
replaces the `String` in `TaggedCommand`, `TaggedResponse`, `Response`,
`CommandStreamPart`, `AppendCommand`, `BadCommand` and NIOIMAP's state
machine errors; `init?(_:)` validates, literals trap. The parser already
enforces the production. IMAPCommands converts its numeric tags.
* Validate and escape namespace delimiters
`NamespaceDescription(string: "x", char: "\r\n", …)` wrote a raw CRLF
inside `"…"`, injecting a response. `"\""` encoded as `"""`, and `"\\"`
as `"\"` (unterminated); non-ASCII was accepted.
RFC 2342: `DQUOTE QUOTED-CHAR DQUOTE / nil`. `init(string:delimiter:
responseExtensions:)` (was `char:`) throws `InvalidDelimiterError` for
non-ASCII, CR, LF, NUL; the delimiter is stored as a byte, read-only.
Quoted-specials are escaped on write and parsed when escaped.
* Gate a capturing exit test on Swift 6.3
* [CI] Adopt GitHub Actions
# Motivation
We want to adopt GitHub actions in all of our Swift repositories to replace our current Jenkins CI.
# Modification
This PR adopts the reusable workflows from NIO and the swiftlang organization to ensure soundness and test coverage. Additionally, this PR enables a sematic label check and adds a `release.yml` to make releasing this package easy.
# Result
Modern CI and release flow.
Support appending a `UnsafeRawBufferPointer`. This is useful when the input is `Data` or `DispatchData`.
Add `inputBufferByteCount: Int` property -- which is useful for logging and tests.
Add `testTwoCommandsAcrossMultipleBuffers()` test case.
This introduces FramingResult, which signals either a "complete" frame, "incomplete", or "invalid".
If a frame is incomplete then the minimum number of required bytes to make a complete frame are sent.
if a frame is invalid or complete then you are provided with the bytes in that frame.
Invalid frames are forwarded onto the user via user events.
It doesn't have many bells and whistles yet, but it handles a lot of use cases we'll see in the wild, and you'll probably still find it useful
Given the command A1 LOG it will delivered .incompleteFrame(2) to signal that this frame is incomplete, and that we need at least 2 more bytes (in this case the CR and LF) to complete the frame (this is because it doesn't have the ability to understand the login command fully as it's only used to build frames).
Given the command A1 LOGIN {3}\r\na it will deliver 2 frames:
.complete("A1 LOGIN {3}\r\n
.complete("a") this is a "complete" frame because we're streaming a literal, so need to forward all bytes as soon as possible
Finally a case of invalid, A1 LOGIN {a will deliver 1 frame containing the data, including the byte where we detected the issue: .invalid("A1 LOGIN {a")
* 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>
The client state machine has now been fully rewritten, and encapsulates the miniature state machines implemented in previous PRs.
I've extracted the state machine from IMAPClientHandler and moved it into ClientStateMachine, hence all the code deletion.
The state machine receives a command, response, or continuation request from the IMAPClientHandler and returns an array of Chunk/EventLoopPromise pairs that the client handler should then write to the network.
Also we didn't previously queue Append commands like we did for others, however it's now become apparent that all commands types should be queued, so we do that now.
A lot of tests have been added to make sure everything works.
Note that a lot of sendCommand tests have been deleted as we now precondition on user errors instead of throwing.
Identical pattern to that used for IDLE and AUTHENTICATE. The main ClientStateMachine detects the append command being sent, and switches state to contain a sub-machine that is dedicated to validating the lifecycle of an append command.
We support "standard" message appends, as well as catenation, and continuation requests for both.
Add support for authentication to the new client state machine, with a typical workflow test, and a test to show that attempting to send another command while authenticating will result in an error.
NOTE: The many defaults used when switching over things is just because this is an example. If we're happy with how this looks, I'll remove them and handle properly.
This is a basic demonstrative implementation of the new client state machine that shows how the IDLE command is supported.
For an idle state to be established, the client needs to send the idleStart command, and the server needs to respond with a continuationRequest, at which point the state is idle.