{-# LANGUAGE OverloadedStrings #-}

module Network.HPACK.Table.Entry (
  -- * Type
    Size
  , Entry(..)
  , Header      -- re-exporting
  , HeaderName  -- re-exporting
  , HeaderValue -- re-exporting
  , Index       -- re-exporting
  -- * Header and Entry
  , toEntry
  , toEntryToken
  -- * Getters
  , entrySize
  , entryTokenHeader
  , entryToken
  , entryHeaderName
  , entryHeaderValue
  -- * For initialization
  , dummyEntry
  , maxNumbers
  ) where

import qualified Data.ByteString as BS
import Network.HPACK.Token
import Network.HPACK.Types

----------------------------------------------------------------

-- | Size in bytes.
type Size = Int

-- | Type for table entry. Size includes the 32 bytes magic number.
data Entry = Entry Size Token HeaderValue deriving Int -> Entry -> ShowS
[Entry] -> ShowS
Entry -> String
(Int -> Entry -> ShowS)
-> (Entry -> String) -> ([Entry] -> ShowS) -> Show Entry
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Entry] -> ShowS
$cshowList :: [Entry] -> ShowS
show :: Entry -> String
$cshow :: Entry -> String
showsPrec :: Int -> Entry -> ShowS
$cshowsPrec :: Int -> Entry -> ShowS
Show

----------------------------------------------------------------

headerSizeMagicNumber :: Size
headerSizeMagicNumber :: Int
headerSizeMagicNumber = Int
32

headerSize :: Header -> Size
headerSize :: Header -> Int
headerSize (HeaderName
k,HeaderName
v) = HeaderName -> Int
BS.length HeaderName
k
                 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HeaderName -> Int
BS.length HeaderName
v
                 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
headerSizeMagicNumber

headerSize' :: Token -> HeaderValue -> Size
headerSize' :: Token -> HeaderName -> Int
headerSize' Token
t HeaderName
v = HeaderName -> Int
BS.length (Token -> HeaderName
tokenFoldedKey Token
t)
                Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HeaderName -> Int
BS.length HeaderName
v
                Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
headerSizeMagicNumber

----------------------------------------------------------------

-- | From 'Header' to 'Entry'.
toEntry :: Header -> Entry
toEntry :: Header -> Entry
toEntry kv :: Header
kv@(HeaderName
k,HeaderName
v) = Int -> Token -> HeaderName -> Entry
Entry Int
siz Token
t HeaderName
v
  where
    t :: Token
t = HeaderName -> Token
toToken HeaderName
k
    siz :: Int
siz = Header -> Int
headerSize Header
kv

toEntryToken :: Token -> HeaderValue -> Entry
toEntryToken :: Token -> HeaderName -> Entry
toEntryToken Token
t HeaderName
v = Int -> Token -> HeaderName -> Entry
Entry Int
siz Token
t HeaderName
v
  where
    siz :: Int
siz = Token -> HeaderName -> Int
headerSize' Token
t HeaderName
v

----------------------------------------------------------------

-- | Getting the size of 'Entry'.
entrySize :: Entry -> Size
entrySize :: Entry -> Int
entrySize (Entry Int
siz Token
_ HeaderName
_) = Int
siz

-- | Getting 'TokenHeader'.
entryTokenHeader :: Entry -> TokenHeader
entryTokenHeader :: Entry -> TokenHeader
entryTokenHeader (Entry Int
_ Token
t HeaderName
v) = (Token
t, HeaderName
v)

-- | Getting 'Token'.
entryToken :: Entry -> Token
entryToken :: Entry -> Token
entryToken (Entry Int
_ Token
t HeaderName
_) = Token
t

-- | Getting 'HeaderName'.
entryHeaderName :: Entry -> HeaderName
entryHeaderName :: Entry -> HeaderName
entryHeaderName (Entry Int
_ Token
t HeaderName
_) = Token -> HeaderName
tokenFoldedKey Token
t

-- | Getting 'HeaderValue'.
entryHeaderValue :: Entry -> HeaderValue
entryHeaderValue :: Entry -> HeaderName
entryHeaderValue (Entry Int
_ Token
_ HeaderName
v) = HeaderName
v

----------------------------------------------------------------

-- | Dummy 'Entry' to initialize a dynamic table.
dummyEntry :: Entry
dummyEntry :: Entry
dummyEntry = Int -> Token -> HeaderName -> Entry
Entry Int
0 Token
tokenMax HeaderName
"dummyValue"

-- | How many entries can be stored in a dynamic table?
maxNumbers :: Size -> Int
maxNumbers :: Int -> Int
maxNumbers Int
siz = Int
siz Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
headerSizeMagicNumber