{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Network.HTTP2.Frame.Decode (
    -- * Decoding
    decodeFrame,
    decodeFrameHeader,
    checkFrameHeader,
    FrameDecodeError (..),

    -- * Decoding payload
    decodeFramePayload,
    FramePayloadDecoder,
    decodeDataFrame,
    decodeHeadersFrame,
    decodePriorityFrame,
    decodeRSTStreamFrame,
    decodeSettingsFrame,
    decodePushPromiseFrame,
    decodePingFrame,
    decodeGoAwayFrame,
    decodeWindowUpdateFrame,
    decodeContinuationFrame,
) where

import Control.Exception (Exception)
import Data.Array (Array, listArray, (!))
import qualified Data.ByteString as BS
import Foreign.Ptr (Ptr, plusPtr)
import qualified Network.ByteOrder as N
import System.IO.Unsafe (unsafeDupablePerformIO)

import Imports
import Network.HTTP2.Frame.Types

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

data FrameDecodeError = FrameDecodeError ErrorCode StreamId ShortByteString
    deriving (FrameDecodeError -> FrameDecodeError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FrameDecodeError -> FrameDecodeError -> Bool
$c/= :: FrameDecodeError -> FrameDecodeError -> Bool
== :: FrameDecodeError -> FrameDecodeError -> Bool
$c== :: FrameDecodeError -> FrameDecodeError -> Bool
Eq, Int -> FrameDecodeError -> ShowS
[FrameDecodeError] -> ShowS
FrameDecodeError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FrameDecodeError] -> ShowS
$cshowList :: [FrameDecodeError] -> ShowS
show :: FrameDecodeError -> String
$cshow :: FrameDecodeError -> String
showsPrec :: Int -> FrameDecodeError -> ShowS
$cshowsPrec :: Int -> FrameDecodeError -> ShowS
Show)

instance Exception FrameDecodeError

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

-- | Decoding an HTTP/2 frame to 'ByteString'.
-- The second argument must be include the entire of frame.
-- So, this function is not useful for real applications
-- but useful for testing.
decodeFrame
    :: ByteString
    -- ^ Input byte-stream
    -> Either FrameDecodeError Frame
    -- ^ Decoded frame
decodeFrame :: ByteString -> Either FrameDecodeError Frame
decodeFrame ByteString
bs =
    (FrameType, FrameHeader)
-> Either FrameDecodeError (FrameType, FrameHeader)
checkFrameHeader (ByteString -> (FrameType, FrameHeader)
decodeFrameHeader ByteString
bs0)
        forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(FrameType
typ, FrameHeader
header) ->
            FrameType -> FramePayloadDecoder
decodeFramePayload FrameType
typ FrameHeader
header ByteString
bs1
                forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \FramePayload
payload -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FrameHeader -> FramePayload -> Frame
Frame FrameHeader
header FramePayload
payload
  where
    (ByteString
bs0, ByteString
bs1) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
9 ByteString
bs

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

-- | Decoding an HTTP/2 frame header.
--   Must supply 9 bytes.
decodeFrameHeader :: ByteString -> (FrameType, FrameHeader)
decodeFrameHeader :: ByteString -> (FrameType, FrameHeader)
decodeFrameHeader (PS ForeignPtr Word8
fptr Int
off Int
_) = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> do
    let p :: Ptr Word8
p = Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
+. Int
off
    Int
len <- forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word8 -> Int -> IO Word32
N.peek24 Ptr Word8
p Int
0
    FrameType
typ <- Word8 -> FrameType
toFrameType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word8 -> Int -> IO Word8
N.peek8 Ptr Word8
p Int
3
    Word8
flg <- Ptr Word8 -> Int -> IO Word8
N.peek8 Ptr Word8
p Int
4
    Word32
w32 <- Ptr Word8 -> Int -> IO Word32
N.peek32 Ptr Word8
p Int
5
    let sid :: Int
sid = Word32 -> Int
streamIdentifier Word32
w32
    forall (m :: * -> *) a. Monad m => a -> m a
return (FrameType
typ, Int -> Word8 -> Int -> FrameHeader
FrameHeader Int
len Word8
flg Int
sid)

(+.) :: Ptr Word8 -> Int -> Ptr Word8
+. :: Ptr Word8 -> Int -> Ptr Word8
(+.) = forall a b. Ptr a -> Int -> Ptr b
plusPtr

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

-- | Checking a frame header and reporting an error if any.
--
-- >>> checkFrameHeader (FrameData,(FrameHeader 100 0 0))
-- Left (FrameDecodeError ProtocolError 0 "cannot used in control stream")
checkFrameHeader
    :: (FrameType, FrameHeader)
    -> Either FrameDecodeError (FrameType, FrameHeader)
checkFrameHeader :: (FrameType, FrameHeader)
-> Either FrameDecodeError (FrameType, FrameHeader)
checkFrameHeader typfrm :: (FrameType, FrameHeader)
typfrm@(FrameType
typ, FrameHeader{Int
Word8
streamId :: FrameHeader -> Int
flags :: FrameHeader -> Word8
payloadLength :: FrameHeader -> Int
streamId :: Int
flags :: Word8
payloadLength :: Int
..})
    | FrameType
typ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FrameType]
nonZeroFrameTypes Bool -> Bool -> Bool
&& Int -> Bool
isControl Int
streamId =
        forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
ProtocolError Int
streamId ShortByteString
"cannot used in control stream"
    | FrameType
typ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FrameType]
zeroFrameTypes Bool -> Bool -> Bool
&& Bool -> Bool
not (Int -> Bool
isControl Int
streamId) =
        forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
ProtocolError Int
streamId ShortByteString
"cannot used in non-zero stream"
    | Bool
otherwise = FrameType -> Either FrameDecodeError (FrameType, FrameHeader)
checkType FrameType
typ
  where
    checkType :: FrameType -> Either FrameDecodeError (FrameType, FrameHeader)
checkType FrameType
FrameHeaders
        | Word8 -> Bool
testPadded Word8
flags Bool -> Bool -> Bool
&& Int
payloadLength forall a. Ord a => a -> a -> Bool
< Int
1 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
FrameSizeError Int
streamId ShortByteString
"insufficient payload for Pad Length"
        | Word8 -> Bool
testPriority Word8
flags Bool -> Bool -> Bool
&& Int
payloadLength forall a. Ord a => a -> a -> Bool
< Int
5 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"insufficient payload for priority fields"
        | Word8 -> Bool
testPadded Word8
flags Bool -> Bool -> Bool
&& Word8 -> Bool
testPriority Word8
flags Bool -> Bool -> Bool
&& Int
payloadLength forall a. Ord a => a -> a -> Bool
< Int
6 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"insufficient payload for Pad Length and priority fields"
    checkType FrameType
FramePriority
        | Int
payloadLength forall a. Eq a => a -> a -> Bool
/= Int
5 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"payload length is not 5 in priority frame"
    checkType FrameType
FrameRSTStream
        | Int
payloadLength forall a. Eq a => a -> a -> Bool
/= Int
4 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"payload length is not 4 in rst stream frame"
    checkType FrameType
FrameSettings
        | Int
payloadLength forall a. Integral a => a -> a -> a
`mod` Int
6 forall a. Eq a => a -> a -> Bool
/= Int
0 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"payload length is not multiple of 6 in settings frame"
        | Word8 -> Bool
testAck Word8
flags Bool -> Bool -> Bool
&& Int
payloadLength forall a. Eq a => a -> a -> Bool
/= Int
0 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"payload length must be 0 if ack flag is set"
    checkType FrameType
FramePushPromise
        | Int -> Bool
isServerInitiated Int
streamId =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
ProtocolError
                    Int
streamId
                    ShortByteString
"push promise must be used with an odd stream identifier"
    checkType FrameType
FramePing
        | Int
payloadLength forall a. Eq a => a -> a -> Bool
/= Int
8 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
FrameSizeError Int
streamId ShortByteString
"payload length is 8 in ping frame"
    checkType FrameType
FrameGoAway
        | Int
payloadLength forall a. Ord a => a -> a -> Bool
< Int
8 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
FrameSizeError Int
streamId ShortByteString
"goaway body must be 8 bytes or larger"
    checkType FrameType
FrameWindowUpdate
        | Int
payloadLength forall a. Eq a => a -> a -> Bool
/= Int
4 =
            forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError
                    ErrorCode
FrameSizeError
                    Int
streamId
                    ShortByteString
"payload length is 4 in window update frame"
    checkType FrameType
_ = forall a b. b -> Either a b
Right (FrameType, FrameHeader)
typfrm

zeroFrameTypes :: [FrameType]
zeroFrameTypes :: [FrameType]
zeroFrameTypes =
    [ FrameType
FrameSettings
    , FrameType
FramePing
    , FrameType
FrameGoAway
    ]

nonZeroFrameTypes :: [FrameType]
nonZeroFrameTypes :: [FrameType]
nonZeroFrameTypes =
    [ FrameType
FrameData
    , FrameType
FrameHeaders
    , FrameType
FramePriority
    , FrameType
FrameRSTStream
    , FrameType
FramePushPromise
    , FrameType
FrameContinuation
    ]

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

-- | The type for frame payload decoder.
type FramePayloadDecoder =
    FrameHeader
    -> ByteString
    -> Either FrameDecodeError FramePayload

payloadDecoders :: Array FrameType FramePayloadDecoder
payloadDecoders :: Array FrameType FramePayloadDecoder
payloadDecoders =
    forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray
        (FrameType
minFrameType, FrameType
maxFrameType)
        [ FramePayloadDecoder
decodeDataFrame
        , FramePayloadDecoder
decodeHeadersFrame
        , FramePayloadDecoder
decodePriorityFrame
        , FramePayloadDecoder
decodeRSTStreamFrame
        , FramePayloadDecoder
decodeSettingsFrame
        , FramePayloadDecoder
decodePushPromiseFrame
        , FramePayloadDecoder
decodePingFrame
        , FramePayloadDecoder
decodeGoAwayFrame
        , FramePayloadDecoder
decodeWindowUpdateFrame
        , FramePayloadDecoder
decodeContinuationFrame
        ]

-- | Decoding an HTTP/2 frame payload.
--   This function is considered to return a frame payload decoder
--   according to a frame type.
decodeFramePayload :: FrameType -> FramePayloadDecoder
decodeFramePayload :: FrameType -> FramePayloadDecoder
decodeFramePayload FrameType
ftyp
    | FrameType
ftyp forall a. Ord a => a -> a -> Bool
> FrameType
maxFrameType = FramePayloadDecoder -> FramePayloadDecoder
checkFrameSize forall a b. (a -> b) -> a -> b
$ FrameType -> FramePayloadDecoder
decodeUnknownFrame FrameType
ftyp
decodeFramePayload FrameType
ftyp = FramePayloadDecoder -> FramePayloadDecoder
checkFrameSize FramePayloadDecoder
decoder
  where
    decoder :: FramePayloadDecoder
decoder = Array FrameType FramePayloadDecoder
payloadDecoders forall i e. Ix i => Array i e -> i -> e
! FrameType
ftyp

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

-- | Frame payload decoder for DATA frame.
decodeDataFrame :: FramePayloadDecoder
decodeDataFrame :: FramePayloadDecoder
decodeDataFrame FrameHeader
header ByteString
bs = FrameHeader
-> ByteString
-> (ByteString -> FramePayload)
-> Either FrameDecodeError FramePayload
decodeWithPadding FrameHeader
header ByteString
bs ByteString -> FramePayload
DataFrame

-- | Frame payload decoder for HEADERS frame.
decodeHeadersFrame :: FramePayloadDecoder
decodeHeadersFrame :: FramePayloadDecoder
decodeHeadersFrame FrameHeader
header ByteString
bs = FrameHeader
-> ByteString
-> (ByteString -> FramePayload)
-> Either FrameDecodeError FramePayload
decodeWithPadding FrameHeader
header ByteString
bs forall a b. (a -> b) -> a -> b
$ \ByteString
bs' ->
    if Bool
hasPriority
        then
            let (ByteString
bs0, ByteString
bs1) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
5 ByteString
bs'
                p :: Priority
p = ByteString -> Priority
priority ByteString
bs0
             in Maybe Priority -> ByteString -> FramePayload
HeadersFrame (forall a. a -> Maybe a
Just Priority
p) ByteString
bs1
        else Maybe Priority -> ByteString -> FramePayload
HeadersFrame forall a. Maybe a
Nothing ByteString
bs'
  where
    hasPriority :: Bool
hasPriority = Word8 -> Bool
testPriority forall a b. (a -> b) -> a -> b
$ FrameHeader -> Word8
flags FrameHeader
header

-- | Frame payload decoder for PRIORITY frame.
decodePriorityFrame :: FramePayloadDecoder
decodePriorityFrame :: FramePayloadDecoder
decodePriorityFrame FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Priority -> FramePayload
PriorityFrame forall a b. (a -> b) -> a -> b
$ ByteString -> Priority
priority ByteString
bs

-- | Frame payload decoder for RST_STREAM frame.
decodeRSTStreamFrame :: FramePayloadDecoder
decodeRSTStreamFrame :: FramePayloadDecoder
decodeRSTStreamFrame FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ErrorCode -> FramePayload
RSTStreamFrame forall a b. (a -> b) -> a -> b
$ Word32 -> ErrorCode
toErrorCode (ByteString -> Word32
N.word32 ByteString
bs)

-- | Frame payload decoder for SETTINGS frame.
decodeSettingsFrame :: FramePayloadDecoder
decodeSettingsFrame :: FramePayloadDecoder
decodeSettingsFrame FrameHeader{Int
Word8
streamId :: Int
flags :: Word8
payloadLength :: Int
streamId :: FrameHeader -> Int
flags :: FrameHeader -> Word8
payloadLength :: FrameHeader -> Int
..} (PS ForeignPtr Word8
fptr Int
off Int
_)
    | Int
num forall a. Ord a => a -> a -> Bool
> Int
10 =
        forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
EnhanceYourCalm Int
streamId ShortByteString
"Settings is too large"
    | Bool
otherwise = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ SettingsList -> FramePayload
SettingsFrame SettingsList
alist
  where
    num :: Int
num = Int
payloadLength forall a. Integral a => a -> a -> a
`div` Int
6
    alist :: SettingsList
alist = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> do
        let p :: Ptr Word8
p = Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
+. Int
off
        forall {t} {b} {c}.
(Eq t, Num t, Num b) =>
t -> Ptr Word8 -> ([(SettingsKey, b)] -> c) -> IO c
settings Int
num Ptr Word8
p forall a. a -> a
id
    settings :: t -> Ptr Word8 -> ([(SettingsKey, b)] -> c) -> IO c
settings t
0 Ptr Word8
_ [(SettingsKey, b)] -> c
builder = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [(SettingsKey, b)] -> c
builder []
    settings t
n Ptr Word8
p [(SettingsKey, b)] -> c
builder = do
        Word16
rawSetting <- Ptr Word8 -> Int -> IO Word16
N.peek16 Ptr Word8
p Int
0
        let k :: SettingsKey
k = Word16 -> SettingsKey
toSettingsKey Word16
rawSetting
            n' :: t
n' = t
n forall a. Num a => a -> a -> a
- t
1
        Word32
w32 <- Ptr Word8 -> Int -> IO Word32
N.peek32 Ptr Word8
p Int
2
        let v :: b
v = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w32
        t -> Ptr Word8 -> ([(SettingsKey, b)] -> c) -> IO c
settings t
n' (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
+. Int
6) ([(SettingsKey, b)] -> c
builder forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SettingsKey
k, b
v) forall a. a -> [a] -> [a]
:))

-- | Frame payload decoder for PUSH_PROMISE frame.
decodePushPromiseFrame :: FramePayloadDecoder
decodePushPromiseFrame :: FramePayloadDecoder
decodePushPromiseFrame FrameHeader
header ByteString
bs = FrameHeader
-> ByteString
-> (ByteString -> FramePayload)
-> Either FrameDecodeError FramePayload
decodeWithPadding FrameHeader
header ByteString
bs forall a b. (a -> b) -> a -> b
$ \ByteString
bs' ->
    let (ByteString
bs0, ByteString
bs1) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
4 ByteString
bs'
        sid :: Int
sid = Word32 -> Int
streamIdentifier (ByteString -> Word32
N.word32 ByteString
bs0)
     in Int -> ByteString -> FramePayload
PushPromiseFrame Int
sid ByteString
bs1

-- | Frame payload decoder for PING frame.
decodePingFrame :: FramePayloadDecoder
decodePingFrame :: FramePayloadDecoder
decodePingFrame FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ByteString -> FramePayload
PingFrame ByteString
bs

-- | Frame payload decoder for GOAWAY frame.
decodeGoAwayFrame :: FramePayloadDecoder
decodeGoAwayFrame :: FramePayloadDecoder
decodeGoAwayFrame FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Int -> ErrorCode -> ByteString -> FramePayload
GoAwayFrame Int
sid ErrorCode
ecid ByteString
bs2
  where
    (ByteString
bs0, ByteString
bs1') = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
4 ByteString
bs
    (ByteString
bs1, ByteString
bs2) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
4 ByteString
bs1'
    sid :: Int
sid = Word32 -> Int
streamIdentifier (ByteString -> Word32
N.word32 ByteString
bs0)
    ecid :: ErrorCode
ecid = Word32 -> ErrorCode
toErrorCode (ByteString -> Word32
N.word32 ByteString
bs1)

-- | Frame payload decoder for WINDOW_UPDATE frame.
decodeWindowUpdateFrame :: FramePayloadDecoder
decodeWindowUpdateFrame :: FramePayloadDecoder
decodeWindowUpdateFrame FrameHeader{Int
Word8
streamId :: Int
flags :: Word8
payloadLength :: Int
streamId :: FrameHeader -> Int
flags :: FrameHeader -> Word8
payloadLength :: FrameHeader -> Int
..} ByteString
bs
    | Int
wsi forall a. Eq a => a -> a -> Bool
== Int
0 =
        forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
ProtocolError Int
streamId ShortByteString
"window update must not be 0"
    | Bool
otherwise = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Int -> FramePayload
WindowUpdateFrame Int
wsi
  where
    wsi :: Int
wsi = forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Word32
N.word32 ByteString
bs forall a. Bits a => a -> Int -> a
`clearBit` Int
31)

-- | Frame payload decoder for CONTINUATION frame.
decodeContinuationFrame :: FramePayloadDecoder
decodeContinuationFrame :: FramePayloadDecoder
decodeContinuationFrame FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ByteString -> FramePayload
ContinuationFrame ByteString
bs

decodeUnknownFrame :: FrameType -> FramePayloadDecoder
decodeUnknownFrame :: FrameType -> FramePayloadDecoder
decodeUnknownFrame FrameType
typ FrameHeader
_ ByteString
bs = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ FrameType -> ByteString -> FramePayload
UnknownFrame FrameType
typ ByteString
bs

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

checkFrameSize :: FramePayloadDecoder -> FramePayloadDecoder
checkFrameSize :: FramePayloadDecoder -> FramePayloadDecoder
checkFrameSize FramePayloadDecoder
func header :: FrameHeader
header@FrameHeader{Int
Word8
streamId :: Int
flags :: Word8
payloadLength :: Int
streamId :: FrameHeader -> Int
flags :: FrameHeader -> Word8
payloadLength :: FrameHeader -> Int
..} ByteString
body
    | Int
payloadLength forall a. Ord a => a -> a -> Bool
> ByteString -> Int
BS.length ByteString
body =
        forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
FrameSizeError Int
streamId ShortByteString
"payload is too short"
    | Bool
otherwise = FramePayloadDecoder
func FrameHeader
header ByteString
body

-- | Helper function to pull off the padding if its there, and will
-- eat up the trailing padding automatically. Calls the decoder func
-- passed in with the length of the unpadded portion between the
-- padding octet and the actual padding
decodeWithPadding
    :: FrameHeader
    -> ByteString
    -> (ByteString -> FramePayload)
    -> Either FrameDecodeError FramePayload
decodeWithPadding :: FrameHeader
-> ByteString
-> (ByteString -> FramePayload)
-> Either FrameDecodeError FramePayload
decodeWithPadding FrameHeader{Int
Word8
streamId :: Int
flags :: Word8
payloadLength :: Int
streamId :: FrameHeader -> Int
flags :: FrameHeader -> Word8
payloadLength :: FrameHeader -> Int
..} ByteString
bs ByteString -> FramePayload
body
    | Bool
padded =
        let (Word8
w8, ByteString
rest) = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error String
"decodeWithPadding") forall a b. (a -> b) -> a -> b
$ ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs
            padlen :: Int
padlen = Word8 -> Int
intFromWord8 Word8
w8
            bodylen :: Int
bodylen = Int
payloadLength forall a. Num a => a -> a -> a
- Int
padlen forall a. Num a => a -> a -> a
- Int
1
         in if Int
bodylen forall a. Ord a => a -> a -> Bool
< Int
0
                then forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> FrameDecodeError
FrameDecodeError ErrorCode
ProtocolError Int
streamId ShortByteString
"padding is not enough"
                else forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> FramePayload
body forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take Int
bodylen ByteString
rest
    | Bool
otherwise = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ByteString -> FramePayload
body ByteString
bs
  where
    padded :: Bool
padded = Word8 -> Bool
testPadded Word8
flags

streamIdentifier :: Word32 -> StreamId
streamIdentifier :: Word32 -> Int
streamIdentifier Word32
w32 = Int -> Int
clearExclusive forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w32

priority :: ByteString -> Priority
priority :: ByteString -> Priority
priority (PS ForeignPtr Word8
fptr Int
off Int
_) = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> do
    let p :: Ptr Word8
p = Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
+. Int
off
    Word32
w32 <- Ptr Word8 -> Int -> IO Word32
N.peek32 Ptr Word8
p Int
0
    let streamdId :: Int
streamdId = Word32 -> Int
streamIdentifier Word32
w32
        exclusive :: Bool
exclusive = Int -> Bool
testExclusive (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w32) -- fixme
    Word8
w8 <- Ptr Word8 -> Int -> IO Word8
N.peek8 Ptr Word8
p Int
4
    let weight :: Int
weight = Word8 -> Int
intFromWord8 Word8
w8 forall a. Num a => a -> a -> a
+ Int
1
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Bool -> Int -> Int -> Priority
Priority Bool
exclusive Int
streamdId Int
weight

intFromWord8 :: Word8 -> Int
intFromWord8 :: Word8 -> Int
intFromWord8 = forall a b. (Integral a, Num b) => a -> b
fromIntegral