packages feed

wai-extra 0.4.0.3 → 0.4.1.1

raw patch · 3 files changed

+74/−12 lines, 3 filesdep +HUnitdep +test-frameworkdep +test-framework-hunitdep ~basedep ~blaze-builderdep ~blaze-builder-enumerator

Dependencies added: HUnit, test-framework, test-framework-hunit, wai-extra, wai-test, zlib

Dependency ranges changed: base, blaze-builder, blaze-builder-enumerator, bytestring, directory, enumerator, http-types, text, time, transformers, wai, zlib-bindings

Files

Network/Wai/Middleware/Debug.hs view
@@ -1,21 +1,56 @@ {-# LANGUAGE OverloadedStrings #-}-module Network.Wai.Middleware.Debug (debug) where+module Network.Wai.Middleware.Debug+    ( debug+    , debugHandle+    ) where -import Network.Wai (Middleware, requestMethod, requestHeaders, rawPathInfo, rawQueryString)+import Network.Wai (Request(..), Middleware)+import Network.Wai.Parse (parseRequestBody, lbsSink, fileName, Param, File) import Data.ByteString.Char8 (unpack)+import qualified Data.ByteString as S+import qualified Data.ByteString.Lazy as L import System.IO (hPutStrLn, stderr) import Control.Monad.IO.Class (liftIO)+import qualified Data.Text.Lazy as T+import Data.Enumerator (run_, ($$), enumList)+import Data.Enumerator.List (consume)  -- | Prints a message to 'stderr' for each request. debug :: Middleware-debug app req = do-    liftIO $ hPutStrLn stderr $ concat+debug = debugHandle $ hPutStrLn stderr . T.unpack++-- | Prints a message using the given callback function for each request.+-- This is not for serious production use- it is inefficient.+-- It immediately consumes a POST body and fills it back in and is otherwise inefficient+debugHandle :: (T.Text -> IO ()) -> Middleware+debugHandle cb app req = do+    body <- consume+    postParams <- if any (requestMethod req ==) ["GET", "HEAD"]+      then return []+      else do postParams <- liftIO $ allPostParams req body+              return $ collectPostParams postParams+    let getParams = map emptyGetParam $ queryString req++    liftIO $ cb $ T.pack $ concat         [ unpack $ requestMethod req         , " "         , unpack $ rawPathInfo req-        , unpack $ rawQueryString req         , "\n"         , (++) "Accept: " $ maybe "" unpack $ lookup "Accept" $ requestHeaders req-        , "\n"+        , paramsToStr  "GET " getParams+        , paramsToStr "POST " postParams         ]-    app req+    -- we just consumed the body- fill the enumerator back up so it is available again+    liftIO $ run_ $ enumList 1 body $$ app req+  where+    paramsToStr prefix params = if null params then "" else "\n" ++ prefix ++ (show params)++    allPostParams req body = run_ $ enumList 1 body $$ parseRequestBody lbsSink req++    emptyGetParam :: (S.ByteString, Maybe S.ByteString) -> (S.ByteString, S.ByteString)+    emptyGetParam (k, Just v) = (k,v)+    emptyGetParam (k, Nothing) = (k,"")++    collectPostParams :: ([Param], [File L.ByteString]) -> [Param]+    collectPostParams (postParams, files) = postParams +++      (map (\(k,v) -> (k, S.append "FILE: " (fileName v))) files)
Network/Wai/Parse.hs view
@@ -8,6 +8,8 @@     , Sink (..)     , lbsSink     , tempFileSink+    , Param+    , File     , FileInfo (..) #if TEST     , Bound (..)
wai-extra.cabal view
@@ -1,15 +1,15 @@ Name:                wai-extra-Version:             0.4.0.3+Version:             0.4.1.1 Synopsis:            Provides some basic WAI handlers and middleware. Description:         The goal here is to provide common features without many dependencies. License:             BSD3 License-file:        LICENSE Author:              Michael Snoyman Maintainer:          michael@snoyman.com-Homepage:            http://github.com/snoyberg/wai-extra+Homepage:            http://github.com/yesodweb/wai Category:            Web Build-Type:          Simple-Cabal-Version:       >=1.6+Cabal-Version:       >=1.8 Stability:           Stable  Library@@ -17,7 +17,7 @@                      bytestring >= 0.9 && < 0.10,                      wai >= 0.4 && < 0.5,                      old-locale >= 1.0 && < 1.1,-                     time >= 1.1.4 && < 1.3,+                     time >= 1.3   && < 1.4,                      network >= 2.2.1.5 && < 2.4,                      directory >= 1.0.1 && < 1.2,                      zlib-bindings >= 0.0 && < 0.1,@@ -42,6 +42,31 @@                      Network.Wai.Parse   ghc-options:       -Wall ++test-suite runtests+    hs-source-dirs: .+    main-is: runtests.hs+    type: exitcode-stdio-1.0++    build-depends:   base                      >= 4        && < 5+                   , wai-extra+                   , wai-test+                   , test-framework+                   , test-framework-hunit+                   , HUnit++                   , wai+                   , http-types+                   , transformers+                   , enumerator+                   , zlib+                   , text+                   , bytestring+                   , directory +                   , zlib-bindings+                   , blaze-builder-enumerator+                   , blaze-builder+ source-repository head   type:     git-  location: git://github.com/snoyberg/wai-extra.git+  location: git://github.com/yesodweb/wai.git