From 7db0ad9bdd2a7cba8ff21b58a7c8dc559d365c2f Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Sat, 22 Aug 2020 07:59:34 -0700 Subject: [PATCH] Fixed a regression where configuring a nil hostname or port could cause the server to crash The hostname and port could still be nil if the server was started using the default configuration (with address set to nil when starting the server). Also, added clarification to what `nil` means in the context of a server's default configuration. Closes #2478 --- Sources/Vapor/HTTP/Server/HTTPServer.swift | 4 ++-- Tests/VaporTests/ServerTests.swift | 24 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Sources/Vapor/HTTP/Server/HTTPServer.swift b/Sources/Vapor/HTTP/Server/HTTPServer.swift index ed66c51d0e..3f9b6ec367 100644 --- a/Sources/Vapor/HTTP/Server/HTTPServer.swift +++ b/Sources/Vapor/HTTP/Server/HTTPServer.swift @@ -20,7 +20,7 @@ public final class HTTPServer: Server { public static let defaultHostname = "127.0.0.1" public static let defaultPort = 8080 - /// Address the server will bind to. + /// Address the server will bind to. Configuring an address using a hostname with a nil host or port will use the default hostname or port respectively. public var address: BindAddress /// Host name the server will bind to. @@ -255,7 +255,7 @@ public final class HTTPServer: Server { let addressDescription: String switch configuration.address { case .hostname(let hostname, let port): - addressDescription = "\(scheme)://\(hostname!):\(port!)" // will never be nil because we set them above to non-optional values + addressDescription = "\(scheme)://\(hostname ?? configuration.hostname):\(port ?? configuration.port)" case .unixDomainSocket(let socketPath): addressDescription = "\(scheme)+unix: \(socketPath)" } diff --git a/Tests/VaporTests/ServerTests.swift b/Tests/VaporTests/ServerTests.swift index 8a3a76e7f4..26a69b90e7 100644 --- a/Tests/VaporTests/ServerTests.swift +++ b/Tests/VaporTests/ServerTests.swift @@ -543,6 +543,30 @@ final class ServerTests: XCTestCase { XCTAssertThrowsError(try app.start()) } + + func testStartWithDefaultHostnameConfiguration() throws { + let app = Application(.testing) + app.http.server.configuration.address = .hostname(nil, port: nil) + defer { app.shutdown() } + + XCTAssertNoThrow(try app.start()) + } + + func testStartWithDefaultHostname() throws { + let app = Application(.testing) + app.http.server.configuration.address = .hostname(nil, port: 8008) + defer { app.shutdown() } + + XCTAssertNoThrow(try app.start()) + } + + func testStartWithDefaultPort() throws { + let app = Application(.testing) + app.http.server.configuration.address = .hostname("0.0.0.0", port: nil) + defer { app.shutdown() } + + XCTAssertNoThrow(try app.start()) + } func testAddressConfigurations() throws { var configuration = HTTPServer.Configuration()