attoparsec / examples / TestRFC2616.hs

{-# LANGUAGE BangPatterns #-}
import RFC2616
import Control.Monad (forM_)
import System.IO
import Control.Exception (bracket)
import System.Environment
import qualified Data.ByteString.Char8 as B
import Data.Attoparsec

refill h = B.hGet h (4*1024)

listy file h = do
  r <- parseWith (refill h) (many request) =<< refill h
  case r of
    Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
    Done _ reqs  -> print (length reqs)
  
incrementy file h = go 0 =<< refill h
 where
   go !n is = do
     r <- parseWith (refill h) request is
     case r of
       Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
       Done bs _req
           | B.null bs -> do
              s <- refill h
              if B.null s
                then print (n+1)
                else go (n+1) s
           | otherwise -> go (n+1) bs
  
main = do
  args <- getArgs
  forM_ args $ \arg ->
    bracket (openFile arg ReadMode) hClose $
      -- listy arg
      incrementy arg
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.