1
{-# LANGUAGE OverloadedStrings #-}
2
-----------------------------------------------------------------
3
-- | Module : Network.Wai.Middleware.MethodOverridePost
5
-- Changes the request-method via first post-parameter _method.
6
-----------------------------------------------------------------
7
module Network.Wai.Middleware.MethodOverridePost
12
import Network.HTTP.Types (parseQuery)
13
import Data.Monoid (mconcat)
14
import Data.Conduit.Lazy (lazyConsume)
15
import Control.Monad.Trans.Resource (ResourceT)
16
import Data.Conduit.List (sourceList)
18
-- | Allows overriding of the HTTP request method via the _method post string parameter.
20
-- * Looks for the Content-Type requestHeader.
22
-- * If the header is set to application/x-www-form-urlencoded
23
-- and the first POST parameter is _method
24
-- then it changes the request-method to the value of that
27
-- * This middlware only applies when the initial request method is POST.
29
methodOverridePost :: Middleware
30
methodOverridePost app req = case (requestMethod req, lookup "Content-Type" (requestHeaders req)) of
31
("POST", Just "application/x-www-form-urlencoded") -> setPost req >>= app
34
setPost :: Request -> ResourceT IO Request
36
body <- lazyConsume (requestBody req)
37
case parseQuery (mconcat body) of
38
(("_method", Just newmethod):_) -> return $ req {requestBody = sourceList body, requestMethod = newmethod}
39
_ -> return $ req {requestBody = sourceList body}