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

66 lines
1.8 KiB
Haskell
Raw Normal View History

2019-05-16 03:00:10 +03:00
-- zuse: +http -----------------------------------------------------------------
module Vere.Http where
import ClassyPrelude
2019-07-02 05:51:26 +03:00
import Noun
2019-05-16 03:00:10 +03:00
2019-07-12 22:24:44 +03:00
import qualified Data.CaseInsensitive as CI
import qualified Network.HTTP.Types as HT
2019-05-24 02:58:18 +03:00
import qualified Network.HTTP.Types.Method as H
2019-06-28 00:28:58 +03:00
--------------------------------------------------------------------------------
2019-07-22 21:10:27 +03:00
data Header = Header Cord Cord
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
2019-05-16 03:00:10 +03:00
2019-05-24 02:58:18 +03:00
type Method = H.StdMethod
2019-05-16 03:00:10 +03:00
data Request = Request
{ method :: Method
2019-07-22 21:10:27 +03:00
, url :: Cord
, headerList :: [Header]
2019-07-22 21:10:27 +03:00
, body :: Maybe Octs
}
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
2019-05-16 03:00:10 +03:00
data ResponseHeader = ResponseHeader
{ statusCode :: Word
2019-07-12 22:24:44 +03:00
, headers :: [Header]
}
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
data Event
= Started ResponseHeader -- [%start hdr (unit octs) ?]
2019-07-22 21:10:27 +03:00
| Received Octs -- [%continue [~ octs] %.n]
| Done -- [%continue ~ %.y]
| Canceled -- %cancel
2019-07-22 21:10:27 +03:00
| Failed Cord -- %cancel
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
--------------------------------------------------------------------------------
instance ToNoun H.StdMethod where
2019-07-22 21:10:27 +03:00
toNoun = toNoun . Cord . decodeUtf8 . H.renderStdMethod
2019-06-28 00:28:58 +03:00
instance FromNoun H.StdMethod where
2019-07-22 21:10:27 +03:00
parseNoun n = named "StdMethod" $ do
2019-06-28 00:28:58 +03:00
Cord m <- parseNoun n
2019-07-22 21:10:27 +03:00
case H.parseMethod (encodeUtf8 m) of
2019-06-28 00:28:58 +03:00
Left bs -> fail ("Unexpected method: " <> unpack (decodeUtf8 bs))
Right m -> pure m
deriveNoun ''Header
deriveNoun ''ResponseHeader
deriveNoun ''Event
deriveNoun ''Request
2019-06-19 03:04:57 +03:00
2019-06-28 00:28:58 +03:00
--------------------------------------------------------------------------------
2019-05-24 02:58:18 +03:00
convertHeaders :: [HT.Header] -> [Header]
convertHeaders = fmap f
where
2019-07-22 21:10:27 +03:00
f (k, v) = Header (Cord $ decodeUtf8 $ CI.original k)
(Cord $ decodeUtf8 v)