{-# LANGUAGE DeriveDataTypeable #-}

module Network.HPACK.Types (
    -- * Header
    HeaderName,
    HeaderValue,
    Header,
    HeaderList,
    TokenHeader,
    TokenHeaderList,

    -- * Misc
    Index,
    HIndex (..),

    -- * Encoding and decoding
    CompressionAlgo (..),
    EncodeStrategy (..),
    defaultEncodeStrategy,
    DecodeError (..),

    -- * Buffer
    Buffer,
    BufferSize,
    BufferOverrun (..),
) where

import Control.Exception as E
import Data.Typeable
import Network.ByteOrder (Buffer, BufferOverrun (..), BufferSize)

import Imports
import Network.HPACK.Token (Token)

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

-- | Header name.
type HeaderName = ByteString

-- | Header value.
type HeaderValue = ByteString

-- | Header.
type Header = (HeaderName, HeaderValue)

-- | Header list.
type HeaderList = [Header]

-- | TokenBased header.
type TokenHeader = (Token, HeaderValue)

-- | TokenBased header list.
type TokenHeaderList = [TokenHeader]

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

-- | Index for table.
type Index = Int

data HIndex = SIndex Int | DIndex Int deriving (HIndex -> HIndex -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HIndex -> HIndex -> Bool
$c/= :: HIndex -> HIndex -> Bool
== :: HIndex -> HIndex -> Bool
$c== :: HIndex -> HIndex -> Bool
Eq, Eq HIndex
HIndex -> HIndex -> Bool
HIndex -> HIndex -> Ordering
HIndex -> HIndex -> HIndex
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: HIndex -> HIndex -> HIndex
$cmin :: HIndex -> HIndex -> HIndex
max :: HIndex -> HIndex -> HIndex
$cmax :: HIndex -> HIndex -> HIndex
>= :: HIndex -> HIndex -> Bool
$c>= :: HIndex -> HIndex -> Bool
> :: HIndex -> HIndex -> Bool
$c> :: HIndex -> HIndex -> Bool
<= :: HIndex -> HIndex -> Bool
$c<= :: HIndex -> HIndex -> Bool
< :: HIndex -> HIndex -> Bool
$c< :: HIndex -> HIndex -> Bool
compare :: HIndex -> HIndex -> Ordering
$ccompare :: HIndex -> HIndex -> Ordering
Ord, Int -> HIndex -> ShowS
[HIndex] -> ShowS
HIndex -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HIndex] -> ShowS
$cshowList :: [HIndex] -> ShowS
show :: HIndex -> String
$cshow :: HIndex -> String
showsPrec :: Int -> HIndex -> ShowS
$cshowsPrec :: Int -> HIndex -> ShowS
Show)

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

-- | Compression algorithms for HPACK encoding.
data CompressionAlgo
    = -- | No compression
      Naive
    | -- | Using indices in the static table only
      Static
    | -- | Using indices
      Linear
    deriving (CompressionAlgo -> CompressionAlgo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressionAlgo -> CompressionAlgo -> Bool
$c/= :: CompressionAlgo -> CompressionAlgo -> Bool
== :: CompressionAlgo -> CompressionAlgo -> Bool
$c== :: CompressionAlgo -> CompressionAlgo -> Bool
Eq, Int -> CompressionAlgo -> ShowS
[CompressionAlgo] -> ShowS
CompressionAlgo -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressionAlgo] -> ShowS
$cshowList :: [CompressionAlgo] -> ShowS
show :: CompressionAlgo -> String
$cshow :: CompressionAlgo -> String
showsPrec :: Int -> CompressionAlgo -> ShowS
$cshowsPrec :: Int -> CompressionAlgo -> ShowS
Show)

-- | Strategy for HPACK encoding.
data EncodeStrategy = EncodeStrategy
    { EncodeStrategy -> CompressionAlgo
compressionAlgo :: CompressionAlgo
    -- ^ Which compression algorithm is used.
    , EncodeStrategy -> Bool
useHuffman :: Bool
    -- ^ Whether or not to use Huffman encoding for strings.
    }
    deriving (EncodeStrategy -> EncodeStrategy -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EncodeStrategy -> EncodeStrategy -> Bool
$c/= :: EncodeStrategy -> EncodeStrategy -> Bool
== :: EncodeStrategy -> EncodeStrategy -> Bool
$c== :: EncodeStrategy -> EncodeStrategy -> Bool
Eq, Int -> EncodeStrategy -> ShowS
[EncodeStrategy] -> ShowS
EncodeStrategy -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EncodeStrategy] -> ShowS
$cshowList :: [EncodeStrategy] -> ShowS
show :: EncodeStrategy -> String
$cshow :: EncodeStrategy -> String
showsPrec :: Int -> EncodeStrategy -> ShowS
$cshowsPrec :: Int -> EncodeStrategy -> ShowS
Show)

-- | Default 'EncodeStrategy'.
--
-- >>> defaultEncodeStrategy
-- EncodeStrategy {compressionAlgo = Linear, useHuffman = False}
defaultEncodeStrategy :: EncodeStrategy
defaultEncodeStrategy :: EncodeStrategy
defaultEncodeStrategy =
    EncodeStrategy
        { compressionAlgo :: CompressionAlgo
compressionAlgo = CompressionAlgo
Linear
        , useHuffman :: Bool
useHuffman = Bool
False
        }

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

-- | Errors for decoder.
data DecodeError
    = -- | Index is out of range
      IndexOverrun Index
    | -- | Eos appears in the middle of huffman string
      EosInTheMiddle
    | -- | Non-eos appears in the end of huffman string
      IllegalEos
    | -- | Eos of huffman string is more than 7 bits
      TooLongEos
    | -- | A peer set the dynamic table size less than 32
      TooSmallTableSize
    | -- | A peer tried to change the dynamic table size over the limit
      TooLargeTableSize
    | -- | Table size update at the non-beginning
      IllegalTableSizeUpdate
    | HeaderBlockTruncated
    | IllegalHeaderName
    | TooLargeHeader
    deriving (DecodeError -> DecodeError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DecodeError -> DecodeError -> Bool
$c/= :: DecodeError -> DecodeError -> Bool
== :: DecodeError -> DecodeError -> Bool
$c== :: DecodeError -> DecodeError -> Bool
Eq, Int -> DecodeError -> ShowS
[DecodeError] -> ShowS
DecodeError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DecodeError] -> ShowS
$cshowList :: [DecodeError] -> ShowS
show :: DecodeError -> String
$cshow :: DecodeError -> String
showsPrec :: Int -> DecodeError -> ShowS
$cshowsPrec :: Int -> DecodeError -> ShowS
Show, Typeable)

instance Exception DecodeError