Skip to content

Conversation

@aryan-25
Copy link
Collaborator

@aryan-25 aryan-25 commented Dec 2, 2025

Motivation:

Currently, when specifying port 0 in HTTPServerConfiguration, there is no API to determine what port the NIOHTTPServer has actually been bound to.

Modifications:

  • Added a SocketAddress type for representing a host address and port.
  • Added an async listeningAddress property on NIOHTTPServer that returns a SocketAddress once the server has started listening (or throws an error if the address couldn't be bound to or if the server has closed).
  • Added an internal State type to NIOHTTPServer for representing the idle, listening, and closed states and managing transitions between them.

Result:

Users can determine the address and port the NIOHTTPServer is listening at.

Currently, when specifying port 0 in `HTTPServerConfiguration`, there is no public API to determine what port the `NIOHTTPServer` has actually been bound to.

Modifications:
- Added a `SocketAddress` type for representing a host address and port.
- Added an async `listeningAddress` property on `NIOHTTPServer` that returns a `SocketAddress` once the server has started listening (or throws an error if the address couldn't be bound to or if the server has closed).
- Added a `State` type to `NIOHTTPServer` for representing the idle, listening, and closed states and managing transitions between them.

Result:
Users can determine the address and port the `NIOHTTPServer` is listening at.
import NIOCore

/// Represents an IPv4 address.
public struct IPv4: Hashable, Sendable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit hesitant to add these types to this package. This is probably something that is generally useful and might require some API design on the HTTPServer protocol over in the other repo. WDYT @gjcairo?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think this should be part of the NIO implementation only. So yes it would make sense to move them to the other repo. Would you be okay with having these here for now, and I can open a PR on the other repo as well? Then once we land them there we can remove them from here alongside the other abstract pieces.

Also: would SocketAddress be something that's part of the currency types we'd like to have?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it is okay to land them here for now but let's file an issue on the other repo. Ideally SocketAddress or something similar is a shared currency type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gjcairo gjcairo self-requested a review December 3, 2025 11:05
@aryan-25 aryan-25 merged commit dae143c into swift-server:main Dec 5, 2025
4 of 18 checks passed
//
//===----------------------------------------------------------------------===//

import NIOCore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the NIO import here right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this. Fixed in #34.

@aryan-25 aryan-25 deleted the add-listening-address branch December 10, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants