Add APIs for reading/writing HTTP Datagrams - #106
Open
aryan-25 wants to merge 2 commits into
Open
Conversation
### Motivation Users may wish to send/receive HTTP Datagrams ([RFC 9297](https://datatracker.ietf.org/doc/html/rfc9297)) using NIOHTTPServer. This PR introduces reader and writer APIs for unreliable datagrams. Note that the underlying support is yet to be provided by `swift-nio-http3`. The read/write source is not wired up, and we currently throw an error when attempting to read or write to/from the unreliable datagram stream. ### Modifications - Added an `UnstableHTTPDatagrams` trait, disabled by default. All new APIs are behind that trait (note that the `HTTP3` trait must also be enabled as unreliable datagrams are currently only supported over QUIC). - Added a `withDatagramReader` method on `NIOHTTPServer.Reader` and a `withDatagramWriter` method on `NIOHTTPServer.ResponseSender.Writer`. Both methods are `consuming` and take a closure that receive the reliable request/response body reader/writer and optionally the unreliable datagram reader/writer as arguments. - In both methods, the readers/writers are passed as `consuming sending`, so callers can run the readers/writers separately in concurrent tasks. - Added `ConnectUDPExample.swift` to show the proposed shape end to end. ### Result APIs for sending/receiving unreliable HTTP Datagrams added.
gjcairo
requested changes
Jul 31, 2026
| #if HTTP3 && UnstableHTTPDatagrams | ||
| /// The unreliable datagram reader, present when the underlying transport is capable of reading/writing | ||
| /// unreliable datagrams. | ||
| private var datagramReader: Disconnected<NIOHTTPServer.DatagramReader>? |
Collaborator
Author
There was a problem hiding this comment.
Yes, without this, the datagramReader can't be vended out as sending in withDatagramReader.
| public consuming func withDatagramWriter( | ||
| _ body: (consuming sending Self, consuming sending NIOHTTPServer.DatagramWriter?) async throws -> Void | ||
| ) async throws { | ||
| let streamFinish = NIOHTTPServer.StreamFinish(writer: self.writer, state: self.writerState) |
Collaborator
There was a problem hiding this comment.
I think this type is a bit unnecessary and adds some indirection which makes things harder to follow, IMO. I'd just finish both streams inline in this func, like we do elsewhere.
Collaborator
Author
There was a problem hiding this comment.
We unfortunately need this indirection because we pass the stream writer (and the datagram writer) as consuming sending to the body closure, which means we can't call finish on either after the body closure.
Comment on lines
+244
to
+245
| readerState: .init(iterator: stream.makeAsyncIterator()), | ||
| datagramReader: .init(value: .init()) |
Collaborator
There was a problem hiding this comment.
All these .inits are a bit confusing to understand - can we spell out the names?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Users may wish to send/receive HTTP Datagrams (RFC 9297) using NIOHTTPServer. This PR introduces reader and writer APIs for unreliable datagrams.
Note that the underlying support is yet to be provided by
swift-nio-http3. The read/write source is not wired up, and we currently throw an error when attempting to read or write to/from the unreliable datagram stream.Modifications
Added an
UnstableHTTPDatagramstrait, disabled by default. All new APIs are behind that trait (note that theHTTP3trait must also be enabled as unreliable datagrams are currently only supported over QUIC).Added a
withDatagramReadermethod onNIOHTTPServer.Readerand awithDatagramWritermethod onNIOHTTPServer.ResponseSender.Writer. Both methods areconsumingand take a closure that receive the reliable request/response body reader/writer and optionally the unreliable datagram reader/writer as arguments.consuming sending, so callers can run the readers/writers separately in concurrent tasks.Added
ConnectUDPExample.swiftto show the proposed shape end to end.Result
APIs for sending/receiving unreliable HTTP Datagrams added.