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 = trueDeclaration
Swift
public var allowPortReuse: Bool -
Controls the maximum number of requests per Keep-Alive connection.
Usage Example:
httpServer.keepAliveState = .unlimitedDeclaration
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 = sslConfigurationDeclaration
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) throwsParameters
portPort number for new connections, e.g. 8080
addressThe 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) throwsParameters
unixDomainSocketPathUnix 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 -> HTTPServerParameters
onPort number for accepting new connections.
addressThe address of a network interface to listen on, for example “localhost”. The default is nil, which listens for connections on all interfaces.
delegateThe 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 -> HTTPServerParameters
unixDomainSocketPathThe path of the Unix domain socket that this server should listen on.
delegateThe 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
portport number for new connections (eg. 8080)
errorHandleroptional 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) -> HTTPServerParameters
portport number for new connections (eg. 8080)
delegateThe delegate handler for HTTP connections.
errorHandleroptional callback for error handling
Return Value
A new
HTTPServerinstance. -
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) -> SelfParameters
callbackThe listener callback that will run after a successfull start-up.
Return Value
A
HTTPServerinstance. -
Add a new listener for a server being stopped.
Usage Example:
server.stopped(callback: callBack)Declaration
Swift
@discardableResult public func stopped(callback: @escaping () -> Void) -> SelfParameters
callbackThe listener callback that will run when the server stops.
Return Value
A
HTTPServerinstance. -
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) -> SelfParameters
callbackThe listener callback that will run when the server throws an error.
Return Value
A
HTTPServerinstance. -
Add a new listener for when
listenSocket.acceptClientConnectionthrows an error.Usage Example:
server.clientConnectionFailed(callback: callBack)Declaration
Swift
@discardableResult public func clientConnectionFailed(callback: @escaping (Swift.Error) -> Void) -> SelfParameters
callbackThe listener callback that will run on server after successfull start-up.
Return Value
A
HTTPServerinstance. -
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
IncomingSockerProcessors for use with new incoming sockets.Usage Example:
server.register(incomingSocketProcessorCreator: creator)Declaration
Swift
public static func register(incomingSocketProcessorCreator creator: IncomingSocketProcessorCreator)Parameters
incomingSocketProcessorCreatorAn implementation of the
IncomingSocketProcessorCreatorprotocol which creates an implementation of theIncomingSocketProcessorprotocol to process the data from a new incoming socket.
View on GitHub
HTTPServer Class Reference