-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechoWebServer.hs
58 lines (48 loc) · 2.19 KB
/
echoWebServer.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
46
47
48
49
50
51
52
53
54
55
56
57
58
import Network (listenOn, accept, withSocketsDo, PortID(PortNumber))
import Network.Socket (socketToHandle, setSocketOption, SocketOption(ReuseAddr), Socket, PortNumber)
import System.IO
import Control.Concurrent (forkIO, ThreadId)
import Control.Monad (forever)
import Control.Monad.Fix (fix)
import Data.Word(Word16)
data HttpRequest = HttpRequest { url :: String,
contentType :: String,
version :: (Int, Int),
cookies :: [(String, String)],
userAgent :: String,
keepAlive :: Bool
}
data HttpResponse = HttpResponse { code :: Int,
body :: String
}
main :: IO ()
main = runServer $ onPort 8080
onPort :: Int -> PortID
onPort p = PortNumber $ fromIntegral p
runServer :: PortID -> IO ()
runServer port = withSocketsDo $ do
sock <- listenOn port
setSocketOption sock ReuseAddr 1
forever $ acceptConnection connectionHandler sock
where
connectionHandler = handleHttpConnection dummyHandler
acceptConnection :: (Handle -> IO()) -> Socket -> IO ThreadId
acceptConnection handler socket = do
connection <- accept socket
let (handle, host, port) = connection
forkIO $ handler $ handle
handleHttpConnection :: (HttpRequest -> IO HttpResponse) -> Handle -> IO ()
handleHttpConnection handler connection = parseHttpRequest connection >>= handler >>= writeHttpResponse connection
writeHttpResponse :: Handle -> HttpResponse -> IO()
writeHttpResponse handle response = do
hPutStr handle "HTTP/1.1 200 OK\n"
hPutStr handle "Content-Length: 11\n\n"
hPutStr handle "Hello World\n"
hFlush handle
hClose handle -- TODO: Keep this open if it's a keepalive request
parseHttpRequest :: Handle -> IO HttpRequest
parseHttpRequest handle = do
line <- hGetLine handle
return HttpRequest{url="/foo", contentType="text/html", version=(1,1), cookies=[], userAgent="Mozilla", keepAlive=False}
dummyHandler :: HttpRequest -> IO HttpResponse
dummyHandler request = return HttpResponse{code=200, body="Hello world!"}