ServerOptions

public struct ServerOptions

ServerOptions allows customization of default server policies, including:

  • requestSizeLimit: Defines the maximum size for the body of an incoming request, in bytes. If a request body is larger than this limit, it will be rejected and the connection will be closed. A value of nil means no limit.
  • connectionLimit: Defines the maximum number of concurrent connections that a server should accept. Clients attempting to connect when this limit has been reached will be rejected. A value of nil means no limit.

The server can optionally respond to the client with a message in either of these cases. This message can be customized by defining requestSizeResponseGenerator and connectionResponseGenerator.

Example usage:

let server = HTTP.createServer()
server.options = ServerOptions(requestSizeLimit: 1000, connectionLimit: 10)
  • A default limit of 100mb on the size of the request body that a server should accept.

    Declaration

    Swift

    public static let defaultRequestSizeLimit: Int
  • A default limit of 10,000 on the number of concurrent connections that a server should accept.

    Declaration

    Swift

    public static let defaultConnectionLimit: Int
  • Defines a default response to an over-sized request of HTTP 413: Request Too Long. A message is also logged at debug level.

    Declaration

    Swift

    public static let defaultRequestSizeResponseGenerator: (Int, String) -> (HTTPStatusCode, String)?
  • Defines a default response when refusing a new connection of HTTP 503: Service Unavailable. A message is also logged at debug level.

    Declaration

    Swift

    public static let defaultConnectionResponseGenerator: (Int, String) -> (HTTPStatusCode, String)?
  • Defines the maximum size for the body of an incoming request, in bytes. If a request body is larger than this limit, it will be rejected and the connection will be closed.

    A value of nil means no limit.

    Declaration

    Swift

    public let requestSizeLimit: Int?
  • Defines the maximum number of concurrent connections that a server should accept. Clients attempting to connect when this limit has been reached will be rejected.

    Declaration

    Swift

    public let connectionLimit: Int?
  • Determines the response message and HTTP status code used to respond to clients whose request exceeds the requestSizeLimit. The current limit and client’s address are provided as parameters to enable a message to be logged, and/or a response to be provided back to the client.

    The returned tuple indicates the HTTP status code and response body to send to the client. If nil is returned, then no response will be sent.

    Example usage:

    let oversizeResponse: (Int, String) -> (HTTPStatusCode, String)? = { (limit, client) in
        Log.debug("Rejecting request from \(client): Exceeds limit of \(limit) bytes")
        return (.requestTooLong, "Your request exceeds the limit of \(limit) bytes.\r\n")
    }
    

    Declaration

    Swift

    public let requestSizeResponseGenerator: (Int, String) -> (HTTPStatusCode, String)?
  • Determines the response message and HTTP status code used to respond to clients that attempt to connect while the server is already servicing the maximum number of connections, as defined by connectionLimit. The current limit and client’s address are provided as parameters to enable a message to be logged, and/or a response to be provided back to the client.

    The returned tuple indicates the HTTP status code and response body to send to the client. If nil is returned, then no response will be sent.

    Example usage:

    let connectionResponse: (Int, String) -> (HTTPStatusCode, String)? = { (limit, client) in
        Log.debug("Rejecting request from \(client): Connection limit \(limit) reached")
        return (.serviceUnavailable, "Service busy - please try again later.\r\n")
    }
    

    Declaration

    Swift

    public let connectionResponseGenerator: (Int, String) -> (HTTPStatusCode, String)?
  • Create a ServerOptions to determine the behaviour of a Server.

    Declaration

    Swift

    public init(requestSizeLimit: Int? = ServerOptions.defaultRequestSizeLimit,
                connectionLimit: Int? = ServerOptions.defaultConnectionLimit,
                requestSizeResponseGenerator: @escaping (Int, String) -> (HTTPStatusCode, String)? = ServerOptions.defaultRequestSizeResponseGenerator,
                connectionResponseGenerator: @escaping (Int, String) -> (HTTPStatusCode, String)? = ServerOptions.defaultConnectionResponseGenerator)

    Parameters

    requestSizeLimit

    The maximum size of an incoming request body. Defaults to ServerOptions.defaultRequestSizeLimit.

    connectionLimit

    The maximum number of concurrent connections. Defaults to ServerOptions.defaultConnectionLimit.

    requestSizeResponseGenerator

    A closure producing a response to send to a client when an over-sized request is rejected. Defaults to ServerOptions.defaultRequestSizeResponseGenerator.

    defaultConnectionResponseGenerator

    A closure producing a response to send to a client when a the server is busy and new connections are not being accepted. Defaults to ServerOptions.defaultConnectionResponseGenerator.