{-# LANGUAGE CPP #-}
{-# LANGUAGE TupleSections #-}
module Network.Wai.Middleware.Rewrite (
PathsAndQueries,
rewriteWithQueries,
rewritePureWithQueries,
rewriteRoot,
rewrite,
rewritePure,
rewriteRequest,
rewriteRequestPure,
) where
#if __GLASGOW_HASKELL__ <= 710
import Control.Applicative
#endif
import Control.Monad.IO.Class (liftIO)
import Data.Functor.Identity (Identity (..))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Network.HTTP.Types as H
import Network.Wai
type PathsAndQueries = ([Text], H.Query)
rewrite :: ([Text] -> H.RequestHeaders -> IO [Text]) -> Middleware
rewrite :: ([Text] -> RequestHeaders -> IO [Text]) -> Middleware
rewrite [Text] -> RequestHeaders -> IO [Text]
convert Application
app Request
req Response -> IO ResponseReceived
sendResponse = do
let convertIO :: ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convertIO = ([Text] -> RequestHeaders -> IO [Text])
-> ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
forall (m :: * -> *).
(Applicative m, Monad m) =>
([Text] -> RequestHeaders -> m [Text])
-> ([Text], Query) -> RequestHeaders -> m ([Text], Query)
pathsOnly (([Text] -> RequestHeaders -> IO [Text])
-> ([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> ((([Text], RequestHeaders) -> IO [Text])
-> [Text] -> RequestHeaders -> IO [Text])
-> (([Text], RequestHeaders) -> IO [Text])
-> ([Text], Query)
-> RequestHeaders
-> IO ([Text], Query)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Text], RequestHeaders) -> IO [Text])
-> [Text] -> RequestHeaders -> IO [Text]
forall a b c. ((a, b) -> c) -> a -> b -> c
curry ((([Text], RequestHeaders) -> IO [Text])
-> ([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> (([Text], RequestHeaders) -> IO [Text])
-> ([Text], Query)
-> RequestHeaders
-> IO ([Text], Query)
forall a b. (a -> b) -> a -> b
$ IO [Text] -> IO [Text]
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Text] -> IO [Text])
-> (([Text], RequestHeaders) -> IO [Text])
-> ([Text], RequestHeaders)
-> IO [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> RequestHeaders -> IO [Text])
-> ([Text], RequestHeaders) -> IO [Text]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Text] -> RequestHeaders -> IO [Text]
convert
newReq <- (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> Request -> IO Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestRawM ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convertIO Request
req
app newReq sendResponse
{-# WARNING
rewrite
[ "This modifies the 'rawPathInfo' field of a 'Request'."
, " This is not recommended behaviour; it is however how"
, " this function has worked in the past."
, " Use 'rewriteWithQueries' instead"
]
#-}
rewritePure :: ([Text] -> H.RequestHeaders -> [Text]) -> Middleware
rewritePure :: ([Text] -> RequestHeaders -> [Text]) -> Middleware
rewritePure [Text] -> RequestHeaders -> [Text]
convert Application
app Request
req =
let convertPure :: ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
convertPure = ([Text] -> RequestHeaders -> Identity [Text])
-> ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
forall (m :: * -> *).
(Applicative m, Monad m) =>
([Text] -> RequestHeaders -> m [Text])
-> ([Text], Query) -> RequestHeaders -> m ([Text], Query)
pathsOnly (([Text] -> RequestHeaders -> Identity [Text])
-> ([Text], Query) -> RequestHeaders -> Identity ([Text], Query))
-> ((([Text], RequestHeaders) -> Identity [Text])
-> [Text] -> RequestHeaders -> Identity [Text])
-> (([Text], RequestHeaders) -> Identity [Text])
-> ([Text], Query)
-> RequestHeaders
-> Identity ([Text], Query)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Text], RequestHeaders) -> Identity [Text])
-> [Text] -> RequestHeaders -> Identity [Text]
forall a b c. ((a, b) -> c) -> a -> b -> c
curry ((([Text], RequestHeaders) -> Identity [Text])
-> ([Text], Query) -> RequestHeaders -> Identity ([Text], Query))
-> (([Text], RequestHeaders) -> Identity [Text])
-> ([Text], Query)
-> RequestHeaders
-> Identity ([Text], Query)
forall a b. (a -> b) -> a -> b
$ [Text] -> Identity [Text]
forall a. a -> Identity a
Identity ([Text] -> Identity [Text])
-> (([Text], RequestHeaders) -> [Text])
-> ([Text], RequestHeaders)
-> Identity [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> RequestHeaders -> [Text])
-> ([Text], RequestHeaders) -> [Text]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Text] -> RequestHeaders -> [Text]
convert
newReq :: Request
newReq = Identity Request -> Request
forall a. Identity a -> a
runIdentity (Identity Request -> Request) -> Identity Request -> Request
forall a b. (a -> b) -> a -> b
$ (([Text], Query) -> RequestHeaders -> Identity ([Text], Query))
-> Request -> Identity Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestRawM ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
convertPure Request
req
in Application
app Request
newReq
{-# WARNING
rewritePure
[ "This modifies the 'rawPathInfo' field of a 'Request'."
, " This is not recommended behaviour; it is however how"
, " this function has worked in the past."
, " Use 'rewritePureWithQueries' instead"
]
#-}
rewriteWithQueries
:: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
-> Middleware
rewriteWithQueries :: (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> Middleware
rewriteWithQueries ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convert Application
app Request
req Response -> IO ResponseReceived
sendResponse = do
newReq <- (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> Request -> IO Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestM ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convert Request
req
app newReq sendResponse
rewritePureWithQueries
:: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
-> Middleware
rewritePureWithQueries :: (([Text], Query) -> RequestHeaders -> ([Text], Query))
-> Middleware
rewritePureWithQueries ([Text], Query) -> RequestHeaders -> ([Text], Query)
convert Application
app Request
req = Application
app Middleware
forall a b. (a -> b) -> a -> b
$ (([Text], Query) -> RequestHeaders -> ([Text], Query))
-> Request -> Request
rewriteRequestPure ([Text], Query) -> RequestHeaders -> ([Text], Query)
convert Request
req
rewriteRoot :: Text -> Middleware
rewriteRoot :: Text -> Middleware
rewriteRoot Text
root = (([Text], Query) -> RequestHeaders -> ([Text], Query))
-> Middleware
rewritePureWithQueries ([Text], Query) -> RequestHeaders -> ([Text], Query)
forall {b} {p}. ([Text], b) -> p -> ([Text], b)
onlyRoot
where
onlyRoot :: ([Text], b) -> p -> ([Text], b)
onlyRoot ([], b
q) p
_ = ([Text
root], b
q)
onlyRoot ([Text], b)
paths p
_ = ([Text], b)
paths
rewriteRequest
:: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
-> Request
-> IO Request
rewriteRequest :: (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> Request -> IO Request
rewriteRequest ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convert Request
req =
let convertIO :: ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convertIO = ((([Text], Query), RequestHeaders) -> IO ([Text], Query))
-> ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (((([Text], Query), RequestHeaders) -> IO ([Text], Query))
-> ([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> ((([Text], Query), RequestHeaders) -> IO ([Text], Query))
-> ([Text], Query)
-> RequestHeaders
-> IO ([Text], Query)
forall a b. (a -> b) -> a -> b
$ IO ([Text], Query) -> IO ([Text], Query)
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ([Text], Query) -> IO ([Text], Query))
-> ((([Text], Query), RequestHeaders) -> IO ([Text], Query))
-> (([Text], Query), RequestHeaders)
-> IO ([Text], Query)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> (([Text], Query), RequestHeaders) -> IO ([Text], Query)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convert
in (([Text], Query) -> RequestHeaders -> IO ([Text], Query))
-> Request -> IO Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestRawM ([Text], Query) -> RequestHeaders -> IO ([Text], Query)
convertIO Request
req
rewriteRequestPure
:: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
-> Request
-> Request
rewriteRequestPure :: (([Text], Query) -> RequestHeaders -> ([Text], Query))
-> Request -> Request
rewriteRequestPure ([Text], Query) -> RequestHeaders -> ([Text], Query)
convert Request
req =
let convertPure :: ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
convertPure = ((([Text], Query), RequestHeaders) -> Identity ([Text], Query))
-> ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (((([Text], Query), RequestHeaders) -> Identity ([Text], Query))
-> ([Text], Query) -> RequestHeaders -> Identity ([Text], Query))
-> ((([Text], Query), RequestHeaders) -> Identity ([Text], Query))
-> ([Text], Query)
-> RequestHeaders
-> Identity ([Text], Query)
forall a b. (a -> b) -> a -> b
$ ([Text], Query) -> Identity ([Text], Query)
forall a. a -> Identity a
Identity (([Text], Query) -> Identity ([Text], Query))
-> ((([Text], Query), RequestHeaders) -> ([Text], Query))
-> (([Text], Query), RequestHeaders)
-> Identity ([Text], Query)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Text], Query) -> RequestHeaders -> ([Text], Query))
-> (([Text], Query), RequestHeaders) -> ([Text], Query)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ([Text], Query) -> RequestHeaders -> ([Text], Query)
convert
in Identity Request -> Request
forall a. Identity a -> a
runIdentity (Identity Request -> Request) -> Identity Request -> Request
forall a b. (a -> b) -> a -> b
$ (([Text], Query) -> RequestHeaders -> Identity ([Text], Query))
-> Request -> Identity Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestRawM ([Text], Query) -> RequestHeaders -> Identity ([Text], Query)
convertPure Request
req
rewriteRequestM
:: (Applicative m, Monad m)
=> (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
-> Request
-> m Request
rewriteRequestM :: forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestM ([Text], Query) -> RequestHeaders -> m ([Text], Query)
convert Request
req = do
(pInfo, qByteStrings) <-
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> [Text] -> Query -> RequestHeaders -> m ([Text], Query)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry ([Text], Query) -> RequestHeaders -> m ([Text], Query)
convert (Request -> [Text]
pathInfo Request
req) (Request -> Query
queryString Request
req) (Request -> RequestHeaders
requestHeaders Request
req)
pure req{pathInfo = pInfo, queryString = qByteStrings}
rewriteRequestRawM
:: (Applicative m, Monad m)
=> (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
-> Request
-> m Request
rewriteRequestRawM :: forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestRawM ([Text], Query) -> RequestHeaders -> m ([Text], Query)
convert Request
req = do
newReq <- (([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
forall (m :: * -> *).
(Applicative m, Monad m) =>
(([Text], Query) -> RequestHeaders -> m ([Text], Query))
-> Request -> m Request
rewriteRequestM ([Text], Query) -> RequestHeaders -> m ([Text], Query)
convert Request
req
let rawPInfo = Text -> ByteString
TE.encodeUtf8 (Text -> ByteString) -> (Request -> Text) -> Request -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
T.intercalate Text
"/" ([Text] -> Text) -> (Request -> [Text]) -> Request -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> [Text]
pathInfo (Request -> ByteString) -> Request -> ByteString
forall a b. (a -> b) -> a -> b
$ Request
newReq
pure newReq{rawPathInfo = rawPInfo}
{-# WARNING
rewriteRequestRawM
[ "This modifies the 'rawPathInfo' field of a 'Request'."
, " This is not recommended behaviour; it is however how"
, " this function has worked in the past."
, " Use 'rewriteRequestM' instead"
]
#-}
pathsOnly
:: (Applicative m, Monad m)
=> ([Text] -> H.RequestHeaders -> m [Text])
-> PathsAndQueries
-> H.RequestHeaders
-> m PathsAndQueries
pathsOnly :: forall (m :: * -> *).
(Applicative m, Monad m) =>
([Text] -> RequestHeaders -> m [Text])
-> ([Text], Query) -> RequestHeaders -> m ([Text], Query)
pathsOnly [Text] -> RequestHeaders -> m [Text]
convert ([Text], Query)
psAndQs RequestHeaders
headers = (,[]) ([Text] -> ([Text], Query)) -> m [Text] -> m ([Text], Query)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> RequestHeaders -> m [Text]
convert (([Text], Query) -> [Text]
forall a b. (a, b) -> a
fst ([Text], Query)
psAndQs) RequestHeaders
headers
{-# INLINE pathsOnly #-}