module Network.HTTP.Lucu.Chunk
    ( chunkHeaderP  -- Num a => Parser a
    , chunkFooterP  -- Parser ()
    , chunkTrailerP -- Parser Headers
    )
    where

import           Network.HTTP.Lucu.Headers
import           Network.HTTP.Lucu.Parser
import           Network.HTTP.Lucu.Parser.Http
import           Numeric


chunkHeaderP :: Num a => Parser a
chunkHeaderP = do hexLen <- many1 hexDigit
                  _      <- extension
                  _      <- crlf

                  let [(len, _)] = readHex hexLen
                  return len
    where
      extension :: Parser ()
      extension = many ( char ';' >>
                         token    >>
                         char '=' >>
                         ( token <|> quotedStr )
                       )
                  >>
                  return ()
{-# SPECIALIZE chunkHeaderP :: Parser Int #-}


chunkFooterP :: Parser ()
chunkFooterP = crlf >> return ()


chunkTrailerP :: Parser Headers
chunkTrailerP = headersP