@@ -1,11 +1,8 @@
11{-# LANGUAGE OverloadedStrings #-}
2−
32-----------------------------------------------------------------
4−-- |
5−-- Module : Network.Wai.Middleware.MethodOverridePost
6−--
7−-- Changing of request-method via first post-parameter "_method".
3+-- | Module : Network.Wai.Middleware.MethodOverridePost
84--
5+-- Changes the request-method via first post-parameter _method.
96-----------------------------------------------------------------
107module Network.Wai.Middleware.MethodOverridePost
118 ( methodOverridePost
@@ -18,17 +15,25 @@ import Data.Conduit.Lazy (lazyConsume)
1815import Control.Monad.Trans.Resource (ResourceT)
1916import Data.Conduit.List (sourceList)
2017
21−-- | Override the request-method by setting the first POST key/value
22−-- to "_method" / <request-method>.
18+-- | Allows overriding of the HTTP request method via the _method post string parameter.
19+--
20+-- * Looks for the Content-Type requestHeader.
21+--
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
25+-- parameter.
26+--
27+-- * This middlware only applies when the initial request method is POST.
2328--
24−-- * Only accepts "application/x-www-form-urlencoded" POSTs.
2529methodOverridePost :: Middleware
26−methodOverridePost app req = case lookup "Content-Type" (requestHeaders req) of
27− Just "application/x-www-form-urlencoded" -> setPost req >>= app
28− _ -> app req
30+methodOverridePost app req = case (requestMethod req, lookup "Content-Type" (requestHeaders req)) of
31+ ("POST", Just "application/x-www-form-urlencoded") -> setPost req >>= app
32+ _ -> app req
2933
3034setPost :: Request -> ResourceT IO Request
3135setPost req = do
3236 body <- lazyConsume (requestBody req)
33− case parseQuery (mconcat body) of (("_method", Just newmethod):_) -> return $ req {requestBody = sourceList body, requestMethod = newmethod}
34− _ -> return $ req {requestBody = sourceList body}
37+ case parseQuery (mconcat body) of
38+ (("_method", Just newmethod):_) -> return $ req {requestBody = sourceList body, requestMethod = newmethod}
39+ _ -> return $ req {requestBody = sourceList body}