Repository: vapor/vapor
File: Sources/Vapor/EngineServer.swift (Swift)

Code before:
import HTTP
import TCP

/// A TCP based server with HTTP parsing and serialization pipeline.
public final class EngineServer: Server {
    /// Chosen configuration for this server.
    public let config: EngineServerConfig

    /// Create a new EngineServer using config struct.
    public init(config: EngineServerConfig) {
        self.config = config
    }

    /// Start the server. Server protocol requirement.
    public func start(with responder: Responder) throws {
        // create a tcp server
        let tcp = try TCP.Server(workerCount: config.workerCount)
        let securityLayer = PeerValidationStream(maxConnectionsPerIP: config.maxConnectionsPerIP)
        let secureTCP = tcp.stream(to: securityLayer)
        
        let server = HTTP.Server(server: secureTCP)
        
        // setup the server pipeline
        server.drain { client in
            let parser = HTTP.RequestParser(queue: client.tcp.queue)
            let responderStream = responder.makeStream()
            let serializer = HTTP.ResponseSerializer()

            client.stream(to: parser)
                .stream(to: responderStream)
                .stream(to: serializer)
                .drain(into: client)

            client.tcp.start()
        }

        server.errorStream = { error in
            debugPrint(error)
        }

        // bind, listen, and start accepting
        try tcp.start(
            hostname: config.hostname,
            port: config.port,

Review comment (security):
I think this could be achieved without touching the HTTP layer by adding a `beforeAccept: BeforeAcceptHandler?` to the `TCP.Server`.

```swift
let tcp = try TCP.Server(workerCount: config.workerCount)
let securityLayer = PeerValidationStream(maxConnectionsPerIP: config.maxConnectionsPerIP)
tcp.beforeAccept = { client in
     securityLayer.foo(bar: client)
     /// return true if should accept, return false if shouldn't
}

let server = HTTP.Server(tcp: tcp)
...
```

Code after:
import HTTP
import ServerSecurity
import TCP

/// A TCP based server with HTTP parsing and serialization pipeline.
public final class EngineServer: HTTPServer {
    /// Chosen configuration for this server.
    public let config: EngineServerConfig

    /// Create a new EngineServer using config struct.
    public init(config: EngineServerConfig) {
        self.config = config
    }

    /// Start the server. Server protocol requirement.
    public func start(with responder: Responder) throws {
        // create a tcp server
        let tcp = try TCP.Server(workerCount: config.workerCount)
        
        tcp.willAccept = PeerValidator(maxConnectionsPerIP: config.maxConnectionsPerIP).willAccept
        
        let server = HTTP.Server(clientStream: tcp)
        
        // setup the server pipeline
        server.drain { client in
            let parser = HTTP.RequestParser(worker: client.tcp.worker)
            let responderStream = responder.makeStream()
            let serializer = HTTP.ResponseSerializer()

            client.stream(to: parser)
                .stream(to: responderStream)
                .stream(to: serializer)
                .drain(into: client)

            client.tcp.start()
        }.catch { error in
            debugPrint(error)
        }

        // bind, listen, and start accepting
        try tcp.start(
            hostname: config.hostname,
            port: config.port,
            backlog: config.backlog