PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/TestRFC2616.hs

http://github.com/bos/attoparsec
Haskell | 37 lines | 31 code | 4 blank | 2 comment | 2 complexity | 92feb2a2c350a63d3fb51103e379bbdb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. {-# LANGUAGE BangPatterns #-}
  2. import RFC2616
  3. import Control.Monad (forM_)
  4. import System.IO
  5. import Control.Exception (bracket)
  6. import System.Environment
  7. import qualified Data.ByteString.Char8 as B
  8. import Data.Attoparsec
  9. refill h = B.hGet h (4*1024)
  10. listy file h = do
  11. r <- parseWith (refill h) (many request) =<< refill h
  12. case r of
  13. Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
  14. Done _ reqs -> print (length reqs)
  15. incrementy file h = go 0 =<< refill h
  16. where
  17. go !n is = do
  18. r <- parseWith (refill h) request is
  19. case r of
  20. Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
  21. Done bs _req
  22. | B.null bs -> do
  23. s <- refill h
  24. if B.null s
  25. then print (n+1)
  26. else go (n+1) s
  27. | otherwise -> go (n+1) bs
  28. main = do
  29. args <- getArgs
  30. forM_ args $ \arg ->
  31. bracket (openFile arg ReadMode) hClose $
  32. -- listy arg
  33. incrementy arg