EdvardM
11/4/2011 - 8:42 PM

naive access log parser in haskell

naive access log parser in haskell

import Data.List
import Text.ParserCombinators.Parsec

w3cLog = endBy line eol
line = do
    [date, time] <- count 2 $ prg field
    [ip, meth, uri, status, bytes, elapsedms] <- count 6 $ prg field
    [referer, ua] <- count 2 $ prg quotedField
    cookie <- quotedField
    let datetime = intercalate " " [date, time]
    return (datetime, ip, meth, uri, int status, int bytes, int elapsedms, referer, ua, cookie)

int s = read s :: Int
csep = char '\t'
field = many (noneOf "\t\n")

prg f = do
  v <- f
  csep
  return v

quotedField =
    do char '"'
       content <- many1 quotedChar
       char '"' <?> "quote at end of field"
       return content

quotedChar = noneOf "\"" <|> try (string "\"\"" >> return '"')

eol = try (string "\n") <?> "end of line"

main =
    do c <- getContents
       case parse w3cLog "(stdin)" c of
            Left e -> do putStrLn "Error parsing input:"
                         print e
            Right r -> mapM_ print r