Description
Redirect to a normalized path.
Description
Interprets dots and slashes and issues a redirect to a normalized path if the request target is not already in normal form.
README.md
Scotty path normalizer
This library provides a Scotty action that normalizes the HTTP request target as if it were a Unix file path. When the path normalization action detects a path that can be simplified in one of the following ways, it issues a redirect to a more canonical path.
- Remove trailing slashes:
https://typeclasses.com/contravariance/
becomeshttps://typeclasses.com/contravariance
- Remove double slashes:
https://typeclasses.com//web-servers////lesson-4
becomeshttps://typeclasses.com/web-servers/lesson-4
- Remove
.
segments, because.
represents "the current directory":https://typeclasses.com/ghc/./scoped-type-variables
becomeshttps://typeclasses.com/ghc/scoped-type-variables
- Remove segments of the form
xyz/..
, because..
represents "the parent directory":https://typeclasses.com/python/../javascript/monoidal-folds
becomeshttps://typeclasses.com/javascript/monoidal-folds
The typical way to apply this to your Scotty server is to put addPathNormalizer
at the top of your ScottyM
app definition.
import qualified Web.Scotty as Scotty
import Web.Scotty.PathNormalizer (addPathNormalizer)
main :: IO ()
main =
Scotty.scotty 3000 $
do
addPathNormalizer
Scotty.get (Scotty.capture "/:word") $
do
beam <- Scotty.param (Data.Text.Lazy.pack "word")
Scotty.html $ fold
[ Data.Text.Lazy.pack "<h1>Scotty, "
, beam
, Data.Text.Lazy.pack " me up!</h1>"
]