Skip to content

The host header should also include the port #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
@@ -771,8 +771,13 @@ extension TaskHandler: ChannelDuplexHandler {
uri: request.uri)
var headers = request.headers

if !request.headers.contains(name: "Host") {
headers.add(name: "Host", value: request.host)
if !request.headers.contains(name: "host") {
let port = request.port
var host = request.host
if !(port == 80 && request.scheme == "http"), !(port == 443 && request.scheme == "https") {
host += ":\(port)"
}
headers.add(name: "host", value: host)
}

do {
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ extension HTTPClientInternalTests {
return [
("testHTTPPartsHandler", testHTTPPartsHandler),
("testBadHTTPRequest", testBadHTTPRequest),
("testHostPort", testHostPort),
("testHTTPPartsHandlerMultiBody", testHTTPPartsHandlerMultiBody),
("testProxyStreaming", testProxyStreaming),
("testProxyStreamingFailure", testProxyStreamingFailure),
40 changes: 38 additions & 2 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
@@ -49,15 +49,15 @@ class HTTPClientInternalTests: XCTestCase {
ignoreUncleanSSLShutdown: false,
logger: HTTPClient.loggingDisabled)).wait()

var request = try Request(url: "http://localhost/get")
var request = try Request(url: "http://localhost:8080/get")
request.headers.add(name: "X-Test-Header", value: "X-Test-Value")
request.body = .string("1234")

XCTAssertNoThrow(try channel.writeOutbound(request))
XCTAssertEqual(3, recorder.writes.count)
var head = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get")
head.headers.add(name: "X-Test-Header", value: "X-Test-Value")
head.headers.add(name: "Host", value: "localhost")
head.headers.add(name: "Host", value: "localhost:8080")
head.headers.add(name: "Content-Length", value: "4")
XCTAssertEqual(HTTPClientRequestPart.head(head), recorder.writes[0])
let buffer = ByteBuffer.of(string: "1234")
@@ -90,6 +90,42 @@ class HTTPClientInternalTests: XCTestCase {
}
}

func testHostPort() throws {
let channel = EmbeddedChannel()
let recorder = RecordingHandler<HTTPClientResponsePart, HTTPClientRequestPart>()
let task = Task<Void>(eventLoop: channel.eventLoop, logger: HTTPClient.loggingDisabled)

try channel.pipeline.addHandler(recorder).wait()
try channel.pipeline.addHandler(TaskHandler(task: task,
kind: .host,
delegate: TestHTTPDelegate(),
redirectHandler: nil,
ignoreUncleanSSLShutdown: false,
logger: HTTPClient.loggingDisabled)).wait()

let request1 = try Request(url: "http://localhost:80/get")
XCTAssertNoThrow(try channel.writeOutbound(request1))
let request2 = try Request(url: "https://localhost/get")
XCTAssertNoThrow(try channel.writeOutbound(request2))
let request3 = try Request(url: "http://localhost:8080/get")
XCTAssertNoThrow(try channel.writeOutbound(request3))
let request4 = try Request(url: "http://localhost:443/get")
XCTAssertNoThrow(try channel.writeOutbound(request4))
let request5 = try Request(url: "https://localhost:80/get")
XCTAssertNoThrow(try channel.writeOutbound(request5))

let head1 = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get", headers: ["host": "localhost"])
XCTAssertEqual(HTTPClientRequestPart.head(head1), recorder.writes[0])
let head2 = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get", headers: ["host": "localhost"])
XCTAssertEqual(HTTPClientRequestPart.head(head2), recorder.writes[2])
let head3 = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get", headers: ["host": "localhost:8080"])
XCTAssertEqual(HTTPClientRequestPart.head(head3), recorder.writes[4])
let head4 = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get", headers: ["host": "localhost:443"])
XCTAssertEqual(HTTPClientRequestPart.head(head4), recorder.writes[6])
let head5 = HTTPRequestHead(version: HTTPVersion(major: 1, minor: 1), method: .GET, uri: "/get", headers: ["host": "localhost:80"])
XCTAssertEqual(HTTPClientRequestPart.head(head5), recorder.writes[8])
}

func testHTTPPartsHandlerMultiBody() throws {
let channel = EmbeddedChannel()
let delegate = TestHTTPDelegate()
14 changes: 7 additions & 7 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ class HTTPClientTests: XCTestCase {
return
}
let hostName = try? JSONDecoder().decode(RequestInfo.self, from: body).data
XCTAssertEqual("127.0.0.1", hostName)
XCTAssertEqual("127.0.0.1:\(self.defaultHTTPBin.port)", hostName)
}

func testPercentEncoded() throws {
@@ -763,7 +763,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))
@@ -787,7 +787,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))
@@ -808,7 +808,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))
@@ -831,7 +831,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))
@@ -859,7 +859,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))
@@ -1036,7 +1036,7 @@ class HTTPClientTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/foo",
headers: HTTPHeaders([("Host", "localhost")]))),
headers: HTTPHeaders([("Host", "localhost:\(web.serverPort)")]))),
try web.readInbound()))
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
try web.readInbound()))