-
Notifications
You must be signed in to change notification settings - Fork 3
/
URLMap.hs
65 lines (47 loc) · 1.56 KB
/
URLMap.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
{-# LANGUAGE OverloadedStrings #-}
module URLMap (parseURLMap, URL, URLMap, ConvInfo(..)) where
import Control.Applicative hiding (many,optional,(<|>))
import qualified Data.ByteString.Char8 as S
import Network.Web.URI
import Text.Parsec
import Text.Parsec.String
type URL = String
data ConvInfo = CIFile !String | CICgi { progDir :: !String
, pathInURL :: !String
}
deriving (Eq,Show)
type URLMap = [(URL,ConvInfo)]
parseURLMap :: String -> URLMap
parseURLMap cs = either (fail . show) (map fixCGI) (parse umap "umap" cs)
where
fixCGI (k, ci@(CICgi _ _)) = (k, ci { pathInURL = scriptPath k})
fixCGI kv = kv
scriptPath x = maybe "" (S.unpack . uriPath) $ parseURI $ S.pack x
umap :: Parser URLMap
umap = comments *> many (line <* comments)
comments :: Parser ()
comments = () <$ many comment
comment :: Parser ()
comment = () <$ char '#' >> many (noneOf "\n") >> eol
line :: Parser (URL,ConvInfo)
line = (,) <$> uri <*> (fileOrCGI <* eol)
uri :: Parser URL
uri = spcs *> (str <* spcs)
fileOrCGI :: Parser ConvInfo
fileOrCGI = file <|> cgi
file :: Parser ConvInfo
file = CIFile <$> (arrow *> dir)
cgi :: Parser ConvInfo
cgi = CICgi <$> (darrow *> dir) <*> pure ""
arrow :: Parser ()
arrow = () <$ string "->" >> spcs
darrow :: Parser ()
darrow = () <$ string "=>" >> spcs
dir :: Parser FilePath
dir = str <* spcs
str :: Parser String
str = many1 (noneOf " \t\n")
eol :: Parser ()
eol = () <$ char '\n'
spcs :: Parser ()
spcs = () <$ many (oneOf " \t")