fix: Workaround to pass UInt16 value for port in ClientRequest.Options #285
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.
Description
Applies the fix proposed in Kitura/Kitura-NIO#140 to Kitura-net, in order to allow port values greater than Int16.max to be used with ClientRequest.
Motivation and Context
ClientRequest.Options defines the
port
option to have a value of typeInt16
. This should beUInt16
as it needs to represent values in the range 1 - 65,535.This code works until you try to connect to a server whose port is in a higher port range (32,768 - 65,535), such as would happen if you were to start the server on an ephemeral port.
Unfortunately, it would be a breaking change for any existing users of ClientRequest (eg. Kitura's tests: https://github.com/IBM-Swift/Kitura/blob/2.6.0/Tests/KituraTests/KituraTest.swift#L161) if we were to correct the type. Instead, this fix uses a trick to pass the bit pattern of a UInt16 value via the Int16 parameter. This requires that the user applies their half of the trick in cases where they want to pass a value greater than Int16.max.
This was already reported under: #275
More recently, Kitura/Kitura#1382 enables the Kitura tests to use an ephemeral port, so that the tests can run in parallel. However, the default ephemeral port range on Mac is 49,152 - 65,535, which guarantees that assignment of the port value to ClientRequest.Options.port will fail.
Kitura-CouchDB also mirrors this bug, since it sits on top of ClientRequest, and we should apply similar changes: https://github.com/IBM-Swift/Kitura-CouchDB/blob/2.1.1/Sources/CouchDB/ConnectionProperties.swift#L29
How Has This Been Tested?
Checklist: