Last active
July 8, 2016 17:52
-
-
Save kjessup/d4d96ee191e26304994deb14f26892ae to your computer and use it in GitHub Desktop.
Perfect 404 response filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Filter404: HTTPResponseFilter { | |
func filterBody(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) { | |
callback(.continue) | |
} | |
func filterHeaders(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) { | |
if case .notFound = response.status { | |
response.bodyBytes.removeAll() | |
response.appendBody(string: "The file \(response.request.path) was not found.") | |
response.setHeader(.contentLength, value: "\(response.bodyBytes.count)") | |
callback(.done) | |
} else { | |
callback(.continue) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment