From cfd7de7b5d7aa676bb9f039660f7569a6449039e Mon Sep 17 00:00:00 2001 From: Tabor Kelly Date: Wed, 15 May 2019 18:01:17 -0700 Subject: [PATCH 1/4] Pass through node to bootstrap.bind(). --- Sources/KituraNet/FastCGI/FastCGIServer.swift | 24 ++++++++++++ Sources/KituraNet/HTTP/HTTPServer.swift | 38 +++++++++++++------ Sources/KituraNet/Server/Server.swift | 20 ++++++++++ Tests/KituraNetTests/ClientE2ETests.swift | 2 +- Tests/KituraNetTests/KituraNIOTest.swift | 2 +- .../LifecycleListenerTests.swift | 6 +-- Tests/KituraNetTests/MonitoringTests.swift | 2 +- Tests/KituraNetTests/PipeliningTests.swift | 4 +- 8 files changed, 79 insertions(+), 19 deletions(-) diff --git a/Sources/KituraNet/FastCGI/FastCGIServer.swift b/Sources/KituraNet/FastCGI/FastCGIServer.swift index f5209dc6..0342c590 100644 --- a/Sources/KituraNet/FastCGI/FastCGIServer.swift +++ b/Sources/KituraNet/FastCGI/FastCGIServer.swift @@ -29,6 +29,9 @@ public class FastCGIServer: Server { /// Port number for listening for new connections public private(set) var port: Int? + /// Has the same meaning as in `getaddrinfo()`. + public private(set) var node: String? + /// A server state. public private(set) var state: ServerState = .unknown @@ -53,16 +56,37 @@ public class FastCGIServer: Server { /// Listens for connections on a socket /// /// - Parameter on: port number for new connections + /// - Parameter node: has the same meaning as in `getaddrinfo()` + public func listen(on port: Int, node: String?) throws { + fatalError("FastCGI is not implemented yet.") + } + + /// Listens for connections on a socket + /// + /// - Parameter on: port number for new connections + @available(*, deprecated, message: "use 'listen(on:node) throws' instead") public func listen(on port: Int) throws { fatalError("FastCGI is not implemented yet.") } + /// Static method to create a new `FastCGIServer` and have it listen for conenctions + /// + /// - Parameter on: port number for accepting new connections + /// - Parameter node: has the same meaning as in `getaddrinfo()` + /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections + /// + /// - Returns: a new `FastCGIServer` instance + public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> FastCGIServer { + fatalError("FastCGI not implemented yet.") + } + /// Static method to create a new `FastCGIServer` and have it listen for conenctions /// /// - Parameter on: port number for accepting new connections /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections /// /// - Returns: a new `FastCGIServer` instance + @available(*, deprecated, message: "use 'listen(on:node:delegate) throws' instead") public static func listen(on port: Int, delegate: ServerDelegate?) throws -> FastCGIServer { fatalError("FastCGI not implemented yet.") } diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index 402d2e5a..efd9f630 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -58,6 +58,9 @@ public class HTTPServer: Server { /// The TCP port on which this server listens for new connections. If `nil`, this server does not listen on a TCP socket. public private(set) var port: Int? + /// Has the same meaning as in `getaddrinfo()`. + public private(set) var node: String? + /// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket. public private(set) var unixDomainSocketPath: String? @@ -232,8 +235,8 @@ public class HTTPServer: Server { // Sockets could either be TCP/IP sockets or Unix domain sockets private enum SocketType { - // An TCP/IP socket has an associated port number - case tcp(Int) + // An TCP/IP socket has an associated port number and optional node value + case tcp(Int, String?) // A unix domain socket has an associated filename case unix(String) } @@ -258,14 +261,21 @@ public class HTTPServer: Server { ### Usage Example: ### ````swift - try server.listen(on: 8080) + try server.listen(on: 8080, node: "localhost") ```` - Parameter on: Port number for new connections, e.g. 8080 - */ - public func listen(on port: Int) throws { + - Parameter node: has the same meaning as in `getaddrinfo()` + */ + public func listen(on port: Int, node: String?) throws { self.port = port - try listen(.tcp(port)) + self.node = node + try listen(.tcp(port, node)) + } + + @available(*, deprecated, message: "use 'listen(on:node) throws' instead") + public func listen(on port: Int) throws { + try listen(on: port, node: nil) } private func listen(_ socket: SocketType) throws { @@ -310,8 +320,8 @@ public class HTTPServer: Server { let listenerDescription: String do { switch socket { - case SocketType.tcp(let port): - serverChannel = try bootstrap.bind(host: "0.0.0.0", port: port).wait() + case SocketType.tcp(let port, let node): + serverChannel = try bootstrap.bind(host: node ?? "0.0.0.0", port: port).wait() self.port = serverChannel?.localAddress?.port.map { Int($0) } listenerDescription = "port \(self.port ?? port)" case SocketType.unix(let unixDomainSocketPath): @@ -360,21 +370,27 @@ public class HTTPServer: Server { ### Usage Example: ### ````swift - let server = HTTPServer.listen(on: 8080, delegate: self) + let server = HTTPServer.listen(on: 8080, node: "localhost", delegate: self) ```` - Parameter on: Port number for accepting new connections. + - Parameter node: has the same meaning as in `getaddrinfo()` - Parameter delegate: The delegate handler for HTTP connections. - Returns: A new instance of a `HTTPServer`. */ - public static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType { + public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> ServerType { let server = HTTP.createServer() server.delegate = delegate - try server.listen(on: port) + try server.listen(on: port, node: node) return server } + @available(*, deprecated, message: "use 'listen(on:node:delagate) throws' instead") + public static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType { + return try listen(on: port, node: nil, delegate: delegate) + } + /** Static method to create a new HTTP server and have it listen for connections on a Unix domain socket. diff --git a/Sources/KituraNet/Server/Server.swift b/Sources/KituraNet/Server/Server.swift index 4053eda3..dcee5da6 100644 --- a/Sources/KituraNet/Server/Server.swift +++ b/Sources/KituraNet/Server/Server.swift @@ -26,20 +26,40 @@ public protocol Server { /// Port number for listening for new connections. var port: Int? { get } + /// Has the same meaning as in `getaddrinfo()`. + var node: String? { get } + /// A server state. var state: ServerState { get } /// Listen for connections on a socket. /// /// - Parameter on: port number for new connections (eg. 8080) + /// - Parameter node: has the same meaning as in `getaddrinfo()` + func listen(on port: Int, node: String?) throws + + /// Listen for connections on a socket. + /// + /// - Parameter on: port number for new connections (eg. 8080) + @available(*, deprecated, message: "use 'listen(on:node) throws' with instead") func listen(on port: Int) throws + /// Static method to create a new Server and have it listen for connections. + /// + /// - Parameter on: port number for accepting new connections + /// - Parameter node: has the same meaning as in `getaddrinfo()` + /// - Parameter delegate: the delegate handler for HTTP connections + /// + /// - Returns: a new Server instance + static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> ServerType + /// Static method to create a new Server and have it listen for connections. /// /// - Parameter on: port number for accepting new connections /// - Parameter delegate: the delegate handler for HTTP connections /// /// - Returns: a new Server instance + @available(*, deprecated, message: "use 'listen(on:node:delegate) throws' with instead") static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType /// Listen for connections on a socket. diff --git a/Tests/KituraNetTests/ClientE2ETests.swift b/Tests/KituraNetTests/ClientE2ETests.swift index c6e0f6ff..a200505f 100644 --- a/Tests/KituraNetTests/ClientE2ETests.swift +++ b/Tests/KituraNetTests/ClientE2ETests.swift @@ -93,7 +93,7 @@ class ClientE2ETests: KituraNetTest { func testEphemeralListeningPort() { do { - let server = try HTTPServer.listen(on: 0, delegate: delegate) + let server = try HTTPServer.listen(on: 0, node: "localhost", delegate: delegate) _ = HTTP.get("http://localhost:\(server.port!)") { response in XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(String(describing: response?.statusCode))") diff --git a/Tests/KituraNetTests/KituraNIOTest.swift b/Tests/KituraNetTests/KituraNIOTest.swift index 79cf444f..eede93b2 100644 --- a/Tests/KituraNetTests/KituraNIOTest.swift +++ b/Tests/KituraNetTests/KituraNIOTest.swift @@ -96,7 +96,7 @@ class KituraNetTest: XCTestCase { try server.listen(unixDomainSocketPath: unixDomainSocketPath) } else { server.allowPortReuse = allowPortReuse - try server.listen(on: port) + try server.listen(on: port, node: "localhost") } return server } diff --git a/Tests/KituraNetTests/LifecycleListenerTests.swift b/Tests/KituraNetTests/LifecycleListenerTests.swift index 8611230b..cd8e4ca2 100644 --- a/Tests/KituraNetTests/LifecycleListenerTests.swift +++ b/Tests/KituraNetTests/LifecycleListenerTests.swift @@ -55,7 +55,7 @@ class LifecycleListenerTests: KituraNetTest { } do { - try server.listen(on: 0) + try server.listen(on: 0, node: "localhost") self.waitForExpectations(timeout: 5) { error in XCTAssertNil(error) @@ -127,7 +127,7 @@ class LifecycleListenerTests: KituraNetTest { } do { - try server.listen(on: 0) + try server.listen(on: 0, node: "localhost") self.waitForExpectations(timeout: 5) { error in XCTAssertNil(error) @@ -155,7 +155,7 @@ class LifecycleListenerTests: KituraNetTest { }) do { - try server.listen(on: -1) + try server.listen(on: -1, node: nil) } catch { // Do NOT fail the test if an error is thrown. // In this test case an error should be thrown. diff --git a/Tests/KituraNetTests/MonitoringTests.swift b/Tests/KituraNetTests/MonitoringTests.swift index d49ac3e7..e20d806c 100644 --- a/Tests/KituraNetTests/MonitoringTests.swift +++ b/Tests/KituraNetTests/MonitoringTests.swift @@ -65,7 +65,7 @@ class MonitoringTests: KituraNetTest { } do { - try server.listen(on: 0) + try server.listen(on: 0, node: nil) self.waitForExpectations(timeout: 10) { error in server.stop() XCTAssertNil(error) diff --git a/Tests/KituraNetTests/PipeliningTests.swift b/Tests/KituraNetTests/PipeliningTests.swift index 1fedd1c3..1599bc2a 100644 --- a/Tests/KituraNetTests/PipeliningTests.swift +++ b/Tests/KituraNetTests/PipeliningTests.swift @@ -47,7 +47,7 @@ class PipeliningTests: KituraNetTest { let server = HTTPServer() server.delegate = Delegate() do { - try server.listen(on: 0) + try server.listen(on: 0, node: "localhost") } catch let error { Log.error("Server failed to listen. Error: \(error)") } @@ -85,7 +85,7 @@ class PipeliningTests: KituraNetTest { server.delegate = Delegate() server.delegate = Delegate() do { - try server.listen(on: 0) + try server.listen(on: 0, node: "localhost") } catch let error { Log.error("Server failed to listen. Error: \(error)") } From 2c8bebd17ae73afb2df70d36c2051751fba81433 Mon Sep 17 00:00:00 2001 From: Tabor Kelly Date: Sat, 18 May 2019 14:25:53 -0700 Subject: [PATCH 2/4] 1. Change node to address. 2. Remove extra code. --- Sources/KituraNet/FastCGI/FastCGIServer.swift | 31 ++++------------ Sources/KituraNet/HTTP/HTTPServer.swift | 36 +++++++------------ Sources/KituraNet/Server/Server.swift | 23 +++++++----- Tests/KituraNetTests/ClientE2ETests.swift | 2 +- Tests/KituraNetTests/KituraNIOTest.swift | 2 +- .../LifecycleListenerTests.swift | 6 ++-- Tests/KituraNetTests/MonitoringTests.swift | 2 +- Tests/KituraNetTests/PipeliningTests.swift | 4 +-- 8 files changed, 42 insertions(+), 64 deletions(-) diff --git a/Sources/KituraNet/FastCGI/FastCGIServer.swift b/Sources/KituraNet/FastCGI/FastCGIServer.swift index 0342c590..dd56f45a 100644 --- a/Sources/KituraNet/FastCGI/FastCGIServer.swift +++ b/Sources/KituraNet/FastCGI/FastCGIServer.swift @@ -29,8 +29,8 @@ public class FastCGIServer: Server { /// Port number for listening for new connections public private(set) var port: Int? - /// Has the same meaning as in `getaddrinfo()`. - public private(set) var node: String? + /// Has the same meaning as node in `getaddrinfo()`. + public private(set) var address: String? /// A server state. public private(set) var state: ServerState = .unknown @@ -56,38 +56,19 @@ public class FastCGIServer: Server { /// Listens for connections on a socket /// /// - Parameter on: port number for new connections - /// - Parameter node: has the same meaning as in `getaddrinfo()` - public func listen(on port: Int, node: String?) throws { + /// - Parameter address: has the same meaning as node in `getaddrinfo()` + public func listen(on port: Int, address: String?) throws { fatalError("FastCGI is not implemented yet.") } - /// Listens for connections on a socket - /// - /// - Parameter on: port number for new connections - @available(*, deprecated, message: "use 'listen(on:node) throws' instead") - public func listen(on port: Int) throws { - fatalError("FastCGI is not implemented yet.") - } - - /// Static method to create a new `FastCGIServer` and have it listen for conenctions - /// - /// - Parameter on: port number for accepting new connections - /// - Parameter node: has the same meaning as in `getaddrinfo()` - /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections - /// - /// - Returns: a new `FastCGIServer` instance - public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> FastCGIServer { - fatalError("FastCGI not implemented yet.") - } - /// Static method to create a new `FastCGIServer` and have it listen for conenctions /// /// - Parameter on: port number for accepting new connections + /// - Parameter address: has the same meaning as node in `getaddrinfo()` /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections /// /// - Returns: a new `FastCGIServer` instance - @available(*, deprecated, message: "use 'listen(on:node:delegate) throws' instead") - public static func listen(on port: Int, delegate: ServerDelegate?) throws -> FastCGIServer { + public static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> FastCGIServer { fatalError("FastCGI not implemented yet.") } diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index efd9f630..0bd05d28 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -58,8 +58,8 @@ public class HTTPServer: Server { /// The TCP port on which this server listens for new connections. If `nil`, this server does not listen on a TCP socket. public private(set) var port: Int? - /// Has the same meaning as in `getaddrinfo()`. - public private(set) var node: String? + /// Has the same meaning as node in `getaddrinfo()`. + public private(set) var address: String? /// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket. public private(set) var unixDomainSocketPath: String? @@ -235,7 +235,7 @@ public class HTTPServer: Server { // Sockets could either be TCP/IP sockets or Unix domain sockets private enum SocketType { - // An TCP/IP socket has an associated port number and optional node value + // An TCP/IP socket has an associated port number and optional address value case tcp(Int, String?) // A unix domain socket has an associated filename case unix(String) @@ -261,21 +261,16 @@ public class HTTPServer: Server { ### Usage Example: ### ````swift - try server.listen(on: 8080, node: "localhost") + try server.listen(on: 8080, address: "localhost") ```` - Parameter on: Port number for new connections, e.g. 8080 - - Parameter node: has the same meaning as in `getaddrinfo()` + - Parameter address: has the same meaning as node in `getaddrinfo()` */ - public func listen(on port: Int, node: String?) throws { + public func listen(on port: Int, address: String?) throws { self.port = port - self.node = node - try listen(.tcp(port, node)) - } - - @available(*, deprecated, message: "use 'listen(on:node) throws' instead") - public func listen(on port: Int) throws { - try listen(on: port, node: nil) + self.address = address + try listen(.tcp(port, address)) } private func listen(_ socket: SocketType) throws { @@ -320,8 +315,8 @@ public class HTTPServer: Server { let listenerDescription: String do { switch socket { - case SocketType.tcp(let port, let node): - serverChannel = try bootstrap.bind(host: node ?? "0.0.0.0", port: port).wait() + case SocketType.tcp(let port, let address): + serverChannel = try bootstrap.bind(host: address ?? "0.0.0.0", port: port).wait() self.port = serverChannel?.localAddress?.port.map { Int($0) } listenerDescription = "port \(self.port ?? port)" case SocketType.unix(let unixDomainSocketPath): @@ -374,23 +369,18 @@ public class HTTPServer: Server { ```` - Parameter on: Port number for accepting new connections. - - Parameter node: has the same meaning as in `getaddrinfo()` + - Parameter address: has the same meaning as node in `getaddrinfo()` - Parameter delegate: The delegate handler for HTTP connections. - Returns: A new instance of a `HTTPServer`. */ - public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> ServerType { + public static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> ServerType { let server = HTTP.createServer() server.delegate = delegate - try server.listen(on: port, node: node) + try server.listen(on: port, address: address) return server } - @available(*, deprecated, message: "use 'listen(on:node:delagate) throws' instead") - public static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType { - return try listen(on: port, node: nil, delegate: delegate) - } - /** Static method to create a new HTTP server and have it listen for connections on a Unix domain socket. diff --git a/Sources/KituraNet/Server/Server.swift b/Sources/KituraNet/Server/Server.swift index dcee5da6..5d5758e2 100644 --- a/Sources/KituraNet/Server/Server.swift +++ b/Sources/KituraNet/Server/Server.swift @@ -26,8 +26,8 @@ public protocol Server { /// Port number for listening for new connections. var port: Int? { get } - /// Has the same meaning as in `getaddrinfo()`. - var node: String? { get } + /// Has the same meaning as node in `getaddrinfo()`. + var address: String? { get } /// A server state. var state: ServerState { get } @@ -35,23 +35,22 @@ public protocol Server { /// Listen for connections on a socket. /// /// - Parameter on: port number for new connections (eg. 8080) - /// - Parameter node: has the same meaning as in `getaddrinfo()` - func listen(on port: Int, node: String?) throws + /// - Parameter address: has the same meaning as node in `getaddrinfo()` + func listen(on port: Int, address: String?) throws /// Listen for connections on a socket. /// /// - Parameter on: port number for new connections (eg. 8080) - @available(*, deprecated, message: "use 'listen(on:node) throws' with instead") func listen(on port: Int) throws /// Static method to create a new Server and have it listen for connections. /// /// - Parameter on: port number for accepting new connections - /// - Parameter node: has the same meaning as in `getaddrinfo()` + /// - Parameter address: has the same meaning as node in `getaddrinfo()` /// - Parameter delegate: the delegate handler for HTTP connections /// /// - Returns: a new Server instance - static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> ServerType + static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> ServerType /// Static method to create a new Server and have it listen for connections. /// @@ -59,7 +58,6 @@ public protocol Server { /// - Parameter delegate: the delegate handler for HTTP connections /// /// - Returns: a new Server instance - @available(*, deprecated, message: "use 'listen(on:node:delegate) throws' with instead") static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType /// Listen for connections on a socket. @@ -114,3 +112,12 @@ public protocol Server { @discardableResult func clientConnectionFailed(callback: @escaping (Swift.Error) -> Void) -> Self } + +extension Server { + public func listen(on port: Int) throws { + try listen(on: port, address: nil) + } + public static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType { + return try Self.listen(on: port, address: nil, delegate: delegate) + } +} diff --git a/Tests/KituraNetTests/ClientE2ETests.swift b/Tests/KituraNetTests/ClientE2ETests.swift index a200505f..c2959c2c 100644 --- a/Tests/KituraNetTests/ClientE2ETests.swift +++ b/Tests/KituraNetTests/ClientE2ETests.swift @@ -93,7 +93,7 @@ class ClientE2ETests: KituraNetTest { func testEphemeralListeningPort() { do { - let server = try HTTPServer.listen(on: 0, node: "localhost", delegate: delegate) + let server = try HTTPServer.listen(on: 0, address: "localhost", delegate: delegate) _ = HTTP.get("http://localhost:\(server.port!)") { response in XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(String(describing: response?.statusCode))") diff --git a/Tests/KituraNetTests/KituraNIOTest.swift b/Tests/KituraNetTests/KituraNIOTest.swift index eede93b2..542935ea 100644 --- a/Tests/KituraNetTests/KituraNIOTest.swift +++ b/Tests/KituraNetTests/KituraNIOTest.swift @@ -96,7 +96,7 @@ class KituraNetTest: XCTestCase { try server.listen(unixDomainSocketPath: unixDomainSocketPath) } else { server.allowPortReuse = allowPortReuse - try server.listen(on: port, node: "localhost") + try server.listen(on: port, address: "localhost") } return server } diff --git a/Tests/KituraNetTests/LifecycleListenerTests.swift b/Tests/KituraNetTests/LifecycleListenerTests.swift index cd8e4ca2..137f0f43 100644 --- a/Tests/KituraNetTests/LifecycleListenerTests.swift +++ b/Tests/KituraNetTests/LifecycleListenerTests.swift @@ -55,7 +55,7 @@ class LifecycleListenerTests: KituraNetTest { } do { - try server.listen(on: 0, node: "localhost") + try server.listen(on: 0, address: "localhost") self.waitForExpectations(timeout: 5) { error in XCTAssertNil(error) @@ -127,7 +127,7 @@ class LifecycleListenerTests: KituraNetTest { } do { - try server.listen(on: 0, node: "localhost") + try server.listen(on: 0, address: "localhost") self.waitForExpectations(timeout: 5) { error in XCTAssertNil(error) @@ -155,7 +155,7 @@ class LifecycleListenerTests: KituraNetTest { }) do { - try server.listen(on: -1, node: nil) + try server.listen(on: -1, address: nil) } catch { // Do NOT fail the test if an error is thrown. // In this test case an error should be thrown. diff --git a/Tests/KituraNetTests/MonitoringTests.swift b/Tests/KituraNetTests/MonitoringTests.swift index e20d806c..3cf745b9 100644 --- a/Tests/KituraNetTests/MonitoringTests.swift +++ b/Tests/KituraNetTests/MonitoringTests.swift @@ -65,7 +65,7 @@ class MonitoringTests: KituraNetTest { } do { - try server.listen(on: 0, node: nil) + try server.listen(on: 0, address: nil) self.waitForExpectations(timeout: 10) { error in server.stop() XCTAssertNil(error) diff --git a/Tests/KituraNetTests/PipeliningTests.swift b/Tests/KituraNetTests/PipeliningTests.swift index 1599bc2a..b8ea3b61 100644 --- a/Tests/KituraNetTests/PipeliningTests.swift +++ b/Tests/KituraNetTests/PipeliningTests.swift @@ -47,7 +47,7 @@ class PipeliningTests: KituraNetTest { let server = HTTPServer() server.delegate = Delegate() do { - try server.listen(on: 0, node: "localhost") + try server.listen(on: 0, address: "localhost") } catch let error { Log.error("Server failed to listen. Error: \(error)") } @@ -85,7 +85,7 @@ class PipeliningTests: KituraNetTest { server.delegate = Delegate() server.delegate = Delegate() do { - try server.listen(on: 0, node: "localhost") + try server.listen(on: 0, address: "localhost") } catch let error { Log.error("Server failed to listen. Error: \(error)") } From 279f321745a85be0bb6116efa4543b13301904eb Mon Sep 17 00:00:00 2001 From: Tabor Kelly Date: Mon, 20 May 2019 18:13:27 -0700 Subject: [PATCH 3/4] Better documentation comments. --- Sources/KituraNet/FastCGI/FastCGIServer.swift | 9 ++++++--- Sources/KituraNet/HTTP/HTTPServer.swift | 9 ++++++--- Sources/KituraNet/Server/Server.swift | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Sources/KituraNet/FastCGI/FastCGIServer.swift b/Sources/KituraNet/FastCGI/FastCGIServer.swift index dd56f45a..1f068006 100644 --- a/Sources/KituraNet/FastCGI/FastCGIServer.swift +++ b/Sources/KituraNet/FastCGI/FastCGIServer.swift @@ -29,7 +29,8 @@ public class FastCGIServer: Server { /// Port number for listening for new connections public private(set) var port: Int? - /// Has the same meaning as node in `getaddrinfo()`. + /// The address of a network interface to listen on, for example "localhost". The default is nil, + /// which listens for connections on all interfaces. public private(set) var address: String? /// A server state. @@ -56,7 +57,8 @@ public class FastCGIServer: Server { /// Listens for connections on a socket /// /// - Parameter on: port number for new connections - /// - Parameter address: has the same meaning as node in `getaddrinfo()` + /// - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this + /// server will listen on all interfaces. public func listen(on port: Int, address: String?) throws { fatalError("FastCGI is not implemented yet.") } @@ -64,7 +66,8 @@ public class FastCGIServer: Server { /// Static method to create a new `FastCGIServer` and have it listen for conenctions /// /// - Parameter on: port number for accepting new connections - /// - Parameter address: has the same meaning as node in `getaddrinfo()` + /// - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this + /// server will listen on all interfaces. /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections /// /// - Returns: a new `FastCGIServer` instance diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index 0bd05d28..fd2e07e9 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -58,7 +58,8 @@ public class HTTPServer: Server { /// The TCP port on which this server listens for new connections. If `nil`, this server does not listen on a TCP socket. public private(set) var port: Int? - /// Has the same meaning as node in `getaddrinfo()`. + /// The address of a network interface to listen on, for example "localhost". The default is nil, + /// which listens for connections on all interfaces. public private(set) var address: String? /// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket. @@ -265,7 +266,8 @@ public class HTTPServer: Server { ```` - Parameter on: Port number for new connections, e.g. 8080 - - Parameter address: has the same meaning as node in `getaddrinfo()` + - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this server + will listen on all interfaces. */ public func listen(on port: Int, address: String?) throws { self.port = port @@ -369,7 +371,8 @@ public class HTTPServer: Server { ```` - Parameter on: Port number for accepting new connections. - - Parameter address: has the same meaning as node in `getaddrinfo()` + - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this server + will listen on all interfaces. - Parameter delegate: The delegate handler for HTTP connections. - Returns: A new instance of a `HTTPServer`. diff --git a/Sources/KituraNet/Server/Server.swift b/Sources/KituraNet/Server/Server.swift index 5d5758e2..7e40a761 100644 --- a/Sources/KituraNet/Server/Server.swift +++ b/Sources/KituraNet/Server/Server.swift @@ -26,7 +26,8 @@ public protocol Server { /// Port number for listening for new connections. var port: Int? { get } - /// Has the same meaning as node in `getaddrinfo()`. + /// The address of a network interface to listen on, for example "localhost". The default is nil, + /// which listens for connections on all interfaces. var address: String? { get } /// A server state. @@ -35,7 +36,8 @@ public protocol Server { /// Listen for connections on a socket. /// /// - Parameter on: port number for new connections (eg. 8080) - /// - Parameter address: has the same meaning as node in `getaddrinfo()` + /// - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this + /// server will listen on all interfaces. func listen(on port: Int, address: String?) throws /// Listen for connections on a socket. @@ -46,7 +48,8 @@ public protocol Server { /// Static method to create a new Server and have it listen for connections. /// /// - Parameter on: port number for accepting new connections - /// - Parameter address: has the same meaning as node in `getaddrinfo()` + /// - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this + /// server will listen on all interfaces. /// - Parameter delegate: the delegate handler for HTTP connections /// /// - Returns: a new Server instance From 9a46b2c4db16674d1ba3c70dc25af6e965b25912 Mon Sep 17 00:00:00 2001 From: Tabor Kelly Date: Thu, 23 May 2019 10:30:44 -0700 Subject: [PATCH 4/4] Make funtion signatures for FastCGIServer and HTTPServer match Kitura-Net exactly. --- Sources/KituraNet/FastCGI/FastCGIServer.swift | 4 ++-- Sources/KituraNet/HTTP/HTTPServer.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/KituraNet/FastCGI/FastCGIServer.swift b/Sources/KituraNet/FastCGI/FastCGIServer.swift index 1f068006..2c268da4 100644 --- a/Sources/KituraNet/FastCGI/FastCGIServer.swift +++ b/Sources/KituraNet/FastCGI/FastCGIServer.swift @@ -59,7 +59,7 @@ public class FastCGIServer: Server { /// - Parameter on: port number for new connections /// - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this /// server will listen on all interfaces. - public func listen(on port: Int, address: String?) throws { + public func listen(on port: Int, address: String? = nil) throws { fatalError("FastCGI is not implemented yet.") } @@ -71,7 +71,7 @@ public class FastCGIServer: Server { /// - Parameter delegate: the delegate handler for FastCGI/HTTP connections /// /// - Returns: a new `FastCGIServer` instance - public static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> FastCGIServer { + public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> FastCGIServer { fatalError("FastCGI not implemented yet.") } diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index fd2e07e9..bf40eb5c 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -269,7 +269,7 @@ public class HTTPServer: Server { - Parameter address: The address of the network interface to listen on. Defaults to nil, which means this server will listen on all interfaces. */ - public func listen(on port: Int, address: String?) throws { + public func listen(on port: Int, address: String? = nil) throws { self.port = port self.address = address try listen(.tcp(port, address)) @@ -377,7 +377,7 @@ public class HTTPServer: Server { - Returns: A new instance of a `HTTPServer`. */ - public static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> ServerType { + public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> ServerType { let server = HTTP.createServer() server.delegate = delegate try server.listen(on: port, address: address)