Skip to content

Instantly share code, notes, and snippets.

@richdougherty
Created February 7, 2014 22:38
Show Gist options
  • Save richdougherty/8873469 to your computer and use it in GitHub Desktop.
Save richdougherty/8873469 to your computer and use it in GitHub Desktop.
Strip duplicate leading slashes before both routing and handling requests
import play.api._
import play.api.mvc._
import scala.concurrent.Future
object RewrittenRequestHeader {
def rewrite(header: RequestHeader): RequestHeader = {
if (header.path.startsWith("//")) {
val newPath = header.path.substring(1)
RewrittenRequestHeader(newPath, header)
} else {
header
}
}
}
case class RewrittenRequestHeader(val path: String, delegate: RequestHeader) extends RequestHeader {
def id = delegate.id
def tags = delegate.tags
def uri = delegate.uri
def method = delegate.method
def version = delegate.version
def queryString = delegate.queryString
def remoteAddress = delegate.remoteAddress
def headers = delegate.headers
}
object StripLeadingSlash extends Filter {
def apply(next: (RequestHeader) => Future[SimpleResult])(header: RequestHeader): Future[SimpleResult] = {
next(RewrittenRequestHeader.rewrite(header))
}
}
object Global extends WithFilters(StripLeadingSlash) {
override def onRouteRequest(header: RequestHeader): Option[Handler] = {
super.onRouteRequest(RewrittenRequestHeader.rewrite(header))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment