0 | module TyTTP.HTTP.Producer
 1 |
 2 | import Data.Buffer
 3 | import TyTTP
 4 | import TyTTP.HTTP.Protocol
 5 |
 6 | unsafeFromString : String -> Buffer
 7 | unsafeFromString str = unsafePerformIO $ do
 8 |   let size = stringByteLength str
 9 |   Just buffer <- newBuffer $ cast size
10 |        | Nothing => assert_total $ idris_crash "could not create new buffer"
11 |   setString buffer 0 str
12 |   pure buffer
13 |
14 | export
15 | sendText :
16 |   Applicative m
17 |   => String
18 |   -> Context me u v h1 s StringHeaders a b
19 |   -> m $ Context me u v h1 s StringHeaders a (Publisher IO e Buffer)
20 | sendText str ctx = do
21 |   let stream : Publisher IO e Buffer = Stream.singleton $ unsafeFromString str
22 |   pure $ { response.body := stream
23 |          , response.headers := 
24 |            [ ("Content-Type", "text/plain")
25 |            , ("Content-Length", show $ length str)
26 |            ]
27 |          } ctx
28 |
29 | export
30 | status :
31 |   Applicative m
32 |   => Status
33 |   -> Context me u v h1 s h2 a b
34 |   -> m $ Context me u v h1 Status h2 a b
35 | status s ctx = pure $ { response.status := s } ctx
36 |
37 |