Skip to content

Instantly share code, notes, and snippets.

@tgpfeiffer
Created June 6, 2013 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgpfeiffer/5719872 to your computer and use it in GitHub Desktop.
Save tgpfeiffer/5719872 to your computer and use it in GitHub Desktop.
A replacement for S.hostAndPath that takes into account Lift running on a host with a different port and SSL setting than the reverse proxy in front of it.
def realHostAndPath(forceNoSsl: Boolean = false): String =
S.request.flatMap(req => (Box !! req.request).map(r => {
// X-Forwarded-Host contains host *and* port (if not standard)
r.header("X-Forwarded-Host") match {
case Full(hostAndPort) =>
r.scheme match {
case "http" if r.header("X-SSL").isDefined =>
"https://" + hostAndPort + S.contextPath
case "http" if r.header("X_FORWARDED_PROTO") == Full("https") && !forceNoSsl =>
// this (correct) URL is not liked well by Java applets, so we allow to override it
"https://" + hostAndPort + S.contextPath
case sch => sch + "://" + hostAndPort + S.contextPath
}
case _ =>
(r.scheme, r.serverPort) match {
case ("http", 80) if r.header("X-SSL").isDefined => "https://" + req.hostName + S.contextPath
case ("http", 80) => "http://" + req.hostName + S.contextPath
case ("https", 443) => "https://" + req.hostName + S.contextPath
case (sch, port) => sch + "://" + req.hostName + ":" + port + S.contextPath
}
}
})
).getOrElse("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment