Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/typeclasses/scotty-path-normalizer
Scotty action to redirect to a normalized path (remove trailing slash, "..", etc.)
https://github.com/typeclasses/scotty-path-normalizer
haskell haskell-library http-server
Last synced: 2 months ago
JSON representation
Scotty action to redirect to a normalized path (remove trailing slash, "..", etc.)
- Host: GitHub
- URL: https://github.com/typeclasses/scotty-path-normalizer
- Owner: typeclasses
- License: mit
- Created: 2018-10-12T17:06:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-14T04:33:19.000Z (almost 6 years ago)
- Last Synced: 2024-04-27T09:40:26.630Z (10 months ago)
- Topics: haskell, haskell-library, http-server
- Language: Haskell
- Homepage: https://hackage.haskell.org/package/scotty-path-normalizer
- Size: 12.7 KB
- Stars: 2
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# 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.1. Remove trailing slashes: `https://typeclasses.com/contravariance/`
becomes `https://typeclasses.com/contravariance`
2. Remove double slashes: `https://typeclasses.com//web-servers////lesson-4`
becomes `https://typeclasses.com/web-servers/lesson-4`
3. Remove `.` segments, because `.` represents "the current directory":
`https://typeclasses.com/ghc/./scoped-type-variables` becomes
`https://typeclasses.com/ghc/scoped-type-variables`
4. Remove segments of the form `xyz/..`, because `..` represents "the parent
directory": `https://typeclasses.com/python/../javascript/monoidal-folds`
becomes `https://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.```haskell
import qualified Web.Scotty as Scotty
import Web.Scotty.PathNormalizer (addPathNormalizer)main :: IO ()
main =
Scotty.scotty 3000 $
do
addPathNormalizerScotty.get (Scotty.capture "/:word") $
do
beam <- Scotty.param (Data.Text.Lazy.pack "word")
Scotty.html $ fold
[ Data.Text.Lazy.pack "Scotty, "
"
, beam
, Data.Text.Lazy.pack " me up!
]
```[Scotty]: https://hackage.haskell.org/package/scotty
[redirect]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302