Skip to content

Instantly share code, notes, and snippets.

@plaflamme
Created April 26, 2014 14:30
Show Gist options
  • Save plaflamme/11321585 to your computer and use it in GitHub Desktop.
Save plaflamme/11321585 to your computer and use it in GitHub Desktop.
object RedirectFilter extends SimpleFilter[HttpRequest, HttpResponse] {
def apply(req: HttpRequest, serv: Service[HttpRequest, HttpResponse] = {
serv(req)
.flatMap { res =>
if(res.statusCode == 302) { // Obviously, you can handle 301 and 307 as well
val uri = res.headers.get("Location")
// You'll want to check if uri is to the same host.
// You'll want to copy all of req (headers, body, etc) and only replace uri, this is incomplete:
val redirect = RequestBuilder().create.uri(uri).buildGet
serv(redirect)
} else Future.const(res)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment