Skip to content
Newer
Older
100644 38 lines (33 sloc) 964 Bytes
b998bcb @bos Add a driver for the RFC2616 example.
authored
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
25fb8a4 @bos Example fiddle faddle.
authored
10 refill h = B.hGet h (4*1024)
b998bcb @bos Add a driver for the RFC2616 example.
authored
11
12 listy file h = do
13 r <- parseWith (refill h) (many request) =<< refill h
14 case r of
15 Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
16 Done _ reqs -> print (length reqs)
17
18 incrementy file h = go 0 =<< refill h
19 where
20 go !n is = do
21 r <- parseWith (refill h) request is
22 case r of
23 Fail _ _ msg -> hPutStrLn stderr $ file ++ ": " ++ msg
24 Done bs _req
25 | B.null bs -> do
26 s <- refill h
27 if B.null s
28 then print (n+1)
29 else go (n+1) s
30 | otherwise -> go (n+1) bs
31
32 main = do
33 args <- getArgs
34 forM_ args $ \arg ->
35 bracket (openFile arg ReadMode) hClose $
36 -- listy arg
37 incrementy arg
Something went wrong with that request. Please try again.