Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit 3e49ea0

Browse files
Trevörtanner0101
authored andcommitted
Specify host port when different from scheme default port (#358)
* Specify host port when different from scheme default port When port is not the same as the standard port for the scheme, it should be specified (https://tools.ietf.org/html/rfc2616#section-14.23) * Add comment to explain why we specify host port * Rename host to hostHeaderValue
1 parent d384de4 commit 3e49ea0

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Sources/HTTPKit/Client/HTTPClient.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public final class HTTPClient {
6969

7070
public func send(_ request: HTTPRequest) -> EventLoopFuture<HTTPResponse> {
7171
let hostname = request.url.host ?? ""
72-
let port = request.url.port ?? (request.url.scheme == "https" ? 443 : 80)
72+
let defaultSchemePort = request.url.scheme == "https" ? 443 : 80
73+
let port = request.url.port ?? defaultSchemePort
7374
let tlsConfig: TLSConfiguration?
7475
switch request.url.scheme {
7576
case "https":
@@ -139,7 +140,16 @@ public final class HTTPClient {
139140
handlers.append(("client-decoder", clientResDecoder))
140141
httpHandlerNames.append("client-decoder")
141142

142-
let clientReqEncoder = HTTPClientRequestEncoder(hostname: hostname)
143+
// When port is different from the default port for the scheme
144+
// it must be explicitly specified in the `Host` HTTP header.
145+
// See https://tools.ietf.org/html/rfc2616#section-14.23
146+
let clientReqEncoder: HTTPClientRequestEncoder
147+
if port == defaultSchemePort {
148+
clientReqEncoder = HTTPClientRequestEncoder(hostHeaderValue: hostname)
149+
} else {
150+
clientReqEncoder = HTTPClientRequestEncoder(hostHeaderValue: "\(hostname):\(port)")
151+
}
152+
143153
handlers.append(("client-encoder", clientReqEncoder))
144154
httpHandlerNames.append("client-encoder")
145155

Sources/HTTPKit/Client/HTTPClientRequestEncoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ internal final class HTTPClientRequestEncoder: ChannelOutboundHandler, Removable
33
typealias OutboundIn = HTTPRequest
44
typealias OutboundOut = HTTPClientRequestPart
55

6-
let hostname: String
6+
let hostHeaderValue: String
77

88
/// Creates a new `HTTPClientRequestSerializer`.
9-
init(hostname: String) {
10-
self.hostname = hostname
9+
init(hostHeaderValue: String) {
10+
self.hostHeaderValue = hostHeaderValue
1111
}
1212

1313
/// See `ChannelOutboundHandler`.
1414
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
1515
let req = unwrapOutboundIn(data)
1616
var headers = req.headers
17-
headers.add(name: .host, value: self.hostname)
17+
headers.add(name: .host, value: self.hostHeaderValue)
1818

1919
let path: String
2020
if let query = req.url.query {

0 commit comments

Comments
 (0)