This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
45 lines (42 loc) · 1.55 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Main where
import Data.Functor
import Z.Data.HTTP
import Z.Data.HTTP.Internal
import Z.Data.HTTP.Request as Q
import Z.Data.HTTP.Response
import Z.Data.Internal
import qualified Z.Data.Text as T
import qualified Z.Data.Vector as V
import Z.HTTP.Client
import Z.HTTP.Internal
import Z.IO
import Z.IO.Internal
import Z.IO.Network
main :: IO ()
main = do
addr <- resolveDNS mozHost defaultHintsHTTP
withResource (initTCPClient defaultTCPClientConfig {tcpRemoteAddr = addrAddress addr}) $ \tcp -> do
(i, o) <- newBufferedIO tcp
writeBuffer' o $ requestToBytes (buildRequest $ Q.http GET "/" HTTP1_1 >> setHost mozHost >> setHeader AcceptLanguage LANG_FR)
readBuffer i >>= print . T.validate
where
mozHost :: Host = ("developer.mozilla.org", defaultPortHTTP)
readme :: IO ()
readme = do
-- Resolve DNS and start a TCP client for the HTTP connection.
-- The client is guaranteed to be closed automatically after all tasks were done.
addr <- resolveDNS mozHost defaultHintsHTTP
withResource (initTCPClient defaultTCPClientConfig {tcpRemoteAddr = addrAddress addr}) $ \tcp -> do
-- Start a HTTP client.
c <- httpClient tcp
-- Build a simple HTTP request.
let q = buildRequest $ do
Q.http GET "/" HTTP1_1
setHost mozHost
setHeader AcceptLanguage LANG_FR
-- Send the request.
sendRequest c q
-- Receive a response then perform some actions using the response.
withResponse c $ \a -> putStrLn $ debugShow (responseToBytes a)
where
mozHost :: Host = ("developer.mozilla.org", defaultPortHTTP)