Created
February 7, 2014 22:38
-
-
Save richdougherty/8873469 to your computer and use it in GitHub Desktop.
Strip duplicate leading slashes before both routing and handling requests
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
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