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

66 lines
1.7 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
import Noun.TH
2019-05-16 03:00:10 +03:00
2019-05-24 02:58:18 +03:00
import qualified Data.CaseInsensitive as CI
import qualified Network.HTTP.Types as HT
import qualified Network.HTTP.Types.Method as H
2019-06-28 00:28:58 +03:00
--------------------------------------------------------------------------------
2019-05-16 03:00:10 +03:00
data Header = Header Text Text
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
, url :: Text
, headerList :: [Header]
, body :: Maybe ByteString
}
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
, headers :: [Header]
}
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
data Event
= Started ResponseHeader -- [%start hdr (unit octs) ?]
| Received ByteString -- [%continue [~ octs] %.n]
| Done -- [%continue ~ %.y]
| Canceled -- %cancel
| Failed Text -- %cancel
2019-06-28 00:28:58 +03:00
deriving (Eq, Ord, Show)
--------------------------------------------------------------------------------
instance ToNoun H.StdMethod where
toNoun = toNoun . Cord . H.renderStdMethod
instance FromNoun H.StdMethod where
parseNoun n = do
Cord m <- parseNoun n
case H.parseMethod m of
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
f (k, v) = Header (decodeUtf8 (CI.original k)) (decodeUtf8 v)