-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.hs
83 lines (67 loc) · 2.38 KB
/
Route.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{-# LANGUAGE DeriveDataTypeable, OverloadedStrings, TupleSections #-}
module Route
( Route
, Path
, Target(..)
, parseRoute
, lookupTarget
) where
import Data.ByteString.Char8 (unpack)
import Data.Char (isDigit)
import Data.Function (on)
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Ord
import qualified Data.Text as T
import Network.HTTP.Types
import Network.URI
import Network.Wai
type NixPathElem = String
data Target = NixHandler FilePath [NixPathElem]
| StaticPath FilePath
| StaticResp Status
deriving (Show, Eq)
type Route = (URI,Target)
type Path = [String]
parseRoute :: (String,String) -> Either String Route
parseRoute (p,t) = do
uPath <- maybe (fail $ "Not a path: "++t) return (parseRelativeReference p)
uTarget <- maybe (fail $ "Not an URI: "++t) return (parseURI t)
t' <- parseTarget uTarget
return (uPath,t')
lookupTarget :: Request -> [Route] -> (Target,Path)
lookupTarget req routes = fromMaybe (StaticResp notFound404, reqPath req) $ do
let routes' = filter (reqMatchUri req . fst) routes
(u,t) <- listToMaybe $ sortBy (orderUri `on` fst) routes'
return (t, drop (length $ uriPath' u) (reqPath req))
-- Private interface --
parseTarget :: URI -> Either String Target
parseTarget uri = case uriScheme uri of
"nix:" -> return $ NixHandler (uriPath uri) []
"file:" -> return $ StaticPath (uriPath uri)
"resp:" -> case reads (dropWhile (not . isDigit) (uriPath uri)) of
[(n,"")] -> return $ StaticResp (mkStatus n "")
_ -> fail $ "Not a valid response status: " ++ uriPath uri
s -> fail $ "Not a valid URI scheme: " ++ s
reqMatchUri :: Request -> URI -> Bool
reqMatchUri req uri =
(uriPath' uri `isPrefixOf` reqPath req) &&
all (flip elem $ reqQuery req) (uriQuery' uri)
orderUri :: URI -> URI -> Ordering
orderUri u1 u2 = case comparing (length . uriPath') u1 u2 of
LT -> GT
GT -> LT
EQ -> comparing (length . uriQuery') u2 u1
reqPath :: Request -> Path
reqPath req = map T.unpack (pathInfo req)
reqQuery :: Request -> [String]
reqQuery req = map qiStr (queryString req)
where
qiStr (p, Nothing) = unpack p
qiStr (p, Just v) = unpack p ++ "=" ++ unpack v
uriPath' :: URI -> Path
uriPath' uri = split ((dropDelims . dropBlanks) (onSublist "/")) (uriPath uri)
uriQuery' :: URI -> [String]
uriQuery' uri =
split ((dropDelims . dropBlanks) (onSublist "&")) (drop 1 $ uriQuery uri)