Last active
December 10, 2015 21:09
-
-
Save hamnis/4493514 to your computer and use it in GitHub Desktop.
Unfiltered kit for detecting Forwarded Proto and setting secure flag
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
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") |
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
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