HTTPServer
public class HTTPServer : Server
An HTTP server that listens for connections on a socket.
Usage Example:
//Create a server that listens for connections on a specified socket.
let server = try HTTPServer.listen(on: 0, delegate: delegate)
...
//Stop the server.
server.stop()
-
Declaration
Swift
public typealias ServerType = HTTPServer
-
Declaration
Swift
public var delegate: ServerDelegate?
-
The TCP port on which this server listens for new connections. If
nil
, this server does not listen on a TCP socket.Declaration
Swift
public private(set) var port: Int? { get }
-
The address of the network interface to listen on. Defaults to nil, which means this server will listen on all interfaces.
Declaration
Swift
public private(set) var address: String? { get }
-
The Unix domain socket path on which this server listens for new connections. If
nil
, this server does not listen on a Unix socket.Declaration
Swift
public private(set) var unixDomainSocketPath: String? { get }
-
A server state
Usage Example:
if(httpSever.state == .unknown) { httpServer.stop() }
Declaration
Swift
public private(set) var state: ServerState { get }
-
Whether or not this server allows port reuse (default: disallowed).
Usage Example:
httpServer.allowPortReuse = true
Declaration
Swift
public var allowPortReuse: Bool
-
Controls the maximum number of requests per Keep-Alive connection.
Usage Example:
httpServer.keepAliveState = .unlimited
Declaration
Swift
public var keepAliveState: KeepAliveState
-
Controls policies relating to incoming connections and requests.
Declaration
Swift
public var options: ServerOptions { get set }
-
SSL cert configuration for handling client requests.
Usage Example:
httpServer.sslConfig = sslConfiguration
Declaration
Swift
public var sslConfig: SSLService.Configuration?
-
Creates an HTTP server object.
Usage Example:
let server = HTTPServer() server.listen(on: 8080)
Declaration
Swift
public init()
-
Listens for connections on a TCP socket.
Usage Example:
try server.listen(on: 8080, address: "localhost")
Declaration
Swift
public func listen(on port: Int, address: String? = nil) throws
Parameters
port
Port number for new connections, e.g. 8080
address
The address of a network interface to listen on, for example “localhost”. The default is nil, which listens for connections on all interfaces.
-
Listens for connections on a Unix socket.
Usage Example:
try server.listen(unixDomainSocketPath: "/my/path")
Declaration
Swift
public func listen(unixDomainSocketPath: String) throws
Parameters
unixDomainSocketPath
Unix socket path for new connections, eg. “/my/path”
-
Static method to create a new HTTP server and have it listen for connections.
Usage Example:
let server = HTTPServer.listen(on: 8080, address: "localhost", delegate: self)
Declaration
Swift
public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> HTTPServer
Parameters
on
Port number for accepting new connections.
address
The address of a network interface to listen on, for example “localhost”. The default is nil, which listens for connections on all interfaces.
delegate
The delegate handler for HTTP connections.
Return Value
A new instance of a
HTTPServer
. -
Static method to create a new HTTP server and have it listen for connections on a Unix domain socket.
Usage Example:
let server = HTTPServer.listen(unixDomainSocketPath: "/my/path", delegate: self)
Declaration
Swift
public static func listen(unixDomainSocketPath: String, delegate: ServerDelegate?) throws -> HTTPServer
Parameters
unixDomainSocketPath
The path of the Unix domain socket that this server should listen on.
delegate
The delegate handler for HTTP connections.
Return Value
A new instance of a
HTTPServer
. -
Listen for connections on a socket.
Usage Example:
try server.listen(on: 8080, errorHandler: errorHandler)
Declaration
Swift
@available(*, deprecated, message: "use 'listen(on:﹚ throws' with 'server.failed(callback:﹚' instead") public func listen(port: Int, errorHandler: ((Swift.Error) -> Void)? = nil)
Parameters
port
port number for new connections (eg. 8080)
errorHandler
optional callback for error handling
-
Static method to create a new HTTPServer and have it listen for connections.
Usage Example:
let server = HTTPServer(port: 8080, delegate: self, errorHandler: errorHandler)
Declaration
Swift
@available(*, deprecated, message: "use 'listen(on:delegate:﹚ throws' with 'server.failed(callback:﹚' instead") public static func listen(port: Int, delegate: ServerDelegate, errorHandler: ((Swift.Error) -> Void)? = nil) -> HTTPServer
Parameters
port
port number for new connections (eg. 8080)
delegate
The delegate handler for HTTP connections.
errorHandler
optional callback for error handling
Return Value
A new
HTTPServer
instance. -
Stop listening for new connections.
Usage Example:
server.stop()
Declaration
Swift
public func stop()
-
Add a new listener for a server being started.
Usage Example:
server.started(callback: callBack)
Declaration
Swift
@discardableResult public func started(callback: @escaping () -> Void) -> Self
Parameters
callback
The listener callback that will run after a successfull start-up.
Return Value
A
HTTPServer
instance. -
Add a new listener for a server being stopped.
Usage Example:
server.stopped(callback: callBack)
Declaration
Swift
@discardableResult public func stopped(callback: @escaping () -> Void) -> Self
Parameters
callback
The listener callback that will run when the server stops.
Return Value
A
HTTPServer
instance. -
Add a new listener for a server throwing an error.
Usage Example:
server.started(callback: callBack)
Declaration
Swift
@discardableResult public func failed(callback: @escaping (Swift.Error) -> Void) -> Self
Parameters
callback
The listener callback that will run when the server throws an error.
Return Value
A
HTTPServer
instance. -
Add a new listener for when
listenSocket.acceptClientConnection
throws an error.Usage Example:
server.clientConnectionFailed(callback: callBack)
Declaration
Swift
@discardableResult public func clientConnectionFailed(callback: @escaping (Swift.Error) -> Void) -> Self
Parameters
callback
The listener callback that will run on server after successfull start-up.
Return Value
A
HTTPServer
instance. -
Wait for all of the listeners to stop.
Usage Example:
server.waitForListeners()
Todo
This calls the ListenerGroup object, and is left in for backwards compatability. It can be safely removed once Kitura is patched to talk directly to ListenerGroup.Declaration
Swift
@available(*, deprecated, message: "Will be removed in future versions. Use ListenerGroup.waitForListeners(﹚ directly.") public static func waitForListeners()
-
Register a class that creates
IncomingSockerProcessor
s for use with new incoming sockets.Usage Example:
server.register(incomingSocketProcessorCreator: creator)
Declaration
Swift
public static func register(incomingSocketProcessorCreator creator: IncomingSocketProcessorCreator)
Parameters
incomingSocketProcessorCreator
An implementation of the
IncomingSocketProcessorCreator
protocol which creates an implementation of theIncomingSocketProcessor
protocol to process the data from a new incoming socket.