{-# LANGUAGE OverloadedStrings #-}
module Network.HTTP2.Arch.Types where
import qualified Control.Exception as E
import Data.ByteString.Builder (Builder)
import Data.IORef
import Data.IntMap.Strict (IntMap)
import Data.Typeable
import qualified Network.HTTP.Types as H
import UnliftIO.Concurrent
import UnliftIO.Exception (SomeException)
import UnliftIO.STM
import Imports
import Network.HPACK
import Network.HTTP2.Arch.File
import Network.HTTP2.Frame
type Scheme = ByteString
type Authority = ByteString
type Path = ByteString
type InpBody = IO ByteString
data OutBody = OutBodyNone
| OutBodyStreaming ((Builder -> IO ()) -> IO () -> IO ())
| OutBodyBuilder Builder
| OutBodyFile FileSpec
data InpObj = InpObj {
:: HeaderTable
, InpObj -> Maybe SettingsValue
inpObjBodySize :: Maybe Int
, InpObj -> InpBody
inpObjBody :: InpBody
, InpObj -> IORef (Maybe HeaderTable)
inpObjTrailers :: IORef (Maybe HeaderTable)
}
instance Show InpObj where
show :: InpObj -> String
show (InpObj (TokenHeaderList
thl,ValueTable
_) Maybe SettingsValue
_ InpBody
_body IORef (Maybe HeaderTable)
_tref) = forall a. Show a => a -> String
show TokenHeaderList
thl
data OutObj = OutObj {
:: [H.Header]
, OutObj -> OutBody
outObjBody :: OutBody
, OutObj -> TrailersMaker
outObjTrailers :: TrailersMaker
}
instance Show OutObj where
show :: OutObj -> String
show (OutObj [Header]
hdr OutBody
_ TrailersMaker
_) = forall a. Show a => a -> String
show [Header]
hdr
type TrailersMaker = Maybe ByteString -> IO NextTrailersMaker
defaultTrailersMaker :: TrailersMaker
defaultTrailersMaker :: TrailersMaker
defaultTrailersMaker Maybe HeaderValue
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Header] -> NextTrailersMaker
Trailers []
defaultTrailersMaker Maybe HeaderValue
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TrailersMaker -> NextTrailersMaker
NextTrailersMaker TrailersMaker
defaultTrailersMaker
data NextTrailersMaker = NextTrailersMaker TrailersMaker
| Trailers [H.Header]
data FileSpec = FileSpec FilePath FileOffset ByteCount deriving (FileSpec -> FileSpec -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileSpec -> FileSpec -> Bool
$c/= :: FileSpec -> FileSpec -> Bool
== :: FileSpec -> FileSpec -> Bool
$c== :: FileSpec -> FileSpec -> Bool
Eq, SettingsValue -> FileSpec -> ShowS
[FileSpec] -> ShowS
FileSpec -> String
forall a.
(SettingsValue -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileSpec] -> ShowS
$cshowList :: [FileSpec] -> ShowS
show :: FileSpec -> String
$cshow :: FileSpec -> String
showsPrec :: SettingsValue -> FileSpec -> ShowS
$cshowsPrec :: SettingsValue -> FileSpec -> ShowS
Show)
data OpenState =
JustOpened
| Continued [HeaderBlockFragment]
Int
Int
Bool
| NoBody HeaderTable
| HasBody HeaderTable
| Body (TQueue ByteString)
(Maybe Int)
(IORef Int)
(IORef (Maybe HeaderTable))
data ClosedCode = Finished
| Killed
| Reset ErrorCode
| ResetByMe SomeException
deriving SettingsValue -> ClosedCode -> ShowS
[ClosedCode] -> ShowS
ClosedCode -> String
forall a.
(SettingsValue -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ClosedCode] -> ShowS
$cshowList :: [ClosedCode] -> ShowS
show :: ClosedCode -> String
$cshow :: ClosedCode -> String
showsPrec :: SettingsValue -> ClosedCode -> ShowS
$cshowsPrec :: SettingsValue -> ClosedCode -> ShowS
Show
data StreamState =
Idle
| Open OpenState
| HalfClosedRemote
| HalfClosedLocal ClosedCode
| Closed ClosedCode
| Reserved
instance Show StreamState where
show :: StreamState -> String
show StreamState
Idle = String
"Idle"
show Open{} = String
"Open"
show StreamState
HalfClosedRemote = String
"HalfClosedRemote"
show (HalfClosedLocal ClosedCode
e) = String
"HalfClosedLocal: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ClosedCode
e
show (Closed ClosedCode
e) = String
"Closed: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ClosedCode
e
show StreamState
Reserved = String
"Reserved"
data Stream = Stream {
Stream -> SettingsValue
streamNumber :: StreamId
, Stream -> IORef StreamState
streamState :: IORef StreamState
, Stream -> TVar SettingsValue
streamWindow :: TVar WindowSize
, Stream -> MVar InpObj
streamInput :: MVar InpObj
}
instance Show Stream where
show :: Stream -> String
show Stream
s = forall a. Show a => a -> String
show (Stream -> SettingsValue
streamNumber Stream
s)
newtype StreamTable = StreamTable (IORef (IntMap Stream))
data Input a = Input a InpObj
data Output a = Output {
forall a. Output a -> a
outputStream :: a
, forall a. Output a -> OutObj
outputObject :: OutObj
, forall a. Output a -> OutputType
outputType :: OutputType
, forall a. Output a -> Maybe (TBQueue StreamingChunk)
outputStrmQ :: Maybe (TBQueue StreamingChunk)
, forall a. Output a -> IO ()
outputSentinel :: IO ()
}
data OutputType = OObj
| OWait (IO ())
| OPush TokenHeaderList StreamId
| ONext DynaNext TrailersMaker
type DynaNext = Buffer -> BufferSize -> WindowSize -> IO Next
type BytesFilled = Int
data Next = Next BytesFilled
Bool
(Maybe DynaNext)
data Control = CFinish HTTP2Error
| CFrames (Maybe SettingsList) [ByteString]
data StreamingChunk = StreamingFinished
| StreamingFlush
| StreamingBuilder Builder
type ReasonPhrase = ShortByteString
data HTTP2Error =
ConnectionIsClosed
| ConnectionErrorIsReceived ErrorCode StreamId ReasonPhrase
| ConnectionErrorIsSent ErrorCode StreamId ReasonPhrase
| StreamErrorIsReceived ErrorCode StreamId
| StreamErrorIsSent ErrorCode StreamId ReasonPhrase
| BadThingHappen E.SomeException
deriving (SettingsValue -> HTTP2Error -> ShowS
[HTTP2Error] -> ShowS
HTTP2Error -> String
forall a.
(SettingsValue -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HTTP2Error] -> ShowS
$cshowList :: [HTTP2Error] -> ShowS
show :: HTTP2Error -> String
$cshow :: HTTP2Error -> String
showsPrec :: SettingsValue -> HTTP2Error -> ShowS
$cshowsPrec :: SettingsValue -> HTTP2Error -> ShowS
Show, Typeable)
instance E.Exception HTTP2Error
checkSettingsList :: SettingsList -> Maybe HTTP2Error
checkSettingsList :: SettingsList -> Maybe HTTP2Error
checkSettingsList SettingsList
settings = case forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (SettingsKey, SettingsValue) -> Maybe HTTP2Error
checkSettingsValue SettingsList
settings of
[] -> forall a. Maybe a
Nothing
(HTTP2Error
x:[HTTP2Error]
_) -> forall a. a -> Maybe a
Just HTTP2Error
x
checkSettingsValue :: (SettingsKey,SettingsValue) -> Maybe HTTP2Error
checkSettingsValue :: (SettingsKey, SettingsValue) -> Maybe HTTP2Error
checkSettingsValue (SettingsKey
SettingsEnablePush,SettingsValue
v)
| SettingsValue
v forall a. Eq a => a -> a -> Bool
/= SettingsValue
0 Bool -> Bool -> Bool
&& SettingsValue
v forall a. Eq a => a -> a -> Bool
/= SettingsValue
1 = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ErrorCode -> SettingsValue -> ReasonPhrase -> HTTP2Error
ConnectionErrorIsSent ErrorCode
ProtocolError SettingsValue
0 ReasonPhrase
"enable push must be 0 or 1"
checkSettingsValue (SettingsKey
SettingsInitialWindowSize,SettingsValue
v)
| SettingsValue
v forall a. Ord a => a -> a -> Bool
> SettingsValue
maxWindowSize = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ErrorCode -> SettingsValue -> ReasonPhrase -> HTTP2Error
ConnectionErrorIsSent ErrorCode
FlowControlError SettingsValue
0 ReasonPhrase
"Window size must be less than or equal to 65535"
checkSettingsValue (SettingsKey
SettingsMaxFrameSize,SettingsValue
v)
| SettingsValue
v forall a. Ord a => a -> a -> Bool
< SettingsValue
defaultPayloadLength Bool -> Bool -> Bool
|| SettingsValue
v forall a. Ord a => a -> a -> Bool
> SettingsValue
maxPayloadLength = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ErrorCode -> SettingsValue -> ReasonPhrase -> HTTP2Error
ConnectionErrorIsSent ErrorCode
ProtocolError SettingsValue
0 ReasonPhrase
"Max frame size must be in between 16384 and 16777215"
checkSettingsValue (SettingsKey, SettingsValue)
_ = forall a. Maybe a
Nothing