scotty-path-normalizer: Redirect to a normalized path

[ library, mit, web ] [ Propose Tags ] [ Report a vulnerability ]

Interprets dots and slashes and issues a redirect to a normalized path if the request target is not already in normal form.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4.7 && <5), bytestring, scotty, text, wai [details]
Tested with ghc ==8.6.1, ghc ==8.4.3, ghc ==8.2.2
License MIT
Copyright 2018 Typeclass Consulting, LLC
Author Chris Martin
Maintainer Chris Martin, Julie Moronuki
Category Web
Home page https://github.com/typeclasses/scotty-path-normalizer
Bug tracker https://github.com/typeclasses/scotty-path-normalizer/issues
Source repo head: git clone https://github.com/typeclasses/scotty-path-normalizer
Uploaded by chris_martin at 2018-10-22T23:40:31Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 753 total (9 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-10-22 [all 1 reports]

Readme for scotty-path-normalizer-0.1.0.0

[back to package description]

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.

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>"
                ]