{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
module Network.HTTP2.H2.Types where
import Control.Concurrent
import Control.Concurrent.STM
import qualified Control.Exception as E
import qualified System.TimeManager as TM
import Data.IORef
import Foreign.Ptr (nullPtr)
import Network.Control
import Network.HTTP.Semantics.Client
import Network.HTTP.Semantics.IO
import Network.Socket hiding (Stream)
import System.IO.Error (catchIOError)
import System.IO.Unsafe
import qualified System.TimeManager as T
import Imports
import Network.HPACK
import Network.HTTP2.Frame
data OpenState
= JustOpened
| NoBody TokenHeaderTable
| HasBody TokenHeaderTable
| Body
(TQueue (Either E.SomeException (ByteString, Bool)))
(Maybe Int)
(IORef Int)
(IORef (Maybe TokenHeaderTable))
data =
{ PartialHeaderBlock -> [ByteString]
phbFragments :: [HeaderBlockFragment]
, PartialHeaderBlock -> Int
phbTotalSize :: Int
, PartialHeaderBlock -> Int
phbNumFrames :: Int
}
data ClosedCode
= Finished
| Killed
| Reset ErrorCode
| ResetByMe E.SomeException
deriving (Int -> ClosedCode -> ShowS
[ClosedCode] -> ShowS
ClosedCode -> String
(Int -> ClosedCode -> ShowS)
-> (ClosedCode -> String)
-> ([ClosedCode] -> ShowS)
-> Show ClosedCode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClosedCode -> ShowS
showsPrec :: Int -> ClosedCode -> ShowS
$cshow :: ClosedCode -> String
show :: ClosedCode -> String
$cshowList :: [ClosedCode] -> ShowS
showList :: [ClosedCode] -> ShowS
Show)
data CancelledStream = CancelledStream
deriving (Int -> CancelledStream -> ShowS
[CancelledStream] -> ShowS
CancelledStream -> String
(Int -> CancelledStream -> ShowS)
-> (CancelledStream -> String)
-> ([CancelledStream] -> ShowS)
-> Show CancelledStream
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CancelledStream -> ShowS
showsPrec :: Int -> CancelledStream -> ShowS
$cshow :: CancelledStream -> String
show :: CancelledStream -> String
$cshowList :: [CancelledStream] -> ShowS
showList :: [CancelledStream] -> ShowS
Show, Show CancelledStream
Typeable CancelledStream
(Typeable CancelledStream, Show CancelledStream) =>
(CancelledStream -> SomeException)
-> (SomeException -> Maybe CancelledStream)
-> (CancelledStream -> String)
-> (CancelledStream -> Bool)
-> Exception CancelledStream
SomeException -> Maybe CancelledStream
CancelledStream -> Bool
CancelledStream -> String
CancelledStream -> SomeException
forall e.
(Typeable e, Show e) =>
(e -> SomeException)
-> (SomeException -> Maybe e)
-> (e -> String)
-> (e -> Bool)
-> Exception e
$ctoException :: CancelledStream -> SomeException
toException :: CancelledStream -> SomeException
$cfromException :: SomeException -> Maybe CancelledStream
fromException :: SomeException -> Maybe CancelledStream
$cdisplayException :: CancelledStream -> String
displayException :: CancelledStream -> String
$cbacktraceDesired :: CancelledStream -> Bool
backtraceDesired :: CancelledStream -> Bool
E.Exception)
closedCodeToError :: StreamId -> ClosedCode -> HTTP2Error
closedCodeToError :: Int -> ClosedCode -> HTTP2Error
closedCodeToError Int
sid ClosedCode
cc =
case ClosedCode
cc of
ClosedCode
Finished -> HTTP2Error
ConnectionIsClosed
ClosedCode
Killed -> HTTP2Error
ConnectionIsTimeout
Reset ErrorCode
err -> ErrorCode -> Int -> HTTP2Error
StreamResetIsReceived ErrorCode
err Int
sid
ResetByMe SomeException
err -> SomeException -> HTTP2Error
BadThingHappen SomeException
err
data StreamState
= Idle
| Open (Maybe ClosedCode) OpenState
| HalfClosedRemote
| Closed ClosedCode
| Reserved
instance Show StreamState where
show :: StreamState -> String
show StreamState
Idle = String
"Idle"
show (Open Maybe ClosedCode
Nothing OpenState
_) = String
"Open"
show (Open (Just ClosedCode
e) OpenState
_) = String
"HalfClosedLocal: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ClosedCode -> String
forall a. Show a => a -> String
show ClosedCode
e
show StreamState
HalfClosedRemote = String
"HalfClosedRemote"
show (Closed ClosedCode
e) = String
"Closed: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ClosedCode -> String
forall a. Show a => a -> String
show ClosedCode
e
show StreamState
Reserved = String
"Reserved"
type RxQ = TQueue (Either E.SomeException (ByteString, Bool))
data Stream = Stream
{ Stream -> Int
streamNumber :: StreamId
, Stream -> TVar StreamState
streamState :: TVar StreamState
, Stream -> MVar (Either SomeException InpObj)
streamInput :: MVar (Either E.SomeException InpObj)
, Stream -> TVar TxFlow
streamTxFlow :: TVar TxFlow
, Stream -> IORef RxFlow
streamRxFlow :: IORef RxFlow
, Stream -> IORef (Maybe RxQ)
streamRxQ :: IORef (Maybe RxQ)
, Stream -> IORef (Maybe ByteString)
streamRequestMethod :: IORef (Maybe ByteString)
, Stream -> TVar Bool
streamDiscardBody :: TVar Bool
}
instance Show Stream where
show :: Stream -> String
show Stream{Int
TVar Bool
TVar TxFlow
TVar StreamState
IORef (Maybe ByteString)
IORef (Maybe RxQ)
IORef RxFlow
MVar (Either SomeException InpObj)
streamNumber :: Stream -> Int
streamState :: Stream -> TVar StreamState
streamInput :: Stream -> MVar (Either SomeException InpObj)
streamTxFlow :: Stream -> TVar TxFlow
streamRxFlow :: Stream -> IORef RxFlow
streamRxQ :: Stream -> IORef (Maybe RxQ)
streamRequestMethod :: Stream -> IORef (Maybe ByteString)
streamDiscardBody :: Stream -> TVar Bool
streamNumber :: Int
streamState :: TVar StreamState
streamInput :: MVar (Either SomeException InpObj)
streamTxFlow :: TVar TxFlow
streamRxFlow :: IORef RxFlow
streamRxQ :: IORef (Maybe RxQ)
streamRequestMethod :: IORef (Maybe ByteString)
streamDiscardBody :: TVar Bool
..} =
String
"Stream{id="
String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
streamNumber
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
",state="
String -> ShowS
forall a. [a] -> [a] -> [a]
++ StreamState -> String
forall a. Show a => a -> String
show (IO StreamState -> StreamState
forall a. IO a -> a
unsafePerformIO (TVar StreamState -> IO StreamState
forall a. TVar a -> IO a
readTVarIO TVar StreamState
streamState))
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"}"
data Output = Output
{ Output -> Stream
outputStream :: Stream
, Output -> OutputType
outputType :: OutputType
, Output -> Maybe Output -> IO ()
outputSync :: Maybe Output -> IO ()
}
data OutputType
= [Header] (Maybe DynaNext) TrailersMaker
| OPush TokenHeaderList StreamId
| ONext DynaNext TrailersMaker
| OInformational [Header]
| OReset (Maybe E.SomeException)
data Sync = Done | Cont Output
data Control = CFrames (Maybe SettingsList) [ByteString]
data ControlQ = ControlQ (TQueue Control) (TVar Int)
type ReasonPhrase = ShortByteString
data HTTP2Error
= ConnectionIsClosed
| ConnectionIsTimeout
| ConnectionIsReset
| ConnectionErrorIsReceived ErrorCode StreamId ReasonPhrase
| ConnectionErrorIsSent ErrorCode StreamId ReasonPhrase
| StreamErrorIsReceived ErrorCode StreamId
| StreamResetIsReceived ErrorCode StreamId
| StreamErrorIsSent ErrorCode StreamId ReasonPhrase
| BadThingHappen E.SomeException
deriving (Int -> HTTP2Error -> ShowS
[HTTP2Error] -> ShowS
HTTP2Error -> String
(Int -> HTTP2Error -> ShowS)
-> (HTTP2Error -> String)
-> ([HTTP2Error] -> ShowS)
-> Show HTTP2Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HTTP2Error -> ShowS
showsPrec :: Int -> HTTP2Error -> ShowS
$cshow :: HTTP2Error -> String
show :: HTTP2Error -> String
$cshowList :: [HTTP2Error] -> ShowS
showList :: [HTTP2Error] -> ShowS
Show)
instance E.Exception HTTP2Error
checkSettingsList :: SettingsList -> Maybe HTTP2Error
checkSettingsList :: SettingsList -> Maybe HTTP2Error
checkSettingsList SettingsList
settings = case ((SettingsKey, Int) -> Maybe HTTP2Error)
-> SettingsList -> [HTTP2Error]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (SettingsKey, Int) -> Maybe HTTP2Error
checkSettingsValue SettingsList
settings of
[] -> Maybe HTTP2Error
forall a. Maybe a
Nothing
(HTTP2Error
x : [HTTP2Error]
_) -> HTTP2Error -> Maybe HTTP2Error
forall a. a -> Maybe a
Just HTTP2Error
x
checkSettingsValue :: (SettingsKey, SettingsValue) -> Maybe HTTP2Error
checkSettingsValue :: (SettingsKey, Int) -> Maybe HTTP2Error
checkSettingsValue (SettingsKey
SettingsEnablePush, Int
v)
| Int
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& Int
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1 =
HTTP2Error -> Maybe HTTP2Error
forall a. a -> Maybe a
Just (HTTP2Error -> Maybe HTTP2Error) -> HTTP2Error -> Maybe HTTP2Error
forall a b. (a -> b) -> a -> b
$ ErrorCode -> Int -> ShortByteString -> HTTP2Error
ConnectionErrorIsSent ErrorCode
ProtocolError Int
0 ShortByteString
"enable push must be 0 or 1"
checkSettingsValue (SettingsKey
SettingsInitialWindowSize, Int
v)
| Int
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxWindowSize =
HTTP2Error -> Maybe HTTP2Error
forall a. a -> Maybe a
Just (HTTP2Error -> Maybe HTTP2Error) -> HTTP2Error -> Maybe HTTP2Error
forall a b. (a -> b) -> a -> b
$
ErrorCode -> Int -> ShortByteString -> HTTP2Error
ConnectionErrorIsSent
ErrorCode
FlowControlError
Int
0
ShortByteString
"Window size must be less than or equal to 65535"
checkSettingsValue (SettingsKey
SettingsMaxFrameSize, Int
v)
| Int
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
defaultPayloadLength Bool -> Bool -> Bool
|| Int
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxPayloadLength =
HTTP2Error -> Maybe HTTP2Error
forall a. a -> Maybe a
Just (HTTP2Error -> Maybe HTTP2Error) -> HTTP2Error -> Maybe HTTP2Error
forall a b. (a -> b) -> a -> b
$
ErrorCode -> Int -> ShortByteString -> HTTP2Error
ConnectionErrorIsSent
ErrorCode
ProtocolError
Int
0
ShortByteString
"Max frame size must be in between 16384 and 16777215"
checkSettingsValue (SettingsKey, Int)
_ = Maybe HTTP2Error
forall a. Maybe a
Nothing
data Config = Config
{ Config -> Buffer
confWriteBuffer :: Buffer
, Config -> Int
confBufferSize :: BufferSize
, Config -> ByteString -> IO ()
confSendAll :: ByteString -> IO ()
, Config -> Maybe (STM () -> ByteString -> IO (Either () ()))
confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
, Config -> Int -> IO ByteString
confReadN :: Int -> IO ByteString
, Config -> Maybe (STM () -> Int -> IO ByteString)
confReadNWith :: Maybe (STM () -> Int -> IO ByteString)
, Config -> PositionReadMaker
confPositionReadMaker :: PositionReadMaker
, Config -> Manager
confTimeoutManager :: TM.Manager
, Config -> SockAddr
confMySockAddr :: SockAddr
, Config -> SockAddr
confPeerSockAddr :: SockAddr
, Config -> Bool
confReadNTimeout :: Bool
, Config -> Int -> TokenHeaderTable -> IO ()
confOnInformational :: StreamId -> TokenHeaderTable -> IO ()
}
defaultConfig :: Config
defaultConfig :: Config
defaultConfig =
Config
{ confWriteBuffer :: Buffer
confWriteBuffer = Buffer
forall a. Ptr a
nullPtr
, confBufferSize :: Int
confBufferSize = Int
0
, confSendAll :: ByteString -> IO ()
confSendAll = \ByteString
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
, confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
confSendAllWith = Maybe (STM () -> ByteString -> IO (Either () ()))
forall a. Maybe a
Nothing
, confReadN :: Int -> IO ByteString
confReadN = \Int
_ -> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
""
, confReadNWith :: Maybe (STM () -> Int -> IO ByteString)
confReadNWith = Maybe (STM () -> Int -> IO ByteString)
forall a. Maybe a
Nothing
, confPositionReadMaker :: PositionReadMaker
confPositionReadMaker = PositionReadMaker
defaultPositionReadMaker
, confTimeoutManager :: Manager
confTimeoutManager = Manager
T.defaultManager
, confMySockAddr :: SockAddr
confMySockAddr = PortNumber -> Word32 -> SockAddr
SockAddrInet PortNumber
0 Word32
0
, confPeerSockAddr :: SockAddr
confPeerSockAddr = PortNumber -> Word32 -> SockAddr
SockAddrInet PortNumber
0 Word32
0
, confReadNTimeout :: Bool
confReadNTimeout = Bool
False
, confOnInformational :: Int -> TokenHeaderTable -> IO ()
confOnInformational = \Int
_ TokenHeaderTable
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
}
sendAllGivingUp :: Config -> STM () -> ByteString -> IO (Either () ())
sendAllGivingUp :: Config -> STM () -> ByteString -> IO (Either () ())
sendAllGivingUp Config{Bool
Int
Maybe (STM () -> Int -> IO ByteString)
Maybe (STM () -> ByteString -> IO (Either () ()))
Buffer
Manager
SockAddr
Int -> IO ByteString
Int -> TokenHeaderTable -> IO ()
PositionReadMaker
ByteString -> IO ()
confWriteBuffer :: Config -> Buffer
confBufferSize :: Config -> Int
confSendAll :: Config -> ByteString -> IO ()
confSendAllWith :: Config -> Maybe (STM () -> ByteString -> IO (Either () ()))
confReadN :: Config -> Int -> IO ByteString
confReadNWith :: Config -> Maybe (STM () -> Int -> IO ByteString)
confPositionReadMaker :: Config -> PositionReadMaker
confTimeoutManager :: Config -> Manager
confMySockAddr :: Config -> SockAddr
confPeerSockAddr :: Config -> SockAddr
confReadNTimeout :: Config -> Bool
confOnInformational :: Config -> Int -> TokenHeaderTable -> IO ()
confWriteBuffer :: Buffer
confBufferSize :: Int
confSendAll :: ByteString -> IO ()
confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
confReadN :: Int -> IO ByteString
confReadNWith :: Maybe (STM () -> Int -> IO ByteString)
confPositionReadMaker :: PositionReadMaker
confTimeoutManager :: Manager
confMySockAddr :: SockAddr
confPeerSockAddr :: SockAddr
confReadNTimeout :: Bool
confOnInformational :: Int -> TokenHeaderTable -> IO ()
..} STM ()
giveUp ByteString
bs = case Maybe (STM () -> ByteString -> IO (Either () ()))
confSendAllWith of
Just STM () -> ByteString -> IO (Either () ())
sendWith -> STM () -> ByteString -> IO (Either () ())
sendWith STM ()
giveUp ByteString
bs
Maybe (STM () -> ByteString -> IO (Either () ()))
Nothing -> () -> Either () ()
forall a b. b -> Either a b
Right (() -> Either () ()) -> IO () -> IO (Either () ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> IO ()
confSendAll ByteString
bs
readNGivingUp :: Config -> STM () -> Int -> IO ByteString
readNGivingUp :: Config -> STM () -> Int -> IO ByteString
readNGivingUp Config{Bool
Int
Maybe (STM () -> Int -> IO ByteString)
Maybe (STM () -> ByteString -> IO (Either () ()))
Buffer
Manager
SockAddr
Int -> IO ByteString
Int -> TokenHeaderTable -> IO ()
PositionReadMaker
ByteString -> IO ()
confWriteBuffer :: Config -> Buffer
confBufferSize :: Config -> Int
confSendAll :: Config -> ByteString -> IO ()
confSendAllWith :: Config -> Maybe (STM () -> ByteString -> IO (Either () ()))
confReadN :: Config -> Int -> IO ByteString
confReadNWith :: Config -> Maybe (STM () -> Int -> IO ByteString)
confPositionReadMaker :: Config -> PositionReadMaker
confTimeoutManager :: Config -> Manager
confMySockAddr :: Config -> SockAddr
confPeerSockAddr :: Config -> SockAddr
confReadNTimeout :: Config -> Bool
confOnInformational :: Config -> Int -> TokenHeaderTable -> IO ()
confWriteBuffer :: Buffer
confBufferSize :: Int
confSendAll :: ByteString -> IO ()
confSendAllWith :: Maybe (STM () -> ByteString -> IO (Either () ()))
confReadN :: Int -> IO ByteString
confReadNWith :: Maybe (STM () -> Int -> IO ByteString)
confPositionReadMaker :: PositionReadMaker
confTimeoutManager :: Manager
confMySockAddr :: SockAddr
confPeerSockAddr :: SockAddr
confReadNTimeout :: Bool
confOnInformational :: Int -> TokenHeaderTable -> IO ()
..} STM ()
giveUp Int
n = case Maybe (STM () -> Int -> IO ByteString)
confReadNWith of
Just STM () -> Int -> IO ByteString
readWith -> STM () -> Int -> IO ByteString
readWith STM ()
giveUp Int
n
Maybe (STM () -> Int -> IO ByteString)
Nothing -> Int -> IO ByteString
confReadN Int
n
sendGoaway :: Config -> ByteString -> IO ()
sendGoaway :: Config -> ByteString -> IO ()
sendGoaway Config
conf ByteString
frame = IO ()
send IO () -> (IOError -> IO ()) -> IO ()
forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError` \IOError
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
send :: IO ()
send = case Config -> Maybe (STM () -> ByteString -> IO (Either () ()))
confSendAllWith Config
conf of
Maybe (STM () -> ByteString -> IO (Either () ()))
Nothing -> Config -> ByteString -> IO ()
confSendAll Config
conf ByteString
frame
Just STM () -> ByteString -> IO (Either () ())
_ -> do
late <- Int -> IO (TVar Bool)
registerDelay Int
1000000
void $ sendAllGivingUp conf (readTVar late >>= check) frame
isAsyncException :: E.Exception e => e -> Bool
isAsyncException :: forall e. Exception e => e -> Bool
isAsyncException e
e =
case SomeException -> Maybe SomeAsyncException
forall e. Exception e => SomeException -> Maybe e
E.fromException (e -> SomeException
forall e. Exception e => e -> SomeException
E.toException e
e) of
Just (E.SomeAsyncException e
_) -> Bool
True
Maybe SomeAsyncException
Nothing -> Bool
False