-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.hs
More file actions
79 lines (61 loc) · 1.74 KB
/
Server.hs
File metadata and controls
79 lines (61 loc) · 1.74 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module Server where
--import Data.Char
--import System.IO
--import Network
--import Data.Time.LocalTime
import Control.Arrow
import Text.Printf
data RequestType
= GET
| POST
deriving (Show)
data Request = Request
{ rtype :: RequestType
, path :: String
, options :: [(String,String)]
}
deriving Show
data Response = Response
{ version :: String
, statuscode :: Int
, location :: String
}
instance Show Response where
show r = format
( version r)
(show $ statuscode r)
( showCode r)
( location r)
where
format = printf $ unlines
[ "%s %s %s\r"
, "Server: HHydra beta\r"
, "Content-Length: 0\r"
, "Location: %s\r"
, "\r"
, "\r"
]
showCode response = case statuscode response of
100 -> "Continue"
200 -> "OK"
302 -> "Found"
404 -> "Not Found"
other -> error $ "status code not supported: " ++ show other
fromString :: String -> RequestType
fromString type_ = case type_ of
"GET" -> GET
"POST" -> POST
other -> error $ "request head not supported: " ++ other
--- This should really validate input or something. Separate validator? Or as-we-go?
parseRequest :: [String] -> Request
parseRequest lns = case words $ head lns of
[t,p,_] -> Request
{ rtype = fromString t
, path = p
, options = parseOptions $ tail lns
}
other -> error $ "corrupted request: " ++ show other
where
parseOptions :: [String] -> [(String,String)]
parseOptions = takeWhile (':' `elem`) >>> map (break (== ' '))