Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active December 10, 2015 21:09
Show Gist options
  • Save hamnis/4493514 to your computer and use it in GitHub Desktop.
Save hamnis/4493514 to your computer and use it in GitHub Desktop.
Unfiltered kit for detecting Forwarded Proto and setting secure flag
package unfilteredx.kit
import unfiltered.request._
import unfiltered.response._
object Secure {
def secure[A,B](intent: unfiltered.Cycle.Intent[A,B]): unfiltered.Cycle.Intent[A,B] = {
case req@ForwardedProto(proto) if (intent.isDefinedAt(req)) => intent(wrap(req, proto.toLowerCase))
case req if (intent.isDefinedAt(req)) => intent(req)
}
private def wrap[A](req: HttpRequest[A], proto: String) = new DelegatingRequest(req) {
override def isSecure = "https" == proto
override def protocol = proto
}
}
object ForwardedProto extends StringHeader("Forwarded-Proto")
package unfilteredx.kit
import unfiltered.request._
import unfiltered.response._
import unfiltered.filter._
import javax.servlet._
import javax.servlet.http._
object SecureFilter {
type Req = HttpServletRequest
type Res = HttpServletResponse
def secure(intent: unfiltered.Cycle.Intent[Req,Res]): unfiltered.Cycle.Intent[Req,Res] = {
case req@ForwardedProto(proto) if (intent.isDefinedAt(req)) => intent(wrap(req, proto.toLowerCase))
case req if (intent.isDefinedAt(req)) => intent(req)
}
private def wrap[A](req: HttpRequest[Req], proto: String) = new RequestBinding(new HttpServletRequestWrapper(req.underlying) {
override def getScheme = proto
override def isSecure = proto == "https"
})
}
object ForwardedProto extends StringHeader("Forwarded-Proto")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment