File: Atto_RFC2616.hs

package info (click to toggle)
haskell-attoparsec 0.14.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 880 kB
  • sloc: haskell: 4,749; ansic: 170; makefile: 22
file content (45 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE BangPatterns #-}

module Main (main) where

import Control.Applicative
import Control.Exception (bracket)
import Control.Monad (forM_)
import Data.Attoparsec.ByteString
import RFC2616
import System.Environment
import System.IO
import qualified Data.ByteString.Char8 as B

refill :: Handle -> IO B.ByteString
refill h = B.hGet h (80*1024)

listy :: FilePath -> Handle -> IO ()
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 :: FilePath -> Handle -> IO ()
incrementy file h = go (0::Int) =<< 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 :: IO ()
main = do
  args <- getArgs
  forM_ args $ \arg ->
    bracket (openFile arg ReadMode) hClose $
      -- listy arg
      incrementy arg