2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Http Client Driver
|
|
|
|
|
|
|
|
TODO When making a request, handle the case where the request id is
|
|
|
|
already in use.
|
2019-05-17 04:25:58 +03:00
|
|
|
-}
|
2019-05-16 03:00:10 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.Vere.Http.Client where
|
2019-05-16 03:00:10 +03:00
|
|
|
|
2021-03-06 02:48:07 +03:00
|
|
|
import Urbit.Prelude hiding (Builder, finally)
|
2019-07-12 22:18:14 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Vere.Http
|
2020-05-13 22:35:57 +03:00
|
|
|
import Urbit.Vere.Pier.Types
|
2020-06-10 22:22:45 +03:00
|
|
|
import Urbit.King.App
|
2020-05-13 22:35:57 +03:00
|
|
|
|
|
|
|
import Urbit.Arvo (BlipEv(..), Ev(..), HttpClientEf(..), HttpClientEv(..),
|
|
|
|
HttpClientReq(..), HttpEvent(..), KingId, ResponseHeader(..))
|
|
|
|
|
2021-03-06 02:48:07 +03:00
|
|
|
import RIO.Orphans ()
|
|
|
|
import Control.Monad.Catch (finally)
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2020-09-22 23:18:35 +03:00
|
|
|
import qualified Data.Map.Strict as M
|
2019-10-02 23:55:30 +03:00
|
|
|
import qualified Network.HTTP.Client as H
|
|
|
|
import qualified Network.HTTP.Client.TLS as TLS
|
|
|
|
import qualified Network.HTTP.Types as HT
|
2019-05-17 04:25:58 +03:00
|
|
|
|
2020-05-13 22:35:57 +03:00
|
|
|
|
2019-06-28 00:28:58 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
2019-05-17 04:25:58 +03:00
|
|
|
|
|
|
|
type ReqId = Word
|
|
|
|
|
2019-09-05 23:09:45 +03:00
|
|
|
data HttpClientDrv = HttpClientDrv
|
|
|
|
{ hcdManager :: H.Manager
|
|
|
|
, hcdLive :: TVar (Map ReqId (Async ()))
|
2019-05-17 04:25:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-09-05 23:09:45 +03:00
|
|
|
cvtReq :: HttpClientReq -> Maybe H.Request
|
2019-05-18 00:52:12 +03:00
|
|
|
cvtReq r =
|
2019-07-22 21:10:27 +03:00
|
|
|
H.parseRequest (unpack (unCord $ url r)) <&> \init -> init
|
2019-08-08 01:24:02 +03:00
|
|
|
{ H.method = encodeUtf8 $ tshow (method r)
|
|
|
|
, H.requestHeaders = unconvertHeaders (headerList r)
|
|
|
|
, H.requestBody =
|
2019-05-18 00:52:12 +03:00
|
|
|
H.RequestBodyBS $ case body r of
|
2019-07-22 21:10:27 +03:00
|
|
|
Nothing -> ""
|
|
|
|
Just (Octs bs) -> bs
|
2019-05-18 00:52:12 +03:00
|
|
|
}
|
2019-05-17 04:25:58 +03:00
|
|
|
|
|
|
|
cvtRespHeaders :: H.Response a -> ResponseHeader
|
2019-05-18 00:52:12 +03:00
|
|
|
cvtRespHeaders resp =
|
2019-06-26 03:15:49 +03:00
|
|
|
ResponseHeader (fromIntegral $ HT.statusCode (H.responseStatus resp)) heads
|
2019-05-18 00:52:12 +03:00
|
|
|
where
|
2019-05-24 02:58:18 +03:00
|
|
|
heads = convertHeaders (H.responseHeaders resp)
|
2019-05-18 00:52:12 +03:00
|
|
|
|
2019-10-02 02:22:27 +03:00
|
|
|
bornEv :: KingId -> Ev
|
|
|
|
bornEv king =
|
|
|
|
EvBlip $ BlipEvHttpClient $ HttpClientEvBorn (king, ()) ()
|
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
_bornFailed :: e -> WorkError -> IO ()
|
|
|
|
_bornFailed env _ = runRIO env $ do
|
2020-06-02 23:48:07 +03:00
|
|
|
pure () -- TODO What to do in this case?
|
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
client'
|
|
|
|
:: HasPierEnv e
|
|
|
|
=> RIO e ([Ev], RAcquire e (DriverApi HttpClientEf))
|
|
|
|
client' = do
|
|
|
|
ventQ :: TQueue EvErr <- newTQueueIO
|
|
|
|
env <- ask
|
|
|
|
|
|
|
|
let (bornEvs, startDriver) = client env (writeTQueue ventQ)
|
|
|
|
|
|
|
|
let runDriver = do
|
|
|
|
diOnEffect <- startDriver
|
|
|
|
let diEventSource = fmap RRWork <$> tryReadTQueue ventQ
|
|
|
|
pure (DriverApi {..})
|
|
|
|
|
|
|
|
pure (bornEvs, runDriver)
|
|
|
|
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Iris -- HTTP Client Driver
|
|
|
|
|
|
|
|
Until born events succeeds, ignore effects.
|
|
|
|
Wait until born event callbacks invoked.
|
|
|
|
If success, signal success.
|
|
|
|
If failure, try again several times.
|
|
|
|
If still failure, bring down ship.
|
|
|
|
Once born event succeeds, hold on to effects.
|
|
|
|
Once all other drivers have booted:
|
|
|
|
- Execute stashed effects.
|
|
|
|
- Begin normal operation (start accepting requests)
|
|
|
|
-}
|
2020-05-13 22:35:57 +03:00
|
|
|
client
|
|
|
|
:: forall e
|
|
|
|
. (HasLogFunc e, HasKingId e)
|
|
|
|
=> e
|
2020-06-02 23:48:07 +03:00
|
|
|
-> (EvErr -> STM ())
|
2020-06-10 22:22:45 +03:00
|
|
|
-> ([Ev], RAcquire e (HttpClientEf -> IO ()))
|
2020-06-02 23:48:07 +03:00
|
|
|
client env plan = (initialEvents, runHttpClient)
|
2019-05-17 03:05:34 +03:00
|
|
|
where
|
2020-05-13 22:35:57 +03:00
|
|
|
kingId = view (kingIdL . to fromIntegral) env
|
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
initialEvents :: [Ev]
|
|
|
|
initialEvents = [bornEv kingId]
|
2019-10-02 02:22:27 +03:00
|
|
|
|
2020-06-07 02:34:27 +03:00
|
|
|
runHttpClient :: RAcquire e (HttpClientEf -> IO ())
|
2019-09-13 23:06:13 +03:00
|
|
|
runHttpClient = handleEffect <$> mkRAcquire start stop
|
2019-09-05 23:09:45 +03:00
|
|
|
|
|
|
|
start :: RIO e (HttpClientDrv)
|
2019-09-13 23:06:13 +03:00
|
|
|
start = HttpClientDrv <$>
|
2019-10-02 23:55:30 +03:00
|
|
|
(io $ H.newManager TLS.tlsManagerSettings) <*>
|
2019-09-13 23:06:13 +03:00
|
|
|
newTVarIO M.empty
|
2019-09-05 23:09:45 +03:00
|
|
|
|
|
|
|
stop :: HttpClientDrv -> RIO e ()
|
|
|
|
stop HttpClientDrv{..} = do
|
|
|
|
-- Cancel all the outstanding asyncs, ignoring any exceptions.
|
|
|
|
liveThreads <- atomically $ readTVar hcdLive
|
|
|
|
mapM_ cancel liveThreads
|
|
|
|
|
2020-06-07 02:34:27 +03:00
|
|
|
handleEffect :: HttpClientDrv -> HttpClientEf -> IO ()
|
2019-09-05 23:09:45 +03:00
|
|
|
handleEffect drv = \case
|
2020-06-07 02:34:27 +03:00
|
|
|
HCERequest _ id req -> runRIO env (newReq drv id req)
|
|
|
|
HCECancelRequest _ id -> runRIO env (cancelReq drv id)
|
2019-09-05 23:09:45 +03:00
|
|
|
|
|
|
|
newReq :: HttpClientDrv -> ReqId -> HttpClientReq -> RIO e ()
|
|
|
|
newReq drv id req = do
|
|
|
|
async <- runReq drv id req
|
2021-03-06 02:48:07 +03:00
|
|
|
-- If the async has somehow already completed, don't put it in the map,
|
|
|
|
-- because then it might never get out.
|
|
|
|
atomically $ pollSTM async >>= \case
|
|
|
|
Nothing -> modifyTVar' (hcdLive drv) (insertMap id async)
|
|
|
|
Just _ -> pure ()
|
2019-09-05 23:09:45 +03:00
|
|
|
|
|
|
|
-- The problem with the original http client code was that it was written
|
|
|
|
-- to the idea of what the events "should have" been instead of what they
|
|
|
|
-- actually were. This means that this driver doesn't run like the vere
|
|
|
|
-- http client driver. The vere driver was written assuming that parts of
|
|
|
|
-- events could be compressed together: a Start might contain the only
|
|
|
|
-- chunk of data and immediately complete, where here the Start event, the
|
|
|
|
-- Continue (with File) event, and the Continue (completed) event are three
|
|
|
|
-- separate things.
|
|
|
|
runReq :: HttpClientDrv -> ReqId -> HttpClientReq -> RIO e (Async ())
|
2021-03-06 02:48:07 +03:00
|
|
|
runReq HttpClientDrv{..} id req = async $ flip finally aftr $
|
2019-09-05 23:09:45 +03:00
|
|
|
case cvtReq req of
|
|
|
|
Nothing -> do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(malformed http client request)", id, req)
|
2019-09-05 23:09:45 +03:00
|
|
|
planEvent id (Cancel ())
|
|
|
|
Just r -> do
|
|
|
|
logDebug $ displayShow ("(http client request)", id, req)
|
|
|
|
withRunInIO $ \run ->
|
|
|
|
H.withResponse r hcdManager $ \x -> run (exec x)
|
|
|
|
where
|
2021-03-06 02:48:07 +03:00
|
|
|
-- Make sure to remove our entry from hcdLive after we're done so the
|
|
|
|
-- map doesn't grow without bound.
|
|
|
|
aftr :: RIO e ()
|
|
|
|
aftr = atomically $ modifyTVar' hcdLive (deleteMap id)
|
|
|
|
|
2019-09-05 23:09:45 +03:00
|
|
|
recv :: H.BodyReader -> RIO e (Maybe ByteString)
|
|
|
|
recv read = io $ read <&> \case chunk | null chunk -> Nothing
|
|
|
|
| otherwise -> Just chunk
|
|
|
|
|
|
|
|
exec :: H.Response H.BodyReader -> RIO e ()
|
|
|
|
exec resp = do
|
|
|
|
let headers = cvtRespHeaders resp
|
|
|
|
getChunk = recv (H.responseBody resp)
|
|
|
|
loop = getChunk >>= \case
|
|
|
|
Nothing -> planEvent id (Continue Nothing True)
|
|
|
|
Just bs -> do
|
2019-09-13 23:06:13 +03:00
|
|
|
planEvent id $
|
|
|
|
Continue (Just $ File $ Octs bs) False
|
2019-09-05 23:09:45 +03:00
|
|
|
loop
|
|
|
|
planEvent id (Start headers Nothing False)
|
|
|
|
loop
|
|
|
|
|
|
|
|
planEvent :: ReqId -> HttpEvent -> RIO e ()
|
|
|
|
planEvent id ev = do
|
|
|
|
logDebug $ displayShow ("(http client response)", id, (describe ev))
|
2020-06-02 23:48:07 +03:00
|
|
|
|
|
|
|
let recvEv = EvBlip
|
|
|
|
$ BlipEvHttpClient
|
|
|
|
$ HttpClientEvReceive (kingId, ()) (fromIntegral id) ev
|
|
|
|
|
|
|
|
let recvFailed _ = pure ()
|
|
|
|
|
|
|
|
atomically $ plan (EvErr recvEv recvFailed)
|
2019-09-05 23:09:45 +03:00
|
|
|
|
|
|
|
-- show an HttpEvent with byte count instead of raw data
|
|
|
|
describe :: HttpEvent -> String
|
|
|
|
describe (Start header Nothing final) =
|
|
|
|
"(Start " ++ (show header) ++ " ~ " ++ (show final)
|
|
|
|
describe (Start header (Just (File (Octs bs))) final) =
|
2019-09-13 23:06:13 +03:00
|
|
|
"(Start " ++ (show header) ++ " (" ++ (show $ length bs) ++ " bytes) " ++
|
|
|
|
(show final)
|
2019-09-05 23:09:45 +03:00
|
|
|
describe (Continue Nothing final) =
|
|
|
|
"(Continue ~ " ++ (show final)
|
|
|
|
describe (Continue (Just (File (Octs bs))) final) =
|
|
|
|
"(Continue (" ++ (show $ length bs) ++ " bytes) " ++ (show final)
|
|
|
|
describe (Cancel ()) = "(Cancel ())"
|
|
|
|
|
|
|
|
waitCancel :: Async a -> RIO e (Either SomeException a)
|
|
|
|
waitCancel async = cancel async >> waitCatch async
|
|
|
|
|
|
|
|
cancelThread :: ReqId -> Async a -> RIO e ()
|
|
|
|
cancelThread id =
|
|
|
|
waitCancel >=> \case Left _ -> planEvent id $ Cancel ()
|
|
|
|
Right _ -> pure ()
|
|
|
|
|
|
|
|
cancelReq :: HttpClientDrv -> ReqId -> RIO e ()
|
|
|
|
cancelReq drv id =
|
|
|
|
join $ atomically $ do
|
|
|
|
tbl <- readTVar (hcdLive drv)
|
|
|
|
case lookup id tbl of
|
|
|
|
Nothing -> pure (pure ())
|
|
|
|
Just async -> do writeTVar (hcdLive drv) (deleteMap id tbl)
|
|
|
|
pure (cancelThread id async)
|