Skip to content

Instantly share code, notes, and snippets.

@kjessup
Last active July 8, 2016 17:52
Show Gist options
  • Save kjessup/d4d96ee191e26304994deb14f26892ae to your computer and use it in GitHub Desktop.
Save kjessup/d4d96ee191e26304994deb14f26892ae to your computer and use it in GitHub Desktop.
Perfect 404 response filter
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