         let filePaths = filePath.characters.split{$0 == "/"}.map(String.init)
         let fileName = filePaths.last
         setHeader("Content-Disposition", value: "attachment; fileName = \"\(fileName!)\"")
-        
+
         let contentType =  ContentType.contentTypeForFile(fileName!)
         if  let contentType = contentType  {
             setHeader("Content-Type", value: contentType)
         }
     }
+
+    ///
+    /// Sets the pre-flush lifecycle handler and returns the previous one
+    ///
+    /// - Parameter newPreFlush: The new pre-flush lifecycle handler
+    public func setPreFlushHandler(newPreFlush: PreFlushLifecycleHandler) -> PreFlushLifecycleHandler {
+        let oldPreFlush = preFlush
+        preFlush = newPreFlush
+        return oldPreFlush
+    }
 }
+
+///
+/// Type alias for "Before flush" (i.e. before headers and body are written) lifecycle handler
+public typealias PreFlushLifecycleHandler = (request: RouterRequest, response: RouterResponse) -> Void

    /// - Returns: a RouterResponse instance
    ///
    public func end() throws -> RouterResponse {

        if  let data = buffer.data  {
            let contentLength = getHeader("Content-Length")

    ///
    var invokedEnd = false

    //
    // Current pre-flush lifecycle handler
    //
    private var preFlush: PreFlushLifecycleHandler = {request, response in }

    ///
    /// Set of cookies to return with the response
    ///
