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
  • HTTP ServerDelegate.

    Usage Example:

    httpServer.delegate = self
    

    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?
  • The address of a network interface to listen on, for example localhost. The default is nil, which listens for connections on all interfaces.

    Declaration

    Swift

    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.

    Declaration

    Swift

    public private(set) var unixDomainSocketPath: String?
  • A server state

    Usage Example:

    if(httpSever.state == .unknown) {
       httpServer.stop()
    }
    

    Declaration

    Swift

    public private(set) var state: ServerState { get set }
  • Controls the maximum number of requests per Keep-Alive connection.

    Usage Example:

    httpServer.keepAliveState = .unlimited
    

    Declaration

    Swift

    public var keepAliveState: KeepAliveState
  • Whether or not this server allows port reuse (default: disallowed).

    Usage Example:

    httpServer.allowPortReuse = true
    

    Declaration

    Swift

    public var allowPortReuse: Bool
  • server configuration

    Declaration

    Swift

    public var options: ServerOptions
  • Creates an HTTP server object.

    Usage Example:

    let config =HTTPServerConfiguration(requestSize: 1000, coonectionLimit: 100)
    let server = HTTPServer(serverconfig: config)
    server.listen(on: 8080)
    

    Declaration

    Swift

    public init(options: ServerOptions = ServerOptions())
  • SSL cert configuration for handling client requests.

    Usage Example:

    httpServer.sslConfig = sslConfiguration
    

    Declaration

    Swift

    public var sslConfig: SSLService.Configuration? { get set }
  • 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

  • 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

    on

    Port number for new connections, e.g. 8080

    address

    The address of the network interface to listen on. Defaults to nil, which means this server will listen on all interfaces.

  • Static method to create a new HTTP server and have it listen for connections.

    Usage Example:

    let server = HTTPServer.listen(on: 8080, node: "localhost", delegate: self)
    

    Declaration

    Swift

    public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> ServerType

    Parameters

    on

    Port number for accepting new connections.

    address

    The address of the network interface to listen on. Defaults to nil, which means this server will listen 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)?)

    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)?) -> ServerType

    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.