shrub/pkg/hs-urbit/lib/Vere/Http/Server.hs

141 lines
3.6 KiB
Haskell
Raw Normal View History

2019-05-16 03:00:10 +03:00
-- +http-server ----------------------------------------------------------------
module Vere.Http.Server where
import ClassyPrelude
import Vere.Http
import Noun
import Noun.TH
2019-05-31 05:53:00 +03:00
import Control.Lens
2019-05-16 03:00:10 +03:00
2019-05-25 02:08:59 +03:00
import Control.Concurrent (ThreadId, killThread, forkIO)
2019-05-31 05:53:00 +03:00
import qualified Data.ByteString as BS
import qualified Network.HTTP.Types as H
import qualified Network.Wai as W
import qualified Network.Wai.Handler.Warp as W
2019-05-25 02:03:46 +03:00
import qualified Network.Wai.Handler.WarpTLS as W
2019-05-24 02:58:18 +03:00
2019-05-16 03:00:10 +03:00
type ServerId = Word
type ConnectionId = Word
type RequestId = Word
data Foo = A | B | C
2019-05-16 03:00:10 +03:00
data Eff = Eff ServerId ConnectionId RequestId ServerRequest
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
2019-05-16 03:00:10 +03:00
-- | An http server effect is configuration, or it sends an outbound response
data ServerRequest
= SetConfig Config
| Response Event
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
2019-05-16 03:00:10 +03:00
data Config = Config
{ secure :: Maybe (Key, Cert)
, proxy :: Bool
, log :: Bool
, redirect :: Bool
}
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
2019-05-16 03:00:10 +03:00
2019-05-16 03:04:21 +03:00
-- Note: We need to parse PEM-encoded RSA private keys and cert or cert chain
-- from Wain
2019-06-19 03:04:57 +03:00
type Key = PEM
type Cert = PEM
2019-06-28 00:28:58 +03:00
newtype Wain = Wain [Text]
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
2019-05-16 03:00:10 +03:00
newtype PEM = PEM Cord
2019-06-28 00:28:58 +03:00
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
deriveNoun ''ServerRequest
deriveNoun ''Config
deriveNoun ''Eff
2019-05-16 03:04:21 +03:00
2019-05-16 03:00:10 +03:00
data ClientResponse
= Progress ResponseHeader Int (Maybe Int) (Maybe ByteString)
| Finished ResponseHeader (Maybe MimeData)
| Cancel
data MimeData = MimeData Text ByteString
2019-05-24 02:58:18 +03:00
--
2019-05-25 02:03:46 +03:00
data Ev
data State = State
{ thread :: MVar (Maybe (Config, ThreadId))
, sChan :: MVar Ev
}
init :: IO State
init =
-- When we initialize things, we send an event into arvo
-- When we receive the set-config event, then we start stuff up
-- This works for now, but we need to actually do stuff per above.
State <$> newMVar Nothing
<*> newEmptyMVar
onSetConfig :: State -> Config -> IO ()
onSetConfig s c = do
v <- takeMVar (thread s)
maybe (pure ()) (killThread . snd) v
putMVar (thread s) Nothing
startServer s c
startServer :: State -> Config -> IO ()
startServer s c = do
tls <- case (secure c) of
Nothing -> error "no wai"
2019-06-19 03:04:57 +03:00
Just (PEM key, PEM cert) ->
pure (W.tlsSettingsMemory (unCord cert) (unCord key))
2019-05-25 02:03:46 +03:00
-- we need to do the dance where we do the socket checking dance. or shove a
-- socket into it.
2019-05-25 02:08:59 +03:00
tid <- forkIO $ W.runTLS tls W.defaultSettings (app s)
putMVar (thread s) (Just (c, tid))
2019-05-25 02:03:46 +03:00
app :: State -> W.Application
app s req respond = bracket_
(pure ())
(pure ())
(respond $ W.responseLBS H.status200 [] "Hello World")
cookMeth :: W.Request -> Maybe Method
2019-05-24 02:58:18 +03:00
cookMeth re =
2019-05-25 02:03:46 +03:00
case H.parseMethod (W.requestMethod re) of
2019-05-24 02:58:18 +03:00
Left _ -> Nothing
Right m -> Just m
data Octs = Octs Atom Atom
2019-05-31 05:53:00 +03:00
bsOcts :: Iso' ByteString Octs
bsOcts = iso toOcts fromOcts
where
toOcts :: ByteString -> Octs
toOcts bs =
2019-07-12 04:16:40 +03:00
Octs (fromIntegral (length bs)) (bs ^. from atomBytes)
2019-05-31 05:53:00 +03:00
fromOcts :: Octs -> ByteString
fromOcts (Octs (fromIntegral -> len) atm) = bs <> pad
where
2019-07-12 04:16:40 +03:00
bs = atm ^. atomBytes
2019-05-31 05:53:00 +03:00
pad = BS.replicate (max 0 (len - length bs)) 0
2019-05-24 02:58:18 +03:00
2019-05-25 02:03:46 +03:00
readEvents :: W.Request -> IO Request
2019-05-24 02:58:18 +03:00
readEvents request = do
let Just method = cookMeth request
2019-05-25 02:03:46 +03:00
url = decodeUtf8 (W.rawPathInfo request)
headers = convertHeaders (W.requestHeaders request)
bodyLbs <- W.strictRequestBody request
2019-05-24 02:58:18 +03:00
let body = if length bodyLbs == 0 then Nothing
else Just (toStrict bodyLbs)
-- TODO: Check if wai just deletes the 'host': header like h2o does?
pure (Request method url headers body)