1 -- |This is an auxiliary parser utilities for parsing things related
4 -- In general you don't have to use this module directly.
5 module Network.HTTP.Lucu.Parser.Http
19 import qualified Data.ByteString.Lazy.Char8 as B
20 import Data.ByteString.Lazy.Char8 (ByteString)
22 import Network.HTTP.Lucu.Parser
24 -- |@'isCtl' c@ is True iff @0x20 <= @c@ < 0x7F@.
31 -- |@'isSeparator' c@ is True iff c is one of HTTP separators.
32 isSeparator :: Char -> Bool
33 isSeparator c = elem c "()<>@,;:\\\"/[]?={} \t"
35 -- |@'isChar' c@ is True iff @c <= 0x7f@.
36 isChar :: Char -> Bool
41 -- |@'isToken' c@ is equivalent to @not ('isCtl' c || 'isSeparator'
43 isToken :: Char -> Bool
44 isToken c = not (isCtl c || isSeparator c)
46 -- |@'listOf' p@ is similar to @'Network.HTTP.Lucu.Parser.sepBy' p
47 -- ('Network.HTTP.Lucu.Parser.char' \',\')@ but it allows any
48 -- occurrences of LWS before and after each tokens.
49 listOf :: Parser a -> Parser [a]
50 listOf p = do many lws
55 -- |'token' is equivalent to @'Network.HTTP.Lucu.Parser.many1' $
56 -- 'Network.HTTP.Lucu.Parser.satisfy' 'isToken'@
57 token :: Parser String
58 token = many1 $ satisfy isToken
60 -- |'lws' is an HTTP LWS: @'Network.HTTP.Lucu.Parser.crlf'?
61 -- ('Network.HTTP.Lucu.Parser.sp' | 'Network.HTTP.Lucu.Parser.ht')+@
63 lws = do s <- option "" crlf
64 xs <- many1 (sp <|> ht)
67 -- |'text' accepts one character which doesn't satisfy 'isCtl'.
69 text = satisfy (\ c -> not (isCtl c))
71 -- |'separator' accepts one character which satisfies 'isSeparator'.
72 separator :: Parser Char
73 separator = satisfy isSeparator
75 -- |'quotedStr' accepts a string surrounded by double quotation
76 -- marks. Quotes can be escaped by backslashes.
77 quotedStr :: Parser String
78 quotedStr = do char '"'
79 xs <- many (qdtext <|> quotedPair)
81 return $ foldr (++) "" xs
83 qdtext = do c <- satisfy (/= '"')
86 quotedPair = do q <- char '\\'