{-# LANGUAGE BangPatterns, OverloadedStrings #-}
module Network.HPACK.Table.Entry (
Size
, Entry(..)
, Header
, HeaderName
, HeaderValue
, Index
, toEntry
, toEntryToken
, entrySize
, entryTokenHeader
, entryToken
, entryHeaderName
, entryHeaderValue
, dummyEntry
, maxNumbers
) where
import qualified Data.ByteString as BS
import Network.HPACK.Token
import Network.HPACK.Types
type Size = Int
data Entry = Entry Size Token HeaderValue deriving Show
headerSizeMagicNumber :: Size
headerSizeMagicNumber = 32
headerSize :: Header -> Size
headerSize (k,v) = BS.length k
+ BS.length v
+ headerSizeMagicNumber
headerSize' :: Token -> HeaderValue -> Size
headerSize' t v = BS.length (tokenFoldedKey t)
+ BS.length v
+ headerSizeMagicNumber
toEntry :: Header -> Entry
toEntry kv@(k,v) = Entry siz t v
where
!t = toToken k
!siz = headerSize kv
toEntryToken :: Token -> HeaderValue -> Entry
toEntryToken t v = Entry siz t v
where
!siz = headerSize' t v
entrySize :: Entry -> Size
entrySize (Entry siz _ _) = siz
entryTokenHeader :: Entry -> TokenHeader
entryTokenHeader (Entry _ t v) = (t, v)
entryToken :: Entry -> Token
entryToken (Entry _ t _) = t
entryHeaderName :: Entry -> HeaderName
entryHeaderName (Entry _ t _) = tokenFoldedKey t
entryHeaderValue :: Entry -> HeaderValue
entryHeaderValue (Entry _ _ v) = v
dummyEntry :: Entry
dummyEntry = Entry 0 tokenMax "dummyValue"
maxNumbers :: Size -> Int
maxNumbers siz = siz `div` headerSizeMagicNumber